0% found this document useful (1 vote)
72 views25 pages

NMO Programs

The document contains programs for numerical integration and solving equations using various numerical methods like bisection, Newton Raphson, Gauss elimination, Gauss Seidel, and numerical integration techniques like trapezoidal rule, Simpson's 1/3 rule, and Simpson's 3/8 rule. The programs take user input, perform the respective calculations, and output the result. Sample outputs and solutions using in-built functions are also provided for verification purposes.

Uploaded by

Rushabh Meher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
72 views25 pages

NMO Programs

The document contains programs for numerical integration and solving equations using various numerical methods like bisection, Newton Raphson, Gauss elimination, Gauss Seidel, and numerical integration techniques like trapezoidal rule, Simpson's 1/3 rule, and Simpson's 3/8 rule. The programs take user input, perform the respective calculations, and output the result. Sample outputs and solutions using in-built functions are also provided for verification purposes.

Uploaded by

Rushabh Meher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Program on Bisection Method

Part A
function y=f(x)
y=cos(x)-1.3*x;

Part B
x1=input('Enter the value of first initial guess x1 = ');
x2=input('Enter the value of second initial guess x2 = ');
n=input('Enter the number of iterations n = ');
y1=f(x1);
y2=f(x2);
while(y1*y2>0)
x1=input('Enter the new value of first initial guess
x1 = ');
x2=input('Enter the new value of second initial guess
x2 = ');
y1=f(x1);
y2=f(x2);
end
for i=1:1:n
x3=(x1+x2)/2;
y3=f(x3);
if (y1*y3)>0
x1=x3;
y1=y3;
else
x2=x3;
y2=y3;
end
end
fprintf('The root of equation is x3 = %f',x3);

Output:-
Enter the value of first initial guess x1 = 0
Enter the value of second initial guess x2 = 1
Enter the number of iterations n = 6
The root of equation is x3 = 0.609375

Solver:-
fzero('cos(x)-1.3*x',1)
ans =
0.6071

Program on Newton Raphson Method

Part A
Function:- f.m function y=f(x)
y=x-cos(x);
df.m function y=df(x)
y=1+sin(x);
ddf.m function y=ddf(x)
y=cos(x);

Part B
x1=input('Enter the value of initial guess x1 = ');
n=input('Enter the number of iterations n = ');
y1=f(x1);
y2=df(x1);
y3=ddf(x1);
a=y1*y3/y2^2;
while(abs(a)>1)
x1=input('Enter the value of initial guess x1 =
');
y1=f(x1);
y2=df(x1);
y3=ddf(x1);
a=y1*y3/y2^2;

end
for i=1:1:n
x2=x1-y1/y2;
x1=x2;
y1=f(x1);
y2=df(x1);

end
fprintf('The root of equation is x2 = %f',x2);

Output:-
Enter the value of initial guess x1 = 0
Enter the number of iterations n = 10
The root of equation is x2 = 0.739085
Solver:-
fzero('x-cos(x)',0)

ans =

0.7391
Program on Successive Approximation Method

Part A
Function:-
g.m function y=g(x)
y=(x^3+8)/15;

dg.m function y=dg(x)


y=(x^2)/5;

Program :-
x1=input('Enter the value of initial guess x1 = ');
n=input('Enter the number of iterations n = ');
a=dg(x1);
while(abs(a)>1)
x1=input('Enter the new value of initial guess x1
= ');
a=dg(x1);

end
for i=1:1:n
x2=g(x1);
x1=x2;

end
fprintf('The root of equation is x2 = %f',x2);

Output:-
Enter the value of initial guess x1 = 0
Enter the number of iterations n = 5
The root of equation is x2 = 0.544070
Solver:-
fzero('x^3-15*x+8',0)

ans =

0.5441

Program on Gauss Elimination Method

Program :-
a=input('Enter the matrix a = ');
d=input('Enter the matrix d = ');
n=length(d);
for i=1:1:n
for k=i+1:1:n
f=a(k,i)/a(i,i);
for j=1:1:n
a(k,j)=a(k,j)-f*a(i,j);
end
d(k)=d(k)-f*d(i);
end
end
for i=n:-1:1
temp=d(i);
for j=i+1:1:n
temp=temp-a(i,j)*x(j);
end
x(i)=temp/a(i,i);
end
fprintf(' x = %f\n',x);

Output:-
Enter the matrix a = [12,-2,3;1,10,-2;3,1,15]
Enter the matrix d = [18;16;52]
x = 1.074761
x = 2.114671
x = 3.110736
Solver:-
[x1,x2,x3]=solve('12*x1-2*x2+3*x3=18','x1+10*x2-
2*x3=16','3*x1+x2+15*x3=52')

x1 = 1912/1779

x2 = 1254/593

x3 =5534/1779

Program on Thomas Algorithm Method

Program :-
a=input('Enter the matrix a = ');
d=input('Enter the matrix d = ');
n=length(d);
for i=2:1:n
f=(a(i,i-1)/a(i-1,i-1));
a(i,i)=a(i,i)-f*a(i-1,i);
d(i)=d(i)-f*d(i-1);
end
for i=n:-1:1
temp=d(i);
for j=i+1:1:n
temp=temp-a(i,j)*x(j);
end
x(i)=temp/a(i,i);
end
fprintf(' x = %f\n',x);

Output:-
Enter the matrix a = [1,2,0,0;-1,1,2,0;0,1,3,1;0,0,2,2]
Enter the matrix d = [4;1;7;8]
x = 2.000000
x = 1.000000
x = 1.000000
x = 3.000000

Solver:-
[x1,x2,x3,x4]=solve('x1+2*x2+0*x3+0*x4=4',
'-
x1+x2+2*x3+0*x4=1','0*x1+x2+3*x3+x4=7','0*x1+0*x2+2*x3+2*x4
=8')

x1 = 2

x2 = 1

x3 = 1

x4 = 3

Program on Gauss Seidal Method

Program :-
fx=inline ('(5-x2-x3)/4');
fy=inline ('(19-x1-2*x3)/6');
fz=inline ('(-10-x1-2*x2)/5');
n=input('\n Enter the required number of iterations n= ');
x1=0;
x2=0;
x3=0;
for i=1:1:n
x1=fx(x2,x3);
x2=fy(x1,x3);
x3=fz(x1,x2);
end
fprintf('\n x1=%f',x1);
fprintf('\n x2=%f',x2);
fprintf('\n x3=%f',x3);
Output:-
Enter the required number of iterations n= 6

x1=1.164944
x2=4.288708
x3=-3.948472

Solver:-
[x1,x2,x3]=solve('4*x1+x2+x3=5','x1+6*x2+2*x3=19','-x1-
2*x2-5*x3=10')

x1 = 113/97

x2 = 416/97

x3 = -383/97

Program to fit a straight line (y=ax+b)

Program :-
x=input(' Enter the values of x = ');
y=input(' Enter the values of y = ');
n=length(x);
sumx=0;sumy=0;sumxy=0;sumxx=0;
for i=1:1:n
sumx=sumx+x(i);
sumy=sumy+y(i);
sumxy=sumxy+x(i)*y(i);
sumxx=sumxx+x(i)*x(i);
end
ds=[sumx,n;sumxx,sumx];
da=[sumy,n;sumxy,sumx];
db=[sumx,sumy;sumxx,sumxy];
a=det(da)/det(ds);
b=det(db)/det(ds);
fprintf('\n y=%f * x + %f ', a,b);

Output:-
Enter the values of x = [50;70;100;120]
Enter the values of y = [12;15;21;25]

y=0.187931 * x + 2.275862

Solver:-
polyfit(x,y,1)

ans =

0.1879 2.2759

Program to fit a Power Equation (y=ab^x)

Program :-
x=input(' Enter the values of x = ');
y=input(' Enter the values of y = ');
n=length(x);
sumX=0;sumY=0;sumXY=0;sumXX=0;
for i=1:1:n
Y(i)=log(y(i));
X(i)=x(i);
sumX=sumX+X(i);
sumY=sumY+Y(i);
sumXY=sumXY+X(i)*Y(i);
sumXX=sumXX+X(i)*X(i);
end
ds=[sumX,n;sumXX,sumX];
dA=[sumY,n;sumXY,sumX];
dB=[sumX,sumY;sumXX,sumXY];
A=det(dA)/det(ds);
B=det(dB)/det(ds);
b=exp(A);
a=exp(B);
fprintf('\n y=(%f) * (%f) ^x’, a,b);

Output:-
Enter the values of x = [2;3;4;5;6]
Enter the values of y = [144;172.8;207.4;248.8;298.5]

y=100.026209 * 1.199905 ^x

Solver:-
polyfit(X,Y,1)

ans = 0.1822 4.6054

a=exp(B)
a = 100.0262
b=exp(A)
b = 1.1999

Program to fit an Exponential Equation (y=ae^(bx)))

Program :-
x=input(' Enter the values of x = ');
y=input(' Enter the values of y = ');
n=length(x);
sumX=0;sumY=0;sumXY=0;sumXX=0;
for i=1:1:n
Y(i)=log(y(i));
X(i)=x(i);
sumX=sumX+X(i);
sumY=sumY+Y(i);
sumXY=sumXY+X(i)*Y(i);
sumXX=sumXX+X(i)*X(i);
end
ds=[sumX,n;sumXX,sumX];
dA=[sumY,n;sumXY,sumX];
dB=[sumX,sumY;sumXX,sumXY];
A=det(dA)/det(ds);
B=det(dB)/det(ds);
b=A;
a=exp(B);
fprintf('\n y = %f * e ^ ( %f * x)’, a,b);

Output:-
Enter the values of x = [0;1;2;3]
Enter the values of y = [1.05;2.10;3.85;8.30]

y = 1.043400 * e^ (0.680853*x)

Solver:-

polyfit(X,Y,1)

ans = 0.6809 0.0425

b=A=0.6809
a=exp(B);
a = 1.0434
Program to fit an Quadratic Equation (y=ax^2 + bx + c)

Program :-
x=input (' Enter the values of x ') ;
y= input (' Enter the values of y') ;
n= length (x);
sumx=0; sumy=0; sumxx=0;sumxy=0; sumxxx=0; sumxxy =0;
sumxxxx=0;
for i=1:1:n
sumx=sumx+x(i);
sumy=sumy+y(i);
sumxx=sumxy+x(i)*x(i);
sumxy=sumxy +x(i)*y(i);
sumxxx=sumxxx + x(i)*x(i)*x(i);
sumxxy=sumxxy +x(i)* x(i)*y(i);
sumxxxx=sumxxxx+x(i)+x(i)*x(i)*x(i);
end
p= [ sumxx, sumx,n;sumxxx,sumxx,sumx;sumxxxx,sumxxx,sumxx ]
;
q=[ sumy;sumxy ;sumxxy ];
v=linsolve (p,q) ;
a=v(1);
b=v(2);
c=v(3);
fprintf ('\n y =%f * x^2 +%f * x + %f ', a , b,c );

Output:-
Enter the values of x = [1;1.5;2;2.5;3;3.5;4]
Enter the values of y = [1.1;1.3;1.6;2;2.7;3.4;4.1]

y = 0.242857 * x^2 +-0.192857* x +1. 035714

Solver:-

polyfit(x,y,2))

ans =

0.2429 -0.1929 1.0357

Program for Trapezoidal Rule

Function:-
function y=f(x)
y=1/(1+x^2);

Program:-
x0=input('Enter the value of x0 = ');
xn=input('Enter the value of xn = ');
n=input('Enter the number of strips = ');
h=(xn-x0)/n;
y0=f(x0);
yn=f(xn);
yt=0;
for i=1:1:n-1
yt=yt+f(x0+i*h);
end
I=h/2*(y0+yn+2*yt);
fprintf('\n I = %f',I);

Output:-
Enter the value of x0 = 0
Enter the value of xn = 6
Enter the number of strips = 4

I = 1.452397

Solver:-
f=@(x)(1/(1+x^2));
I=quadv(f,0,6)

I =

1.4056

Program for Simpson’s 1/3 Rule


Function:-
function y=f(x)
y=(exp(x)-x^3-2*x+1);

Program:-
x0=input('Enter the value of x0 = ');
xn=input('Enter the value of xn = ');
n=input('Enter the number of strips = ');
h=(xn-x0)/n;
y0=f(x0);
yn=f(xn);
ys1=0;
ya=0;
for i=2:2:n-1
ys1=ys1+f(x0+i*h);
end
for j=1:1:n-1
ya=ya+f(x0+j*h);
end
ys2=ya-ys1;
I = h/3 * (y0+yn+4*ys2+2*ys1);
fprintf('I = %f',I);
Output:-

Enter the value of x0 = 1


Enter the value of xn = 4
Enter the number of strips = 6
I = -23.852640

Solver:-
f=@(x)(exp(x)-x^3-2*x+1);
quadv(f,1,4)

ans =

-23.8701
Program for Simpson’s 3/8 Rule
Function:-
function y=f(x)
y=4+2*sin(x);

Program:-
x0=input('Enter the value of x0 = ');
xn=input('Enter the value of xn = ');
n=input('Enter the number of strips = ');
h=(xn-x0)/n;
y0=f(x0);
yn=f(xn);
ys1=0;
ya=0;
for i=3:3:n-1
ys1=ys1+f(x0+i*h);
end
for j=1:1:n-1
ya=ya+f(x0+j*h);
end
ys2=ya-ys1;
I=(3*h)/8*(y0+yn+3*ys2+2*ys1);
fprintf('I = %f', I);

Output:-

Enter the value of x0 = 0


Enter the value of xn = 3.142
Enter the number of strips = 6
I = 16.572022

Solver:-
f=@(x)(4+2*sin(x));

I=quadv(f,0,3.142)

I =
16.5680

Program for Gauss 2 Point Method


Function:-
function y=f(x)
y=x^3-6*x+13;

Program:-
x0=input('Enter the value of x0 = ');
xn=input('Enter the value of xn = ');
c=(xn-x0)/2;
d=(xn+x0)/2;
x1=(c/sqrt(3)+d);
x2=(-c)/sqrt(3)+d;
y1=f(x1);
y2=f(x2);
I=(y1+y2)*c;
fprintf('I = %f',I);

Output:-

Enter the value of x0 = -1


Enter the value of xn = 1
I = 26.000000

Solver:-
f=@(x)(x^3-6*x+13);

I=quadv(f,-1,1))

I =
26

Program for Gauss 3 Point Method


Function:-
function y=f(x)
y=exp(x)*cos(x)-2*x;

Program:-
x0=input('Enter the value of x0 = ');
xn=input('Enter the value of xn = ');
c=(xn-x0)/2;
d=(xn+x0)/2;
x1=(c*sqrt(3/5)+d);
x2=(-c*sqrt(3/5)+d);
x3=d;
y1=f(x1);
y2=f(x2);
y3=f(x3);
I=((5/9)*(y1+y2)+((8/9)*(y3))*c);
fprintf('I=%f',I);

Output:-

Enter the value of x0 = 0


Enter the value of xn = 1
I=0.378021

Solver:-
f=@(x)(exp(x)*cos(x)-2*x);

I=quadv(f,-0,1))
I =

0.3780

Program for Lagrange’s Interpolation Method

Program :-
x= input (' Enter the values of x');
y=input ('Enter the values of y');
n=length(x);
xg=input ('enter the value of xg : ');
yg=0;
for i=1:1:n
nu=1;
de=1;
for j=1:1:n
if (i~=j)
nu= nu*(xg -x (j));
de=de*(x (i) -x (j));
end
end
l(i)=nu/de;
yg=yg+l(i)*y(i);
end

fprintf (' The final value of yg = % f ', yg );

Output:-
Enter the values of x [0;1;2;5]
Enter the values of y [2;3;12;147]
enter the value of xg : 1.5
The final value of yg = 6.125000

Solver:-

yg=interp1(x,y,xg,'spline')

yg =

6.1250

Program for Inverse Interpolation Method

Program :-
x= input (' Enter the values of x');
y=input ('Enter the values of y');
n=length(x);
yg=input ('enter the value of yg : ');
xg=0;
for i=1:1:n
nu=1;
de=1;
for j=1:1:n
if (i~=j)
nu= nu*(yg -y (j));
de=de*(y(i)-y (j));
end
end
l(i)=nu/de;
xg=xg+l(i)*x(i);
end

fprintf (‘ % f ', xg );
Output:-
Enter the values of x [0;1;2;3]
Enter the values of y [0;1;7;25]
enter the value of yg : 2
The final value of xg = 1.716138

Solver:-

xg=interp1(y,x,yg,'spline')

xg =

1.7161

Program for Newton’s Forward Difference Interpolation Method


Program :-
x=input('Enter the values of x = ');
y=input('Enter the values of y = ');
xg=input('Enter the values of xg = ');
n=length(x);
for j=2:1:n
for i=1:1:n-j+1
y(i,j)=y(i+1,j-1)-y(i,j-1);
end
end
fprintf('\n Table');
for i=1:1:n-1
fprintf('\n');
for j=2:1:(n-i+1)
fprintf('\t %f',y(i,j));
end
end
h=x(2)-x(1);
u=(xg-x(1))/h;
yg=y(1,1)+u*y(1,2);
p=u*(u-1);
k=2;
for j=3:1:n
fact=factorial(k);
yg=yg+p*y(1,j)/fact;
p=p*(u-k);
k=k+1;
end
fprintf('\n yg = %f', yg);
Output:-
Enter the values of x = [0.1;0.3;0.5;0.7;0.9]
Enter the values of y = [0.72;1.81;2.73;3.47;3.98]
Enter the values of xg = 0.2

Table
1.090000 -0.170000 -0.010000 -0.040000
0.920000 -0.180000 -0.050000
0.740000 -0.230000
0.510000
yg = 1.287188

Program for Euler’s Method


Function:-
function y=f(x,y)
y=x^2+y^2;

Program:-
x0=input('Enter the value of x0 = ');
y0=input('Enter the value of y0= ');
xg=input('Enter the value of xg = ');
h=input('Enter tyhe value of step size = ');
n=(xg-x0)/h;
for i=1:1:n
yg=y0+h*f(x0,y0);
x0=x0+h;
y0=yg;
end
fprintf('yg=%f',yg);
Output:-
Enter the value of x0 = 0
Enter the value of y0= 0
Enter the value of xg = 0.5
Enter tyhe value of step size = 0.1
yg=0.030022
solver:
f=inline('x^2+y^2')
f = f(x, y) = x^2+y^2
>> [x,y]=ode45(f,[0:0.1:0.5],0)

X= 0.00000
0.000000
0.000333
0.002667
0.009003

Y=
0.10000
0.20000
0.30000
0.40000
0.50000

Program for Runge Kutta 4 th Order Method


Function:-
function y=f(x,y)
y=x^2+y^2;

Program:-
x0=input('Enter the value of x0 = ');
y0=input('Enter the value of y0= ');
xg=input('Enter the value of xg = ');
h=input('Enter tyhe value of step size = ');
n=(xg-x0)/h;
for i=1: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=(1/6)*(k1+2*k2+2*k3+k4);
yg=y0+k;
x0=x0+h;
y0=yg;
end
fprintf('yg=%f',yg);
Output:
Enter the value of x0 = 1
Enter the value of y0= 1.5
Enter the value of xg = 1.3
Enter tyhe value of step size = 0.1
yg=3.554948

Solver:-
f=inline('x^2+y^2')
f =
Inline function:
f(x,y) = x^2+y^2
[x,y]=ode45(f,[1:0.1:1.3],1.5)
x = y =
1.0000 1.5000
1.1000 1.8955
1.2000 2.5044
1.3000 3.5554

Program for Runge Kutta 2 nd Order Method for Simultaneous


Equations
Function:-
function y=f(x,y)
y=x^2/(2*y);

Program:-
x0=input('Enter the value of x0 = ');
y0=input('Enter the value of y0= ');
xg=input('Enter the value of xg = ');
h=input('Enter tyhe value of step size = ');
n=(xg-x0)/h;
for i=1:1:n
k1=h*f(x0,y0);
k2=h*f(x0+h,y0+k1);
k=(k1+k2)/2;
yg=y0+k;
x0=x0+h;
y0=yg;
end
fprintf('yg=%f',yg);
Output:
Enter the value of x0 = 0
Enter the value of y0= 1.2
Enter the value of xg = 0.4
Enter the value of step size = 0.4
yg=1.213333

Solver:-
f=inline('x^2/2*y')
f =
Inline function:
f(x,y) = x^2/2*y

[x,y]=ode23(f,[0:0.4:0.4],1.2)
x = y =
0.0000 1.2000
0.0400 1.2000
0.0800 1.2001
0.1200 1.2002
0.1600 1.2006
0.2000 1.2011
0.2400 2.2019
0.2800 2.2030
0.3200 2.2045
0.3600 2.2065
0.4000 2.2089

Program for Laplace Equations


Program:-
Trhs = input('\n Enter the value of Trhs = ');
Tlhs = input('\n Enter the value of Tlhs = ');
Tupp = input('\n Enter the value of Tupp = ');
Tlow = input('\n Enter the value of Tlow = ');
a=[4,-1,-1,0;-1,4,0,-1;-1,0,4,-1;0,-1,-1,4];
b(1,1)=Tlhs+Tupp;
b(2,1)=Tupp+Trhs;
b(3,1)=Tlhs+Tlow;
b(4,1)=Tlow+Trhs;
v=linsolve(a,b);
T1=v(1);
T2=v(2);
T3=v(3);
T4=v(4);
fprintf('\n T1 = %f, T2 = %f, T3=%f, T4 = %f',T1,T2,T3,T4);

Output:
Enter the value of Trhs = 100

Enter the value of Tlhs = 0

Enter the value of Tupp = 100

Enter the value of Tlow = 0

T1 = 50.000000, T2 = 75.000000, T3=25.000000, T4 =


50.000000

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy