You are on page 1of 66

%bisectionmethod

%NAME=Akshay T Shinde
%DIV=TE(MECH)
%ROLL NO=30
clc;
clear;
z=input('enter the equation=','s');
f=inline(z);
x1=input('enter the value of initial guess x1:REGULA FALSI');
x2=input('enter the value of initial guess x2:');
acc=input('enter the acc');
y1=f(x1);
y2=f(x2);
while(y1*y2>0)
fprintf('guesses are wrong enter the new value');
x1=('enter the value of initial guess x1:');
x2=('enter the value of initial guess x2:');
y1=f(x1);
y2=f(x2);
end
while (abs(x1-x2)>acc)
x3=(x1+x2)/2;
y3=f(x3);
if(y1*y3<0)
x2=x3;
y2=y3;
else
x1=x3;
y1=y3;
end
end
fprintf('the root of equation is x3=%f',x3);
enter the equation=(cos(x)-1.3*x)

enter the value of initial guess x1:0

enter the value of initial guess x2:1

enter the acc0.01

the root of equation is x3=0.617188>>


%BiSectionMethod
%NAME=Akshay T Shinde
%ROLL NO=30
%DIV=TE(MECH)
clc;
clear;
x1=input('Enter the value of initial guess x1:');
x2=input('Enter the value of initial guess x2:');
n=input('Enter the number of iterations n: ');
y1=(-0.9*x1*x1+1.7*x1+2.5);
y2=(-0.9*x2*x2+1.7*x2+2.5);
while(y1*y2>0)
fprintf('Guesses are wrong enter the new value');
x1=input('Enter the value of initial guess x1:');
x2=input('Enter the value of initial guess x2:');
y1=(-0.9*x1*x1+1.7*x1+2.5);
y2=(-0.9*x2*x2+1.7*x2+2.5);
end
for i=1:n
x0=(x1+x2)/2;
y0=(-0.9*x0*x0+1.7*x0+2.5);
if(y1*y0<0)
x2=x0;
y2=y0;
else
x1=x0;
y1=y0;
end
end
fprintf('The root of equation is x0=%f',x0)
Enter the value of initial guess x1:2.8

Enter the value of initial guess x2:3

Enter the number of iterations n: 3

The root of equation is x0=2.875000>>


%RegulaFalsePosition
%NAME=Akshay T Shinde
%ROLL NO=30
%DIV=TE(MECH)
clc;
clear all;
x0=input('enter the value of initial guess x0:');
x1=input('enter the value of initial guess x1:');
n=input('enter the number of iteration n:');
y0=(sin*(x0)-(x0)+2);
y1=(sin*(x1)-(x1)+2);
while(y0*y1>0)
fprintf ('guesses are wrong enter the new value');
x0=('enter the value of initial guess x0:');
x1=('enter the value of initial guess x1:');
y0=(sin*(x0)-(x0)+2);
y1=(sin*(x1)-(x1)+2);
end
for i=1:n
x2=(x1*y0-x0*y1)/(y0-y1);
y2=(sin*(x2)-(x2)+2);
if(y1*y2<0)
x0=x2;
y0=y2;
else
x1=x0;
y1=y0;
end
end
fprintf('the root of equation is x0=%f',x2);
enter the value of initial guess x0:2

enter the value of initial guess x1:3

enter the number of iteration n:5

the root x3=2.554196<<


%NewtonRaphson
%NAME=Akshay T Shinde
%ROLL NO=30
%DIV=TE(MECH)
clc;
clear all;
x1=input('Enter the value of initial guess x1:');
n=input('enter the no. of iterations n:');
y1=(0.51*x1-sin(x1));
y2=(0.51-cos(x1));
y3=(sin(x1));
a=(y1*y3)/(y2*y2);
while(abs(a)>1)
fprintf('The initial guesses are wrong enter new value');
x1=input('Enter the value of initial guess x1:');
y1=(0.51*x1-sin(x1));
y2=(0.51-cos(x1));
y3=(sin(x1));
a=(y1*y3)/(y2*y2);
end
for i=1:n
x2=x1-(y1/y2);
x1=x2;
y1=(0.51*x1-sin(x1));
y2=(0.51-cos(x1));
end
fprintf('the root of equation is x2=%f',x2);
Enter the value of initial guess x1:2

enter the no. of iterations n:3

the root of equation is x2=1.872322>>


%SuccessiveApproximation
%NAME=Akshay T Shinde
%ROLL NO=30
%DIV=TE(MECH)
Clc;
clear all;
x1=input('enter the value of initial guess x1:');
n=input('enter the no of iteration:');
y=(x*x-8*(x)+6);
y1=(x*x-6/8);
while (abs(y1)>1)
fprintf('guesses are wrong enter new value');
x1=input('enter the value of initial guess x1:');
y1=(x*x-6/8);
end
for i=1:n
x2=(x*x-6/8);
x1=x2;
end
fprintf('the root of equation is x2=%f',x2);
enter the value of initial guess x1:0

