Professional Documents
Culture Documents
%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)
end
fprintf('the root of equation is x3=%f',x3);
enter the equation=(exp(x)*cos(x)-1.2*sin(x)-0.5)
0 1.0000 0.6452
0 0 1.0000
Enter no of iterations=6
>>
%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
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
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
a=0.469650
b=0.122756
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
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
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
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
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
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
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
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
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
>>
%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
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
1 2 3
2 3 4
3 4 5
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
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
a=0.160694
b=0.637339