Gauss Elimination Method:
Code:
clc;
clear;
close
all;
% Prompt user for the number of equations
n = input('Enter the number of equations: ');
% Initialize matrices
A = zeros(n, n); % Coefficient matrix
B = zeros(n, 1); % Constant matrix
% Input coefficients and constants
disp('Enter the coefficients row-wise followed by the constant
term:'); for i = 1:n
row = input('', 's'); % Read input as string
values = str2num(row); % Convert to numerical
array A(i, :) = values(1:end-1); % Assign
coefficients
B(i) = values(end); % Assign
constant end
% Augmented Matrix
Aug = [A B];
% Forward Elimination
for i = 1:n-1
for j = i+1:n
factor = Aug(j,i) / Aug(i,i);
Aug(j, :) = Aug(j, :) - factor * Aug(i, :);
end
end
% Back Substitution
X = zeros(n,1);
X(n) =
Aug(n,end)/Aug(n,n); for
i = n-1:-1:1
X(i) = (Aug(i,end) - Aug(i,i+1:n) * X(i+1:n)) / Aug(i,i);
end
% Displaying results
disp('Solution of the
system:'); for i = 1:n
fprintf('x%d = %.4f\n',i,X(i));
end
Output:
Enter the number of equations:
3
Enter the coefficients row-wise followed by the constant term:
2 3 1 9
4 1 2 13
3 2 3 14
Solution of the system:
x1 = 2.0000
x2 = 1.0000
x3 = 2.0000
Solver:
A=[2 3 1;4 1 2;3 2 3];
>> B=[9; 13; 14];
>>
sol=linsolve(A,B)
sol =
2
1
2
Gauss Seidal:
Code:
fx=inline('((28-(2*y)-(6*z))/20)');
fy=inline('((-23-x-(9*z))/20)');
fz=inline('((-57-(2*x)+(7*y))/-20)');
n=input('Enter the No. of Iterations=
'); x=0;
y=0;
z=0;
for (i=0:n-1)
x=fx(y,z);
y=fy(x,z);
z=fz(x,y)
; end
fprintf('Value of x,y,z are:%f %f %f',x,y,z);
Output:
gausssiedal
Enter the No. of
Iterations= 11
Value of x,y,z are:0.514829 -2.945292 3.932335
Solver:
>> B= [-23; -57; 28];
>> A=[1, 20, 9;2 ,-7, -20;20, 2, 6];
>>
sol=linsolve(A,B)
sol =
0.5148
-2.9453
3.9323
Newton Raphson:
Code:
% Newton Rhapson Method to find roots of the equation. Number of iterations
% and Accuracy
function Newton_Rhapson()
clc;
x1 = input('\n Enter the initial
guess:'); acc = input('\n Enter the
accuracy: ');
n = input('\n Enter the number of iteration: ');
fprintf('\n itr_no x1 x2 (x2-
x1)'); for i=1:1:n
x2 = x1-(f(1)/df(x1));
fprintf('\n %d%f%f%f', i,x1,x2,abs(x2-
x1)); if abs(x2-x1)>acc
x1=x2
; else
fprintf('\n The root f the given equation is %f',x2);
en
d
en
d
en
d
function y = f(x)
y = ((3.141*((x^2)/2))-0.5);
end
function
z=df(x) z =
(3.141*x);
end
Output:
0.1
Enter the accuracy:
0.001
Enter the number of
iteration: 7
itr_no x1 x2 (x2-x1)
10.100000-
3.3081503.408150
2-3.308150-3.2051270.103023
3-3.205127-3.0987930.106334
4-3.098793-2.9888100.109983
5-2.988810-2.8747800.114030
6-2.874780-2.7562260.118553
7-2.756226-2.6325730.123653
Solver:
x=fzero("(3.141*x*x*x/2-
0.5)",0.1) x =
0.6828
Trapezoidal Rule:
Code:
function trapezoidalintegral()
%clc
xa = input('Enter the value of lower limit:
'); xb = input('Enter the value of upper
limit: '); n = input('Enter the number of
strips n: '); fprintf('Area under the curve
\n');
fn = input('Enter the equation:
','s'); f = inline(fn);
h = (xb - xa) /n;
sum = 0;
for i = 0: 1: n-1
sum = sum+ f(xa)
+f(xa+h); xa = xa+h;
end
integral = sum * h/2;
fprintf('the value of integration is :
%f',integral); end
Output:
0
Enter the value of upper
limit: 3
Enter the number of strips
n: 6
Area under the
curve Enter the
equation:
2*x-(x*x)
the value of integration is :-0.125000
Slover:
I=quad('2.*x-
(x.^2)',0,3) I =
2.2204e-1
Simpsons 3/8 Rule:
Code:
function simpsons_3_8th
() clc;
X0 = input('Enter the Value of x0
='); Xn = input('Enter the Value of
xn ='); n = input('Enter the Value
of n ='); fprintf('Area under\n')
fn = input('Enter Function :
','s'); f = inline(fn);
h=(Xn-X0)/n;
sum = 0;
for i = 0:2: n-2
sum = sum+f(X0)+2*f(X0+3*h)
+3*f(X0+2*h); X0=X0+2*h;
end
Integral = 3*(sum*h)/8;
fprintf('The Value of Integration is: %f\n',
Integral); end
Output:
0
Enter the Value of xn
= 3
Enter the Value of n
= 3
Area under
Enter Function
: 2+(0.5*x*x)
The Value of Integration is: 10.125000
Solver:
I=quad('2+(0.5.*x.^2)',0,
3) I =
10.5000
Simpsons 1/3 Rule:
Code:
clea
r
clc
f= @(t) 4*t.^2 + 2*t + 5;
x0=input("Enter the lower limit:");
xn=input("Enter the upper limit:");
n=input("Enter the number of
iterations:");
%step-size
calculation h=(xn-
x0)/n;
sum = 0;
%sum of odd y
terms for i= 1: 2:
n-1
sum = sum + 4*f(x0 + i*h);
end
%sum of even y
terms for i= 2: 2:
n-1
sum = sum + 2*f(x0 + i*h);
end
I = h/3* (f(x0) + f(xn) + sum);
fprintf("Approximate integral is: %.6f\
n",I);
Output:
0
Enter the upper limit:
6
Enter the number of
iterations: 6
Approximate integral is: 354.000000
Solver:
>> I=quad('4.*t.^2 + 2.*t +
5',0,6) I =
354.0000
Bisection (Iteration):
Code:
% program for Bisection Method based on
Iteration clc;
f=inline('4*(3.141)*x-1000/x^2');
x1=input('enter value of x1=');
x2=input('enter value of x2=');
n=input('enter number of
iterations:'); y1=f(x1);
y2=f(x2);
while
y1*y2>0
x1=input('Enter the value of
x1='); x2=input('Enter the value
of x2='); y1=f(x1);
y=f(x2);
end
for i=1:n
x3=(x1+x2)/2;
y3=f(x3);
if y1*y3<0
x2=x3
;
y2=y3
;
els
e x1=x3
;
y1=y3
en end ;
d
fprintf('The root of the equation is =%f',x3);
Output:
enter value of
x1= 2
enter value of
x2= 6
enter number of
iterations: 11
The root of the equation is =4.302734
Solver:
x=fzero('4*(3.141)*x-1000/
x^2',2) x =
4.3015
Bisection (Accuracy):
Code:
%clc
%clearing screen
% Setting x as symbolic
variable syms x;
f = input('enter an equation:
'); x1 = input('enter value of
x1: '); x2 = input('enter value
of x2: ');
acc = input('enter value of accuracy:
'); y1 = eval(subs(f,x,x1));
y2 = eval(subs(f,x,x2));
while(y1*y2>0)
x1 = input('enter value of
x1:'); x2 = input('enter value
of x2:'); y1 =
eval(subs(f,x,x1));
y2 = eval(subs(f,x,x2));
end
fprintf('\n\nx1\t\ty1\t\tx2\t\ty2\t\tx3\t\ty3\
n'); while(abs(x2-x1)>acc)
x3 = (x2+x1)/2;
y3 = eval(subs(f,x,x3)); fprintf('%f\t%f\t%f\t%f\t
%f\t%f\n' ,x1,y1,x2,y2,x3,y3); if y1*y3<0
x2=x3;
y2=eval(subs(f,x,x2));
else
x1=x3
;
y1=y3
;
end
end
fprintf('the root of the equation is =%f',x3);
Output:
enter an equation:
x^3 - x - 2
enter value of
x1: 1
enter value of
x2:
2
enter value of accuracy:
0.01
x1 y1 x2 y2 x3 y3
1.00000 -2.000000 2.000000 4.000000 1.500000 -
0 0.125000
1.50000 -0.125000 2.000000 4.000000 1.750000 1.609375
0
1.50000 -0.125000 1.750000 1.609375 1.625000 0.666016
0
1.50000 -0.125000 1.625000 0.666016 1.562500 0.252197
0
1.50000 -0.125000 1.562500 0.252197 1.531250 0.059113
0
1.50000 -0.125000 1.531250 0.059113 1.515625 -
0 0.034054
1.51562 -0.034054 1.531250 0.059113 1.523438 0.012250
5 of the is =1.523438
the equation
root
Solver:
x=fzero('(x*x*x-x-
2)',1) x =
1.5214
Guass Quadrature 2 point:
Code:
function Gauss_lrgendres_2point()
%clc
a = input("Enter the first value:\n");
b = input("Enter the value second value:\n");
c = (b-a)/2;
d = (a+b)/2;
fn = input("Enter the funtion to be solved:\
n",'s'); f = inline(fn);
x1= c*(1/sqrt(3)) +d;
x2 = -c*(1/sqrt(3))
+d;
fprintf("Values of c and d %f",c,d);
fprintf("Values of x1 and x2 %f",x1,x2);
fprintf("Values for y1 and y2
%f",f(x1),f(x2)); Area = (f(x1)+f(x2))*c;
fprintf("Area of function:\n
%f",Area); end
Output:
Enter the first value:
0
Enter the value second value:
2
Enter the funtion to be solved:
3*x^3 - 5*x^2 + 2*x + 4
Values of c and d 1.000000Values of c and d
1.000000 Value of x1 1.577350
Value of x2 0.422650
Value for y1 6.488034
Value for y2 4.178633Area of
function: 10.666667
Solver:
f=inline('3*x^3-5*x^2+2*x+4');
quadv(f,0,2)
ans =
10.6667
Gauss Quadrature 3 point:
Code:
function Gauss_lrgendres_3point()
%clc
a = input("Enter the first value:\n");
b = input("Enter the value second value:\n");
c = (b-a)/2;
d = (a+b)/2;
fn = input("Enter the funtion to be solved:\
n",'s'); x1= c*(sqrt(3/5)) +d;
x2 = -c*(1/sqrt(3/5))
+d; x3 = d;
f = inline(fn);
fprintf("Values of c and d %f\
n",c,d); fprintf("\nValues of x1 %f\
n",x1); fprintf("\nValues of x2 %f\
n",x2); fprintf("\nValues of x3 %f\
n",x3); fprintf("\nValues for y1%f\
n",f(x1)); fprintf("\nValues for
y2%f\n",f(x2)); fprintf("\nValues
for y3%f\n",f(x3));
Area = (((5/9)*f(x1))+((8/9)*f(x2))+((5/9)*f(x3))); fprintf("\nArea
%f\n",Area);
end
Output:
Enter the first
value: 0
Enter the value second
value: 2
Enter the funtion to be
solved: 2*x^4 - 3*x^3 + x^2 +
5
Values of c and d
1.000000 Values of c and
d 1.000000
Values of x1 1.774597
Values of x2 -
0.290994 Values of x3
1.000000 Values for
y111.218387 Values
for y25.172941 Values
for y35.000000
Area13.608384
Solver :
f=inline('2*x^4-3*x^3+x^2+5');
quadv(f,0,2)
ans =
13.4667
Lagranges Interpolation:
Code:
function lagranges_inter()
%clc
x = input("Enter the intervals:");
y = input("\nEnter the values corresponding to the interval:");
xg = input("\nEnter the interval for which you have to find the
value:"); n = size(x,2);
sum = 0;
for
i=1:1:n
p = 1;
for j=1:1:n
if i~=j
p = p*(xg-x(j))/(x(i)-x(j));
end
end
sum = sum+p*y(i);
end
fprintf("\n Y=
%f",sum); end
Output:
Enter the intervals:
[0 4 8 12]
Enter the values corresponding to the
interval: [0 8 24 48]
Enter the interval for which you have to find the
value: 6
Y=15.000000
Solver:
>> a=[0 4 8 12];
>> b=[0 8 24 48];
>>
interp1(a,b,6)
ans =
16
Straight Line Equation:
Code:
function straight_line()
clc;
x = input('Enter the values of x:');
y = input('\n Enter the values of
y:'); n = size(x,2);
sx=0;
sy=0;
sxy=0
;
sx2=0
;
for i=1:1:n
sx=sx+x(i);
sy=sy+y(i);
sx2=sx2+x(i)*x(i);
sxy=sxy+x(i)*y(i);
end
D = (sx*sx-n*sx2);
a = (sy*sx-n*sxy)/D;
b = (sx*sxy-sy*sx2)/D;
fprintf('/nValues of a and b : .2%f, %.2f',a,b);
fprintf('/nValue of y : %.2f',D);
Output:
[100 150 200 250 300]
Enter the values of y:
[201.2 202.4 203.1 204.3 205.5]
/n Values of a and b : .20.021000 199.10/n Value of y : -125000.00
Solver:
>> x=[20 25 30 35 40];
>> y=[290 300 310 320 330];
>>
polyfit(x,y,5)
ans =
0.0000 -0.0018 0.1057 -3.0804 46.2262
Double Integration
Code:
%Program for double integration
x0=input('\nEnter the Lower Limit of X:
'); xn=input('\nEnter the Upper Limit of
X: '); y0=input('\nEnter the Lower Limit
of Y: '); ym=input('\nEnter the Upper
Limit of Y: '); n=input('\nHeight of
Strips on X-Axis: '); m=input('\nHeight
of Strips on Y-Axis: ');
f=inline('x+y');
h=((xn-x0)/n);
k=((ym-y0)/m);
x=linspace(x0,xn,n+1)
;
y=linspace(y0,ym,m+1)
; F=zeros(n+1, m+1);
for i=1:n+1
for j=1:m+1
F(i,j)=f(x(i),y(j));
end
end
I=0;
for i=1:n+1
Output:
Enter the Lower Limit of
X: 0
Enter the Upper Limit of
X: 1
Enter the Lower Limit of
Y: 0
Enter the Upper Limit of
Y: 2
Height of Strips on X-
Axis: 2
Height of Strips on Y-
Axis: 2
Double Integradted Value is 3.750000
Solver:
>> f=inline('x.*x+y.*y');
dblquad(f,0,1,0,2)
ans =
3.3333
Parabolic Equation(Least Square Method):
Code:
function
parabolic_equation() clc;
x = input('Enter the values of x:');
y = input('\n Enter the values of
y:'); n = size(x,2);
sx=0;
sy=0;
sxy=0;
sx2=0;
sx2y=0;
sx3=0;
sx4=0;
for i=1:1:n
sx=sx+x(i);
sy=sy+y(i);
sx2=sx2+x(i)*x(i);
sxy=sxy+x(i)*y(i);
sx3=sx3+x(i)*x(i)*x(i
);
sx4=sx4+x(i)*x(i)*x(i)*x(i);
sx2y=sx2y+x(i)*x(i)*y(i);
end
D = (sx*sx-n*sx2);
a = (sy*sx-n*sxy)/D;
b = (sx*sxy-sy*sx2)/D;
c = (sx*sx2y-sxy*sx3)/D;
fprintf('/n Values of a and b : y=%.2fx^2+%.2fx+
%.2f',a,b,c); end
Output:
>> [1 2 3 4 5]
Enter the values of y:
[2.4 5.5 10.8 17.2 26.0]
/n Values of a and b : y=5.89x^2+-
5.29x+786.66
Solver:
>> x=[1 2 3 4 5];
>> y=[2.4 5.5 10.8 17.2 26.0];
>>
polyfit(x,y,5)
ans =
0.0263 -0.2951 1.0554 -0.2261 0 1.8394
Runge-Kutta 2nd Order Method(ODE):
Code:
%program for RK2
method clc;
x0=input('\n Enter the value of x, x0:');
y0=input('\n Enter the value of y, y0:');
h=input('\n Enter the step size (h):');
n=input('\n Enter the number of iterations
(n):'); fn=input('\n Enter the given ODE
function:','s'); f=inline(fn);
fprintf('x0,y0');
for i=1:1:n
x1=x0+h;
k1=h*f(x0);
k2=h*f(x0+h);
y1=y0+(k1+k2)/2
; x0=x1;
y0=y1;
end
fprintf('\n The value of x and y, %f %f',x0,y0);
Output:
0
Enter the value of y, y0:
10
Enter the step size (h):
1
Enter the number of iterations
(n): 2
Enter the given ODE function:
(3*x^2 + 2*x +1)
x0,y0
The value of x and y, 2.000000 25.000000
Newton’s Forward Difference Interpolation:
Code:
clc;
n = input('\nEnter number of data points:');
xg = input('\nEnter X for which value of Y is
calculated'); for i=1:n
fprintf('\nX%d =
%f',i); x(i) = input('
'); fprintf('\nX%d =
%f',i); y(i,1) =
input(' ');
end
h = x(2)-x(1);
u = (xg-
x(1))/h; for
j=2:n
for i=1:n-j+1
y(i,j)=y(i+1,j-1)-y(i,j-1);
end
end
yg=y(1);
u1 = 1;
for j=1:n-1
u1=u1*(u-j+1)/j;
yg = yg+u1*y(1,j+1);
end
fprintf("\n Value of Yg=%f at Xg=%f",yg,xg);
Output:
4
Enter X for which value of Y is
calculated 2.5
X1 =
1
Y1 =
10
X2 =
2
Y2 =
18
X3 =
3
Y3 =
26
X4 =
4
Y4 =
38
Value of Yg=21.750000 at Xg=2.500000
Solver:
>> x=[1 2 3 4];
>> y=[10 18 26 38];
>>
interp1(x,y,2.5)
ans =
22
Euler’s Method(ODE):
Code:
clc;
x0=input('\n Enter the value of x, x0:');
y0=input('\n Enter the value of y, y0:');
h=input('\n Enter the step size (h):');
n=input('\nEnter the number of iterations
(n):'); fn=input('\n Enter the given ODE
function:','s'); f=inline(fn);
fprintf('x0,y0');
for i=1:1:n
x1=x0+h; y1=y0+
(h*f(y0));
x0=x1;
y0=y1;
end
fprintf('\n The value of y at x = 1 is : .2%f, %f %f',x0,y0);
Output:
0
Enter the value of y, y0:
100
Enter the step size (h):
0.2
Enter the number of iterations
(n): 5
Enter the given ODE function:
-
3*(y)
x0,y0
The value of y at x = 1 is : .21.000000, 1.024000
Power Equation(Curve fitting):
Code:
function power_eqn()
xx = input('\n Enter the values of x :
'); yy = input('\n Enter the values of
y : ');
n = size(xx,
2); x =
log(xx);
y = log(yy);
sX = 0;
sY = 0;
sX2 = 0;
sXY = 0;
for i = 1:n
sX = sX +
x(i); sY = sY
+ y(i);
sX2 = sX2 + x(i)^2;
sXY = sXY + y(i) * x(i);
end
D = (sX * sX - n * sX2);
a = (sY * sX - n * sXY) / D;
b = (sX * sY - sXY * n) / D;
a1 =
exp(b); b1
= a;
fprintf('\n Equation is Y = %fx^%f\n', a1, b1);
end
Output:
>> power_eqn
Enter the values of x :
[5 10 15 20 25]
Enter the values of y :
[20 80 180 320 500]
Equation is Y = 7.389056x^2.000000
Solver:
>> x=[5 10 15 20 25];
>> y=[20 80 180 320 500];
>>
polyfit(log(x),log(y),5)
ans =
-0.0100 0.1282 -0.6500 1.6253 0 0.7433
>>
b=exp(ans(6))
b =
2.1029
a=exp(ans(4)
) a =
5.0799
Thomas Algorithm(Tri-diagonal Matrix Algorithm) Method
Code:
a=input('Enter matrix
a:'); b=input('Enter
matrix b:');
n=length(b);
for i=2:n
a(i,i)=a(i,i)-a(i,i-1)/a(i-1,i-1)*a(i-1,i);
b(i)=b(i)-a(i,i-1)/a(i-1,i-1)*b(i-
1); end
x=zeros(n,1)
; for i=n:-
1:1
x(i)=(b(i)-a(i,i+1:n)*x(i+1:n))/a(i,i);
fprintf('\n the value of x(%d)=
%f',i,x(i)); end
Output:
>> Thomas
Enter matrix a:
[2,-1,0;-1,2,-1;0,-1,2]
Enter matrix b:
[100;0;200]
the value of
x(3)=175.000000 the value
of x(2)=150.000000 the
value of x(1)=125.000000
Solver:
>> B= [100; 0; 200];
>> A=[2 , -1 , 0;-1 , 2 , -1;0 , -1 , 2];
>>
sol=linsolve(A,B)
sol =
125.0000
150.0000
175.0000
Exponential Equation(Curve fitting):
Code:
clc;
x=input("Enter the value of
cc"); y=input("Enter the value
of yy"); n=size(x,2);
xx=log(x)
;
yy=log(y)
; sxx=0;
syy=0;
sxxyy=0;
sxx2=0;
for i=1:1:n
sxx=sxx+xx(i);
syy=syy+yy(i);
sxxyy=sxxyy+xx(i)*y(i)
;
sxx2=sxx2+xx(i)*xx(i);
end
D=(sxx*sxx)-(sxx2*n);
A=((syy*sxx)-
(n*sxxyy))/D;
B=((sxx*sxxyy)-(syy-sxx2))/D;
b=A;
a=log(A);
fprintf("The equation is y=%f.e^%fx",a,b);
Output:
[1 2 3 4]
Enter the value of yy
[1200 1500 2000 2800]
The equation is y=8.786265.e^6543.747963x
Solver:
>> x=[1 2 3 4 ];
>> y=[1200 1500 2000 2800];
>>
polyfit(x,log(y),4)
ans =
0.0019 -0.0221 0.1162 0 6.9940
>>
a=exp(ans(5))
a =
1.0901e+03
b=
exp(ans(1))
b =
1.0020
Runge-Kutta 4th Order Method(ODE):
Code:
clc;
f=input('\n Input the function (dy/dx): ');
x0 = input("\n Enter the initial value of
x0: "); y0 = input("\n Enter the initial
value of y0: "); h = input("\n Enter the
step size h: ");
xg = input("\n Enter the given value of xg:
"); n = (xg-x0)/h;
fprintf('Iteration x k1 k2 k3 k4 k
yg'); for i = 1:n
k1 = h*f(x0,y0);
k2 = h*f(x0+(h/2),y0+
(k1/2)); k3 =
h*f(x0+(h/2),y0+(k2/2));
k4 = h*f(x0+h,y0+k3);
k =
(k1+2*k2+2*k3+k4)/6;
yg=y0+k;
x0=x0+h;
y0=yg;
fprintf("\n%d %.4f \t%.4f \t%.4f \t%.4f \t%.4f \t%.4f \t%.4f
",i,k1,k2,k3,k4,k,yg);
end
Output:
@(x,y)-0.1*(y-25)
Enter the initial value of
x0: 0
Enter the initial value of
y0: 90
Enter the step size
h: 1
Enter the given value of
xg: 5
Iteration x k k2 k3 k4 k yg
1
1 -6.5000 -6.1750 -6.1913 -5.8809 -6.1856 83.8144
2 -5.8814 -5.5874 -5.6021 -5.3212 -5.5969 78.2175
3 -5.3218 -5.0557 -5.0690 -4.8149 -5.0643 73.1532
4 -4.8153 -4.5746 -4.5866 -4.3567 -4.5824 68.5708
5 -4.3571 -4.1392 -4.1501 -3.9421 -4.1463 64.4245