enter the no of iteration:4

the root x2=0.836968<<


%REGULAFALSI
%NAME=Akshay T Shinde
%DIV=TE(MECH)
%ROLL NO=30
clc;
clear;
z=input('enter the equation=','s');
f=inline(z);
x1=input('enter the value of initial guess x1:');
x2=input('enter the value of initial guess x2:');
n=input('enter the no of iterations:');
y1=f(x1);
y2=f(x2);
while(y1*y2>0)
fprintf ('guesses are wrong enter the new value');
x1=('enter the value of initial guess x1:');
x2=('enter the value of initial guess x2:');
y1=f(x1);
y2=f(x2);
end
for i=1:n
x3=(((x2*y1)-(x1*y2))/(y1-y2));
y3=f(x3);
if (y1*y3<0)
x2=x3;
y2=y3;
else
x1=x3;
y1=y3;
end

end
fprintf('the root of equation is x3=%f',x3);
enter the equation=(exp(x)*cos(x)-1.2*sin(x)-0.5)

enter the value of initial guess x1:0

enter the value of initial guess x2:1

enter the no of iterations:3

the root of equation is x3=0.970941>>


%newtonraphson
%NAME=Akshay T Shinde
%DIV=TE(MECH)
%ROLL NO=30
clc;
clear;
syms x;
n=input('enter the no of iterations=');
x0=input('enter the value of initial guess=');
z=input('enter the equation=','s');
f=inline(z);
dz=diff(z);
df=inline(dz);
dzz=diff(dz);
dff=inline(dzz);
y1=f(x0);
y2=df(x0);
y3=dff(x0);
a=(y1*y3)/(y2*y2);
while (abs(a)>1)
x0=('enter the value of initial guess=');
y1=f(x0);
y2=df(x0);
y3=dff(x0);
a=(y1*y3)/(y2*y2);
end
for i=1:n
x1=x0-(y1/y2);
x0=x1;
end
fprintf('the root x2=%f',x1);
enter the no of iterations=3

enter the value of initial guess=2

enter the equation=0.51*x-sin(x)

the root x2=1.641409>>


%SuccessiveApproximation
%NAME=Akshay T Shinde
%DIV:TE(MECH)
%ROLL NO:40
clc;
clear all;
x1=input('enter the value of initial guess x1:');
n=input('enter the no of iteration:');
y=(x*x-8*(x)+6);
y1=(x*x-6/8);
while (abs(y1)>1)
fprintf('guesses are wrong enter new value');
x1=input('enter the value of initial guess x1:');
y1=(x*x-6/8);
end
for i=1:n
x2=(x*x-6/8);
x1=x2;
end
fprintf('the root of equation is x2=%f',x2);
enter the value of initial guess x1:0

enter the no of iteration:4

the root x2=0.836968<<


%gausselimination
%NAME=Akshay T Shinde
%DIV=TE(MECH)
%ROLL NO=30
clc;
clear;
n=input('Enter the no of variables=');
for(i=1:n)
for(j=1:n)
ar(i,j)=input('Enter the values of elements=');
end
end
for(i=1:n)
ar1(i)=input('Enter the constant terms=');
end
for(q=1:n)
i=q;
d=ar(i,i);
for(j=1:n)
ar(i,j)=(ar(i,j)/d);
end
ar1(i)=ar1(i)/d;
for(i=(q+1):n)
s=ar(i,q);
for(j=1:n)
ar(i,j)=ar(i,j)-s*ar(q,j);
end
ar1(i)=ar1(i)-s*ar1(q);
end
end
fprintf('\n %f');
disp(ar)
disp(ar1)
a(n)=ar1(n);
for(w=(n-1):(-1):1)
a(w)=ar1(w);
for(e=w:n-1)
a(w)=a(w)-(a(e+1)*ar(w,e+1));
end
end
for(j=1:n)
fprintf('\n The Value of x%d=%f',j,a(j));
end
Enter the no of variables=3

Enter the values of elements=2

Enter the values of elements=-3

Enter the values of elements=-4

Enter the values of elements=9

Enter the values of elements=2

Enter the values of elements=-8

Enter the values of elements=15

Enter the values of elements=-8

Enter the values of elements=6

Enter the constant terms=11

Enter the constant terms=1.9

Enter the constant terms=14.7

1.0000 -1.5000 -2.0000

0 1.0000 0.6452

0 0 1.0000

5.5000 -3.0710 -0.8734

The Value of x1=-0.007990

The Value of x2=-2.507506

The Value of x3=-0.873366>>


%gaussseidal
%NAME=Akshay T Shinde
%DIV=TE(MECH)
%ROLL NO=30
clc;
clear;
n=input('Enter no of equations=');
for i=1:n
for j=1:n
ar(i,j)=input('Enter matrix elements row wise=');
end
end
for i=1:n
ar1(i)=input('Enter second matrix element=');
end
n1=input('Enter no of iterations=');
for i=1:n-1
for j=i+1:n
if(abs(ar(i,i))<abs(ar(j,i)));
for k=1:n
temp=ar(i,k);
ar(i,k)=ar(j,k);
ar(j,k)=temp;
temp=ar1(i);
ar1(i)=ar1(j);
ar1(j)=temp;
end
end
end
end
for i=1:n
x(i)=0;
end
for k=1:n1
for i=1:n
const=ar1(i);
for j=1:n
if(i~=j)
const=const-(x(j)*ar(i,j));
end
end
x(i)=const/ar(i,i);
fprintf('\tx%d=%f',i,x(i));
end
fprintf('\n\n');
end
Enter no of equations=3

Enter matrix elements row wise=4

Enter matrix elements row wise=1

Enter matrix elements row wise=1

Enter matrix elements row wise=1

Enter matrix elements row wise=6

Enter matrix elements row wise=2

Enter matrix elements row wise=-1

Enter matrix elements row wise=-2

Enter matrix elements row wise=-5

Enter second matrix element=5

Enter second matrix element=19

Enter second matrix element=10

Enter no of iterations=6

x1=1.250000 x2=2.958333 x3=-3.433333

x1=1.368750 x2=4.082986 x3=-3.906944

x1=1.205990 x2=4.267983 x3=-3.948391

x1=1.170102 x2=4.287780 x3=-3.949132

x1=1.165338 x2=4.288821 x3=-3.948596

x1=1.164944 x2=4.288708 x3=-3.948472

>>
%thomasalgorithm
%NAME=Akshay T Shinde
%DIV=TE(MECH)
%ROLL NO=30
clc;
clear all;
n=input('enter the no of elements=');
for i=1:n
for j=1:n
ar(i,j)=input('enter the matrix elements row wise=');
end
end
for i=1:n
ar1(i)=input('enter the second matrix elements=');
end
for i=2:n
fact=ar(i,i-1)/ar(i-1,i-1);
for j=1:n
ar(i-1,j)=ar(i-1,j)*fact;
end
ar1(i-1)=ar1(i-1)*fact;
for j=1:n
ar(i,j)=ar(i,j)-ar(i-1,j);
end
ar1(i)=ar1(i)-ar1(i-1);
end
for i=1:n
dig=ar(i,i);
for j=1:n
ar(i,j)=ar(i,j)/dig;
end
ar1(i)=ar1(i)/dig;
end
a(n)=ar1(n);
for w=n-1:-1:1
a(w)=ar1(w);
for e=w:n-1
a(w)=a(w)-a(e+1)*ar(w,e+1);
end
end
for j=1:n
fprintf('\n x%d=%f',j,a(j));
end
enter the no of elements=4

enter the matrix elements row wise=1

enter the matrix elements row wise=2

enter the matrix elements row wise=0

enter the matrix elements row wise=0

enter the matrix elements row wise=-1

enter the matrix elements row wise=1

enter the matrix elements row wise=2

enter the matrix elements row wise=0

enter the matrix elements row wise=0

enter the matrix elements row wise=1

enter the matrix elements row wise=3

enter the matrix elements row wise=4

enter the matrix elements row wise=0

enter the matrix elements row wise=0

enter the matrix elements row wise=2

enter the matrix elements row wise=2

enter the second matrix elements=4

enter the second matrix elements=1

enter the second matrix elements=7

enter the second matrix elements=8

x1=9.200000

x2=-2.600000

x3=6.400000

x4=-2.400000>>
%straightlineregression
%NAME=Akshay T Shinde
%DIV=TE(MECH)
%ROLL NO=30
clc;
clear;
n=input('Enter the number of point=');
for i=1:n
x(i)=input('enter the value of x=');
y(i)=input('enter the corresponding value of y=');
end
sx=0;
sy=0;
sxy=0;
sx2=0;
for i=1:n
sx=sx+x(i);
sy=sy+y(i);
sxy=sxy+(x(i)*y(i));
sx2=sx2+(x(i)^2);
end
d=det([sx*sx-n*sx2]);
da=det([sy*sx-n*sxy]);
db=det([sx*sxy-sy*sx2]);
a=da/d;
b=db/d;
fprintf('\nthe value of a is:%f',a);
fprintf('\nthe value of b is:%f',b);
fprintf('\nthe equation of best curve y=%f*x+%f',a,b);
Enter the number of point=7

enter the value of x=1

enter the corresponding value of y=0.5

enter the value of x=2

enter the corresponding value of y=2.5

enter the value of x=3

enter the corresponding value of y=2.0

enter the value of x=4

enter the corresponding value of y=4

enter the value of x=5

enter the corresponding value of y=3.5

enter the value of x=6

enter the corresponding value of y=6

enter the value of x=7

enter the corresponding value of y=5.5

the value of a is:0.839286

the value of b is:0.071429

the equation of best curve y=0.839286*x+0.071429>>


%Power Equation
%NAME=Akshay T Shinde
%DIV=TE(MECH)
%ROLL NO=30

clc;
clear;
n=input('Enter the number of points n=');
for i=1:n
x(i)=input('Enter the value of x=');
y(i)=input('Enter the corresponding value of y=');
Y(i)=log(y(i));
end
sx=0;
sY=0;
sx2=0;
sxY=0;
for i=1:n
sx=sx+x(i);
sY=sY+Y(i);
sx2=sx2+(x(i)*x(i));
sxY=sxY+(x(i)*Y(i));
end
D=det([sx*sx-n*sxY]);
DA=det([sY*sx-n*sx2]);
DB=det([sx*sx2-sY*sxY]);
A=DA/D;
B=DB/D;
a=exp(B);
b=exp(A);
fprintf('\n\na=%f',a);
fprintf('\n\nb=%f',b);
fprintf('\nThe Equation of best fit curve is y=%f(%f^x)',a,b);
Enter the number of points n:4

Enter the value of x:1

Enter the corresponding value of y:4

Enter the value of x:2

Enter the corresponding value of y:11

Enter the value of x:3

Enter the corresponding value of y:35

Enter the value of x:4

Enter the corresponding value of y:100

a=0.469650

b=0.122756

The Equation of best fit curve is y=2.948829(11.326650^x)>>


%exponentionalequation
%NAME=Akshay T Shinde
%DIV=TE(MECH)
%ROLL NO=30

clc;
clear;
n=input('Enter the number of point:');
for i=1:n
x(i)=input('enter the value of x:');
y(i)=input('enter the corresponding value of y:');
Y(i)=log(y(i));
end
sx=0;
sY=0;
sxY=0;
sx2=0;
for i=1:n
sx=sx+x(i);
sY=sY+Y(i);
sxY=sxY+(x(i)*Y(i));
sx2=sx2+(x(i)^2);
end
d=det([sx*sx-n*sx2]);
da=det([sY*sx-n*sxY]);
db=det([sx*sxY-sY*sx2]);
a=da/d;
b=db/d;
fprintf('\nthe value of a is:%f',a);
fprintf('\nthe value of b is:%f',b);
fprintf('\nthe equestion of best fit curve %fexp(%f*x)',a,b);
enter the number of point:4
enter the value of x:0.1
enter the corresponding value of y:1.832
enter the value of x:0.2
enter the corresponding value of y:2.238
enter the value of x:0.3
enter the corresponding value of y:2.733
enter the value of x:0.4
enter the corresponding value of y:3.338
a=1.500073
b=1.999708
y=1.500073e^1.999708x<<
%2ndDegreeRegression
%NAME=Akshay T Shinde
%ROLL NO=30
%DIV=TE(MECH)
clc;
clear all;
n=input('Enter the number of point:');
sprintf('enter the value of x and y sequntially')
for i=1:n
x(i)=input('enter the value of x:');
y(i)=input('enter the corresponding value of y:');
end
sx=0;
sy=0;
sxy=0;
sx2=0;
sx3=0;
sx4=0;
sx2y=0;
for i=1:n
sx=sx+x(i);
sy=sy+y(i);
sxy=sxy+(x(i)*y(i));
sx2=sx2+(x(i)^2);
sx3=sx3+(x(i)^3);
sx4=sx4+(x(i)^4);
sx2y=sx2y+((x(i)^2)*y(i));
end
D=det([sx2sxn;sx3sx2sx;sx4sx3sx2]);
Da=det([sysxn;sxysx2sx;sx2ysx3sx2]);
Db=det([sx2syn;sx3sxysx;sx4sx2ysx2]);
Dc=det([sx2sxsy;sx3sx2sxy;sx4sx3sx2y]);
a=Da/D;
b=Db/D;
c=Dc/D;
fprintf('the value of a is:%f',a);
fprintf('the value of b is:%f',b);
fprintf('the value of b is:%f',c);
fprintf('the equestion of best curve y=%f*x^2+%f*x+%f',a,b,c);
Enter the number of point:7

enter the value of x:-3

enter the corresponding value of y:12

enter the value of x:-2

enter the corresponding value of y:4

enter the value of x:-1

enter the corresponding value of y:1

enter the value of x:0

enter the corresponding value of y:2

enter the value of x:1

enter the corresponding value of y:7

enter the value of x:2

enter the corresponding value of y:15

enter the value of x:3

enter the corresponding value of y:30

0.000000 1.000000 0.000000 2.928571


-0.000000 -0.000000 1.000000 1.666668

y=2.119048x*x+2.928571*x+1.666668
%LagrangesInterpolation
%NAME=Akshay T Shinde
%DIV=TE(MECH)
%ROLL NO=30

clc;
clear all;
n=input('Enter the number of elements:');
xg=input('enter the value of xg:');
for i=1:n
x(i)=input('enter the value of x:');
y(i)=input('enter the value of y:');
end
yg=0;
for i=1:n
nu=1;
de=1;
for j=i:n
if(i~=j)
nu=nu*(yg-x(j));
de=de*(y(i)-y(j));
end
end
L(i)=nu/de;
yg=yg+L(i)*x(i);
end
fprintf('yg=%f',yg);
Enter the number of elements:4
enter the value of xg:1.5
enter the value of x:0
enter the value of y:2
enter the value of x:1
enter the value of y:3
enter the value of x:2
enter the value of y:12
enter the value of x:5
enter the value of y:147
yg=6.125000>>
%NewtonBackwardDifference
%NAME=Akshay T Shinde
%DIV=TE(MECH)
%ROLL NO=30

clc;
clear all;
n=input('Enter the no. of points n:');
for(i=1:n)
x(i)=input('Enter the value of x:');
y(i,1)=input('Enter the corresponding value of y:');
end
c=n-1;
for(j=2:n)
for(i=1:c)
y(i,j)=y(i+1,j-1)-y(i,j-1);
end
c=c-1;
end
c=n;
for(i=1:n)
fprintf('\n%f');
for(j=1:c)
end
c=c-1;
end
xg=input('Enter the value of xg =');
h=x(2)-x(1);
u=(xg-x(1))/h;
ans=y(1,1)+(y(1,2)*u);
a=0;
m=2;
u1=u;
for(j=3:n)
if(m<j)
u=u*(u1-(m-1));
end
fact=1;
for(i=1:j-1)
fact=fact*i;
end
a=a+(u/fact)*y(1,j);
m=m+1;
end
yg=a+ans;
fprintf('\nThe value for yg=%f',yg);
Enter the value of x:2

Enter the corresponding value of y:19

Enter the value of x:3

Enter the corresponding value of y:48

Enter the value of x:4

Enter the corresponding value of y:99

Enter the value of x:5

Enter the corresponding value of y:178

Enter the value of x:6

Enter the corresponding value of y:291

Enter the value of x:7

Enter the corresponding value of y:444

Enter the value of x:8

Enter the corresponding value of y:643

Enter the value of x:9

Enter the corresponding value of y:894

Enter the value of xg =3.5

The value for yg=70.375000>>


%NeetonForwardDifference
%NAME=Akshay T Shinde
%DIV=TE(MECH)
%ROLL NO=30

clc;
clear all;
n=input('Enter the no. of points n:');
for(i=1:n)
x(i)=input('Enter the value of x:');
y(i,1)=input('Enter the corresponding value of y:');
end
c=n-1;
for(j=2:n)
for(i=1:c)
y(i,j)=y(i+1,j-1)-y(i,j-1);
end
c=c-1;
end
c=n;
for(i=1:n)
fprintf('\n%f');
for(j=1:c)
end
c=c-1;
end
xg=input('Enter the value of xg =');
h=x(2)-x(1);
u=(xg-x(1))/h;
ans=y(1,1)+(y(1,2)*u);
a=0;
m=2;
u1=u;
for(j=3:n)
if(m<j)
u=u*(u1-(m-1));
end
fact=1;
for(i=1:j-1)
fact=fact*i;
end
a=a+(u/fact)*y(1,j);
m=m+1;
end
yg=a+ans;
fprintf('\nThe value of yg=%f',yg);
Enter the no. of points n:5

Enter the value of x:0.1

Enter the corresponding value of y:1.4

Enter the value of x:0.2

Enter the corresponding value of y:1.56

Enter the value of x:0.3

Enter the corresponding value of y:1.76

Enter the value of x:0.4

Enter the corresponding value of y:2

Enter the value of x:0.5

Enter the corresponding value of y:2.28

Enter the value of xg =0.25

The value of yg=1.655000>>


%InverseInterpolation
%NAME=Akshay T Shinde
%DIV=TE(MECH)
%ROLL NO=30

clc;
clear all;
n=input('Enter the number of elements:');
yg=input('enter the value of yg:');
for i=1:n
x(i)=input('enter the value of x:');
y(i)=input('enter the value of y:');
end
xg=0;
for i=1:n
nu=1;
de=1;
for j=i:n
if(i~=j)
nu=nu*(yg-x(j));
de=de*(y(i)-y(j));
end
end
L(i)=nu/de;
xg=xg+L(i)*x(i);
end
fprintf('xg=%f',xg);
Enter the number of elements:4

enter the value of yg:2

enter the value of x:0

enter the value of y:0

enter the value of x:1

enter the value of y:1

enter the value of x:2

enter the value of y:7

enter the value of x:3

enter the value of y:25

xg=1.79613>>
%TrapezoidalRule
%NAME=Akshay T Shinde
%DIV=TE(MECH)
%ROLL NO=30

clc;
clear all;
z=input('enter f(x)=','s');
f=inline(z);
x0=input('enter x0=');
xn=input('enter xn=');
n=input('enter no of strips=');
h=(xn-x0)/n;
for i=1:n+1
x(i)=x0+(i-1)*h;
z(i)=f(x(i));
fprintf('\n\t\tx=%f,z=%f',x(i),z(i));
end
yg=0;
for i=1:n
A(i)=(h/2)*(z(i)+z(i+1));
yg=yg+A(i);
end
fprintf('\n=%f',area);
enter f(x)=4*x+2

enter x0=1

enter xn=4

enter no of strips=6

x=1.000000,z=6.000000

x=1.500000,z=8.000000

x=2.000000,z=10.000000

x=2.500000,z=12.000000

x=3.000000,z=14.000000

x=3.500000,z=16.000000

x=4.000000,z=18.000000

area=36.000000>>
%Simpsons1/3Rule
%NAME=Akshay T Shinde
%ROLL NO=30
%DIV=TE(MECH)
clc;
clear all;
z=input('enter f(x)=','s');
f=inline(z);
x0=input('enter x0=');
xn=input('enter xn=');
n=input('enter no of strips=');
h=(xn-x0)/n;
for i=1:n+1
x(i)=x0+(i-1)*h;
z(i)=f(x(i));
fprintf('\n\t\tx=%f,z=%f',x(i),z(i));
end
yg=0;
for i=1:n
A(i)=(h/3)*(z(i)+4*z(i+1)+z(i+2));
yg=yg+A(i);
end
fprintf('\nA=%f',yg);
enter f(x)=exp(x)

enter x0=0

enter xn=4

enter no of strips=4

x=0.000000,z=1.000000

x=1.000000,z=2.7181

x=2.000000,z=7.389

x=3.000000,z=20.089

x=4.000000,z=54.5981

A=53.863846>>
%Simpsons3/8Rule
%NAME=Akshay T Shinde
%ROLL NO=30
%DIV=TE(MECH)
clc;
clear all;
z=input('enter f(x)=','s');
f=inline(z);
x0=input('enter x0=');
xn=input('enter xn=');
n=input('enter no of strips=');
h=(xn-x0)/n;
for i=1:n+1
x(i)=x0+(i-1)*h;
z(i)=f(x(i));
fprintf('\n\t\tx=%f,z=%f',x(i),z(i));
end
yg=0;
for i=1:3:n
A(i)=(3*h/8)*(z(i)+3*z(i+1)+3*z(i+2)+z(i+3));
yg=yg+A(i);
end
fprintf('\nA=%f',yg);
enter f(x)=4*x-1

enter x0=1

enter xn=4

enter no of strips=6

x=1.000000,z=3.000000

x=1.500000,z=5.000000

x=2.000000,z=7.000000

x=2.500000,z=9.000000

x=3.000000,z=11.000000

x=3.500000,z=13.000000

x=4.000000,z=15.000000

A=27.000000>>
%GaussLegendre2PointFormula
%NAME=Akshay T Shinde
%ROLL NO=30
%DIV=TE(MECH)
clc;
clear all;
y=input('enter the equation f(x)=','s');
f=inline(y);
x0=input('Enter value of lower limit x0:');
xn=input('Enter value of lower limit 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);
area=(y1+y2)*c;
fprintf('Total area under the curve=%f',area);
enter the equation f(x)=x*x*x+x-1

Enter value of lower limit x0:1

Enter value of lower limit xn:4

Total area under the curve=68.250000>>


%GaussLegendre3PointFormula
%NAME=Akshay T Shinde
%ROLL NO=30
%DIV=TE(MECH)
clc;
clear all;
y=input('enter the equation f(x)=','s');
f=inline(y);
x0=input('Enter value of lower limit x0:');
xn=input('Enter value of lower limit xn:');
c=(xn-x0)/2;
d=(xn+x0)/2;
x1=(c*sqrt(3/5)+d);
x2=(-c*sqrt(3/5)+d);
x3=(c*(0)+d);
y1=f(x1);
y2=f(x2);
y3=f(x3);
area=((5/3)*y1+(8/9)*y2+(5/9)*y3);
area=area*c;
fprintf('Total area under the curve=%f',area);
enter the equation f(x)=x*x-5*x+2

Enter value of lower limit x0:3

Enter value of lower limit xn:5

Total area under the curve=3.333333>>


%Euler'sMethod
%NAME=Akshay T Shinde
%ROLL NO=30
%DIV=TE(MECH)
clc;
clear all;
z=input('enter the equation of f(x,y)=','s');
f=inline(z);
x0=input('enter the initial value x0=');
y0=input('enter the initial value y0=');
xg=input('enter the givan value xg=');
h=input('enter number step size h=');
n=(xg-x0)/h;
for i=1:n
yg=y0+h*(f(x0,y0));
x0=x0+h;
y0=yg;
end
fprintf('\nyg=%f',y0);
enter the equation of f(x,y)=x-y*y

enter the initial value x0=0

enter the initial value y0=1

enter the givan value xg=4

enter number step size h=1

yg=1.000000>>
%RungeKutta2ndOrder
%NAME=Akshay T Shinde
%ROLL NO=30
%DIV=TE(MECH)
clc;
clear all;
z=input('enter the equation of f(x,y)=','s');
f=inline(z);
x0=input('enter the initial value x0=');
y0=input('enter the initial value y0=');
xg=input('enter the givan value xg=');
h=input('enter number step size h=');
n=(xg-x0)/h;
for i=1:n
k1=h*(f(x0,y0));
k2=h*(f(x0+(h),y0+(k1)));
k=(k1+k2)/2;
y0=y0+k;
x0=x0+h;
end
fprintf('\nyg=%f',y0);
enter the equation of f(x,y)=x+y

enter the initial value x0=0

enter the initial value y0=1

enter the givan value xg=0.2

enter number step size h=0.1

yg=1.242050>>
%RungeKutta4thOrder
%NAME=Akshay T Shinde
%ROLL NO=30
%DIV=TE(MECH)
clc;
clear all;
z=input('enter the equation of f(x,y)=','s');
f=inline(z);
x0=input('enter the initial value x0=');
y0=input('enter the initial value y0=');
xg=input('enter the givan value xg=');
h=input('enter number step size h=');
n=(xg-x0)/h;
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;
y0=y0+k;
x0=x0+h;
end
fprintf('\nyg=%f',y0);
enter the equation of f(x,y)=y

enter the initial value x0=0

enter the initial value y0=2

enter the givan value xg=0.2

enter number step size h=0.1

yg=2.464208>>
%RungeKutta4thOrderForSimultaneousDifferential
%NAME=Akshay T Shinde
%ROLL NO=30
%DIV=TE(MECH)
clc;
clear all;
z=input('enter the equation of f(x,y,z)=','s');
f=inline(z);
z=input('enter the equation of g(x,y,z)=','s');
g=inline(z);
x0=input('enter the initial value x0=');
y0=input('enter the initial value y0=');
z0=input('enter the initial value z0=');
xg=input('enter the given value of xg=');
h=input('enter number step size h=');
n=(xg-x0)/h;
for i=1:n
k1=h*(f(x0,y0,z0));
l1=h*(g(x0,y0,z0));
k2=h*(f(x0+(h/2),y0+(k1/2),z0+(l1/2)));
l2=h*(g(x0+(h/2),y0+(k1/2),z0+(l1/2)));
k3=h*(f(x0+(h/2),y0+(k2/2),z0+(l2/2)));
l3=h*(g(x0+(h/2),y0+(k2/2),z0+(l2/2)));
k4=h*(f(x0+h,y0+k3,z0+l3));
l4=h*(g(x0+h,y0+k3,z0+l3));
k=(k1+(2*k2)+(2*k3)+k4)/6;
l=(l1+(2*l2)+(2*l3)+l4)/6;
yg=y0+k;
zg=z0+l;
x0=x0+h;
y0=yg;
z0=zg;
end
fprintf('\nyg=%f',y0);
fprintf('\nzg=%f',z0);
enter the equation of f(x,y,z)=z

enter the equation of g(x,y,z)=(z*z*x)-(y*y)

enter the initial value x0=0

enter the initial value y0=1

enter the initial value z0=0

enter the given of value xg=0.2

enter number step size h=0.2

yg=0.980144

zg=-0.196965>>
%ParabolicEquation
%NAME=Akshay T Shinde
%ROLL NO=30
%DIV=TE(MECH)
clc;
clear all;
y=input('enter f(x)=','s');
x0=input('\n enter initial value of x:');
xn=input('\n enter the final value of x:');
t0=input('\n enter initial avlue of t:');
tn=input('\n enter value of tn:');
h=input('\n enter value of h:');
k=input('\n enter value of k:');
u1=input('\n enter the value of u,for all t and at x0:');
u2=input('\n enter the value of u, for all t and xn:');
n=(xn-x0)/h;
n=round(n);
for i=1:n+1
x1(i)=(x0+(h*(i-1)));
end
m=(tn-t0)/k;
m=round(m);
for i=1:m+1
t1(i)=(t0+(k*(i-1)));
end
for r=1:m+1
x(r,1)=u1;
x(r,n+1)=u2;
end
for c=2:n
x(1,c)=(x0+(h*(c-1)));
end
v=(k/(h*h));
for r=2:m+1
for c=2:n
x(r,c)=v*(x(r-1,c-1))+(1-2*v)*(x(r-1,c))+v*(x(r-1,c+1));
end
end
for r=m+1:-1:1
for c=1:n+1
fprintf('\t%f',x(r,c));
end
fprintf('\n');
end
enter f(x)=2*x+1;20

enter initial value of x:0

enter the final value of x:0.5

enter initial avlue of t:0

enter value of tn:0.03

enter value of h:0.1

enter value of k:0.01

enter the value of u,for all t and at x0:1

enter the value of u, for all t and xn:1

1.0000 1.20000 0.40000 2.60000 -0.20000 1.00000

1.000000 1.200000 1.400000 0.600000 1.800000 1.000000

1.000000 1.200000 1.400000 1.600000 0.800000 1.000000

1.000000 1.20000 1.400000 1.600000 1.800000 1.000000

>>
%EllipticEquation
%NAME=Akshay T Shinde
%ROLL NO=30
%DIV=TE(MECH)
clc;
clear all;
Trhs=input('Enter value of Trhs:');
Tlhs=input('Enter value of Tlhs:');
Tupp=input('Enter value of Tupp:');
Tlow=input('Enter 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);
fprintf('\nResult=\n');
for i=1:4
fprintf('\n T%d:%0.4f',i,v(i));
end
Enter value of Trhs:0

Enter value of Tlhs:100

Enter value of Tupp:100

Enter value of Tlow:0

Result=

T1:50.0000

T2:25.0000

T3:50.0000

T4:75.0000>>
%TrapezoidalRule
%NAME=Akshay T Shinde
%ROLL NO=30
%Div:TE(MECH)
clc;
clear all;
y=input('enter the equation f(x,y)=','s');
f=inline(y);
x0=input('Enter the value of x0=');
xn=input('Enter the value of xn=');
y0=input('Enter the value of y0=');
y1=input('Enter the value of ym=');
n=input('Enter the number of strips for x=');
m=input('Enter the number of strips for y=');
h=(xn-x0)/n;
k=(y1-y0)/m;
for i=1:m+1
for j=1:n+1
a(i,j)=f(x0,y0);
x0=x0+h;
end
y0=y0+k;
x0=x0-(n+1)*h;
end
disp(a);
ans=0;
for i=1:m
for j=1:n
ans=ans+a(i,j)+a(i,j+1)+a(i+1,j)+a(i+1,j+1);
end
end
area=((h*k)/4)*ans;
fprintf('Total area under the curve=%f',area);
Enter the equation f(x,y)=x+y

Enter the value of x0=1

Enter the value of xn=3

Enter the value of y0=0

Enter the value of ym=2

Enter the number of strips for x=2

Enter the number of strips for y=2

1 2 3

2 3 4

3 4 5

Total area under the curve=12.000000>>


%Simpsons1/3RuleDI
%NAME=Akshay T Shinde
%ROLL NO=30
%Div:TE(MECH)
clc;
clear all;
y=input('enter the equation f(x,y)=','s');
f=inline(y);
x0=input('Enter the value of x0=');
xn=input('Enter the value of xn=');
y0=input('Enter the value of y0=');
y1=input('Enter the value of ym=');
n=input('Enter the number of strips for x=');
m=input('Enter the number of strips for y=');
h=(xn-x0)/n;
k=(y1-y0)/m;
for i=1:m+1
for j=1:n+1
a(i,j)=f(x0,y0);
x0=x0+h;
end
y0=y0+k;
x0=x0-(n+1)*h;
end
disp(a);
ans=0;
for i=1:2:((m/2)+1)
for j=1:2:((n/2)+1)

ans=ans+(a(i,j)+a(i,j+2)+a(i+2,j+2)+a(i+2,j)+4*(a(i+1,j)+a(i,j+1)+a(i+1,j+2)+
a(i+2,j+1))+16*(a(i+1,j+1)));
end
end
area=((h*k)/9)*ans;
fprintf('Total area under the curve=%f',area);
Enter the equation f(x,y)=x-y+1

Enter the value of x0=6

Enter the value of xn=14

Enter the value of y0=1

Enter the value of ym=5

Enter the number of strips for x=4

Enter the number of strips for y=4

6 8 10 12 14

5 7 9 11 13

4 6 8 10 12

3 5 7 9 11

2 4 6 8 10

Total area under the curve=256.000000>>


%PowerEquation:y=ax^b
%Name:Akshay T Shinde
%RollNo:30
%Div:T.E
clc;
clear all;
n=input('Enter the number of points n:');
for i=1:n
x(i)=input('Enter the value of x:');
y(i)=input('Enter the corresponding value of y:');
X(i)=log(x(i));
Y(i)=log(y(i));
end
sx=0;
sY=0;
sx2=0;
sxY=0;
for i=1:n
sx=sx+x(i);
sY=sY+Y(i);
sx2=sx2+(x(i)*x(i));
sxY=sxY+(x(i)*Y(i));
end
D=det([sx*sx-n*sxY]);
DA=det([sY*sx-n*sx2]);
DB=det([sx*sx2-sY*sxY]);
A=DA/D;
B=DB/D;
a=exp(B);
b=exp(A);
fprintf('\n\na=%f',a);
fprintf('\n\nb=%f',b);
fprintf('\nThe Equation of best fit curve is y=%f(%f^x)',a,b);
Enter the number of points n:5

Enter the value of x:2000

Enter the corresponding value of y:15

Enter the value of x:3000

Enter the corresponding value of y:15.5

Enter the value of x:4000

Enter the corresponding value of y:16

Enter the value of x:5000

Enter the corresponding value of y:17

Enter the value of x:6000

Enter the corresponding value of y:18

a=0.160694

b=0.637339

The Equation of best fit curve is y=(4.338491)X^(0.324622)>>

You might also like