All Codes With Solver
All Codes With Solver
Roots of Equations
clc
clear all
f=@(x)(exp(x)*cos(x)-1.4);
x1=input('\nEnter the value of initial approximation x1=');
x2=input('\nEnter the value of initial approximation x2=');
fprintf('\n1=Using the accuracy condition\n2=Using iteration criterion');
ch=input('\nEnter your Choice of method to be used=');
if(ch==1)
acc=input('\nEnter the accuracy=');
while (f(x1)*f(x2)>0)
x1=input('\nEnter the value of initial approximation x1=');
x2=input('\nEnter the value of initial approximation x2=');
end
xroot=(x1+x2)/2;
it=1;
a=1;
fprintf('\nit\tx1\t\tf(x1)\t\tx2\t\tf(x2)\t\txroot\t\tcal acc)');
while (abs(a)>acc)
if(f(x1)*f(xroot)<0)
x2=xroot;
else
x1=xroot;
end
xrootold=xroot;
xroot=(x1+x2)/2;
a=abs((xroot-xrootold)/xroot);
fprintf('\n%d\t%2.4f\t%2.4f\t%8.4f\t%8.4f\t%8.4f\t%8.4f',it,x1,f(x1),x2,f(x2),xroot,a);
it=it+1;
end
end
if(ch==2)
itr=input('\nEnter the iterations required itr=');
while (f(x1)*f(x2)>0)
x1=input('\nEnter the value of initial approximation x1=');
x2=input('\nEnter the value of initial approximation x2=');
end
xroot=(x1+x2)/2;
Numerical Methods and Optimization Techniques Page 1
it=1;
fprintf('\nit\tx1\t\tf(x1)\t\tx2\t\tf(x2)\t\txroot\t\tcal acc)');
for i=1:itr
if(f(x1)*f(xroot)<0)
x2=xroot;
else
x1=xroot;
end
xrootold=xroot;
xroot=(x1+x2)/2;
a=abs((xroot-xrootold)/xroot);
fprintf('\n%d\t%2.4f\t%2.4f\t%8.4f\t%8.4f\t%8.4f\t%8.4f',it,x1,f(x1),x2,f(x2),xroot,a);
it=it+1;
end
end
Output
Enter the value of initial approximation x1=0
p=[1 -8 6];
solverroot=roots(p);
fprintf('\nSolution by Solver code is as follows');
fprintf('\nBy Program\tBy Solver');
fprintf('\n%8.4f %8.4f',xrootnew,solverroot);
output
%Solver
rootsolver=fzero(f,x1);
for k=1:n
m=k;
max=a(k,k);
for i=(k+1):n
if (max<a(i,k))
max=a(i,k);
m=i;
end
end
for j=1:n
prod=a(k,j);
a(k,j)=a(m,j);
a(m,j)=prod;
end
pmp=b(m);
Numerical Methods and Optimization Techniques Page 8
b(m)=b(k);
b(k)=pmp;
end
fprintf('\nMatrix in Partially Pivoted Format is=\n');
for i=1:n
for j=1:n
fprintf('%8.2f',a(i,j));
end
fprintf('%8.2f',b(i));
fprintf('\n');
end
for k=1:(n-1)
pivot=a(k,k);
for i=(k+1):n
multi=a(i,k)/pivot;
for j=1:n
a(i,j)=a(i,j)-(multi*a(k,j));
end
b(i)=b(i)-(multi*b(k));
end
end
fprintf('\nMatrix in Upper Triangular Format is=\n');
for i=1:n
for j=1:n
fprintf('%8.2f',a(i,j));
end
fprintf('%8.2f',b(i));
fprintf('\n');
end
x(n)=b(n)/a(n,n);
for i=(n-1):-1:1
for j=(i+1):n
b(i)=b(i)-(x(j)*a(i,j));
end
x(i)=b(i)/a(i,i);
end
for i=1:n
fprintf ('\n The values of x(%d)=%8.2f',i,x(i))
end
output
2.2500
-1.1250
Numerical Methods and Optimization Techniques Page 10
0.6250
x1=0.626400x2=0.725160x3=0.797736
x1=0.822127x2=0.868172x3=0.903116
x1=0.914709x2=0.936818x3=0.953562
x1=0.959122x2=0.969717x3=0.977742
x1=0.980407x2=0.985486x3=0.989332
x1=0.990609x2=0.993043x3=0.994887
x1=0.995499x2=0.996666x3=0.997549
x1=0.997843x2=0.998402x3=0.998825
x1=0.998966x2=0.999234x3=0.999437
>>
for i=1:n-1
for j=i+1:n
if(abs(a(i,i))<abs(a(j,i)))
for k=1:n
temp=a(i,k);
a(i,k)=a(j,k);
a(j,k)=temp;
temp=b(i);
b(i)=b(j);
b(j)=temp;
end
end
end
end
fprintf('\nMatrix in Partially Pivoted Format is=\n');
for i=1:n
for j=1:n
fprintf('%8.2f',a(i,j));
end
fprintf('%8.2f',b(i));
Numerical Methods and Optimization Techniques Page 14
fprintf('\n');
end
for i=1:n
x(i)=0;
end
itr=1;
fprintf('\nIt no\tx1\t\tx2\t\tx3');
for k=1:n1
fprintf('\n%d\t',itr);
for i=1:n
const=b(i);
for j=1:n
if(i~=j)
const=const-(x(j)*a(i,j));
end
end
y(i)=const/a(i,i);
fprintf('%8.4f',y(i));
end
itr=itr+1;
for z=1:n
x(z)=y(z);
end
end
fprintf('\nResult by Program');
for i=1:n
fprintf('\n%8.4f',x(i));
end
output
It no x1 x2 x3
1 0.5000 -1.2000 -1.3333
2 1.1333 -0.7667 -0.7000
3 0.8667 -1.1467 -1.2000
4 1.0867 -0.8933 -0.8578
5 0.9378 -1.0742 -1.1000
Result by Program
0.9378
-1.0742
-1.1000
1.0000
-1.0000
-1.0000
output
output
It x0 x1 strip_area total_area
1 0.00000.52330.4998 0.4998
2.0000
%Solution by Solver
solver=quadv(f,a1,b1)
fprintf('\nProgram Ans\tSolver Ans');
fprintf('\n%8.4f %8.4f',area,solver);
output
It x0 x1 strip_area total_area
1 1.00002.50002.8500 2.8500
2 4.00005.50001.1201 3.9701
Total Area= 1.9851
1.9459
%Solution by Solver
solver=quadv(f,a1,b1)
fprintf('\nProgram Ans\tSolver Ans');
fprintf('\n%8.4f %8.4f',area,solver);
output
It x0 x1 strip_area total_area
1 0.00000.52339.0131 9.0131
1.7695
%Solution by Solver
solver=quadv(f,a1,b1)
fprintf('\nProgram Ans\tSolver Ans');
fprintf('\n%8.4f %8.4f',area,solver);
Program
clc
clear all
fprintf('\nProgram for Gauss Legendre 2-point formula');
f=@(x)(x^3+1);
% f=@(x)(x^3-x+1);
x0=input('\nEnter the lower limit x0=');
xn=input('\nEnter the Upper limit xn=');
a=(xn-x0)/2;
b=(xn+x0)/2;
x1=(-a/sqrt(3))+b;
x2=(a/sqrt(3))+b;
area=a*(f(x1)+f(x2));
disp(f);
fprintf('\n\ta\t\tb\t\tx1\t\tx2\t\tarea');
fprintf('\n%0.4f\t%0.4f\t%0.4f\t%0.4f\t%0.4f\t',a,b,x1,x2,area);
fprintf('\nTotal area=%8.4f',area);
output
a b x1 x2 area
2.00003.00001.84534.1547 160.0000
Total area=160.0000
solver =
160.0000
Evaluate I=
output
a b x1 x2 x3 area
2.00003.00001.45083.0000 4.5492160.0000
Total area=160.0000
solver =
160.0000
Evaluate integration I= dx dy
output
2.9525
%Solution by Solver
solver=dblquad(f,a1,b1,c,d)
fprintf('\nProgram Ans\tSolver Ans');
fprintf('\n%8.4f %8.4f',area,solver);
output
30.6667
% %Solution by Solver
solver=dblquad(f,a1,b1,c,d)
fprintf('\nProgram Ans\tSolver Ans');
fprintf('\n%8.4f %8.4f',area,solver);
output
enter total no points n=8
enter values of x=0
enter value of y=-5
enter values of x=1
enter value of y=-3
enter values of x=2
enter value of y=-1
enter values of x=3
enter value of y=1
enter values of x=4
2 -5
>>
%solver code
fprintf('\nSolution by solver code');
solver=polyfit(x,y,1)
clc
clear all
fprintf('\nProgram for Curve Fitting Exponential Equation y=a.x^b');
n=input('\nenter total no of points n=');
for i=1:n
x(i)=input('enter the value of x=');
y(i)=input('enter the value of y=');
x1(i)=log(x(i));
y1(i)=log(y(i));
end
sx1=0;
sy1=0;
sx2=0;
sx1y1=0;
for i=1:n
sx1=sx1+(x1(i));
sy1=sy1+(y1(i));
sx1y1=sx1y1+(x1(i)*y1(i));
sx2=sx2+(x1(i)*x1(i));
end
d1=[n sx1;sx1 sx2];
da1=[n sy1;sx1 sx1y1];
db1=[sy1 sx1;sx1y1 sx2];
d=det(d1);
da=det(da1);
db=det(db1);
A=da/d;
B=db/d;
a=exp(B);
b=A;
fprintf('y=%f*x^(%f))',a,b);
output
%solution by solver
%solver=polyfit(x,y,'spline')
y=ax^2+bx+c
x 1 2 3 4
y 6 11 18 27
output
1.Langranes interpolation
Evaluate y xr=9
X 5 7 11 13 17
Y 150 392 1452 2366 5202
output
810
By Program By solver
810.0000 810.0000>>
%Solver
solver=interp1(x,y,xr,'spline')
fprintf('\nBy Program\tBy solver');
fprintf('\n%8.4f\t%8.4f',prod1,solver);
end
for i=1:(n-1)
for j=1:(n-i)
fprintf('%8.2f',d(i,j));
end
fprintf('\n');
end
fprintf('Ans by program is =%f',value)
output
0.4226
By Program By solver
0.4227 0.4226>>
%Solver
solver=interp1(x,y,xr,'spline')
fprintf('\nBy Program\tBy solver');
fprintf('\n%8.4f\t%8.4f',value,solver);
Evaluate x yr=810
X 5 7 11 13 17
Y 150 392 1452 2366 5202
clc
clear all
fprintf('\nProgram on Inverse Lagranges Interpolation');
n=input('\nenter total no of points n=');
yr=input('enter value of yr=');
for i=1:n
x(i)=input('enter values of x=');
y(i)=input('enter values of y=');
end
prod1=0;
for i=1:n
prod=x(i);
for j=1:n
if(i~=j)
prod=((yr-y(j))/(y(i)-y(j)))*prod;
end
end
prod1=prod1+prod;
end
fprintf('the value of y=%f',prod1);
output
By Program By solver
9.2331 9.2391>>
%Solver
solver=interp1(y,x,yr,'spline')
fprintf('\nBy Program\tBy solver');
fprintf('\n%8.4f\t%8.4f',prod1,solver);
output
@(x,y)(1+x*y)
0
0.0500
0.1000
0.1500
0.2000
0.2500
0.3000
0.3500
0.4000
0.4500
0.5000
y=
1.0000
1.0513
1.1053
1.1624
1.2229
1.2870
1.3552
1.4278
1.5053
1.5882
1.6770
>>
%Solver
[x y]=ode23(f,[a xn],b)
clc
clear all
fprintf('\nProgram for Runge Kutta 4th order Method');
% f=@(x,y)(y-x)
f=@(x,y)(1+x*y)
x0=input('\nEnter the value of x0=');
a=x0;
y0=input('\nEnter the value of y0=');
b=y0;
xn=input('\nEnter the value of xn=');
h=input('\nEnter the value of h=');
itr=1;
while(x0<xn)
s1=h*f(x0,y0);
s2=h*f(x0+(h/2),y0+(s1/2));
s3=h*f(x0+(h/2),y0+(s2/2));
s4=h*f((x0+h),(y0+s3));
y1=y0+((s1+2*s2+2*s3+s4)/6);
x0=x0+h;
y0=y1;
fprintf('\n%d\t%0.4f\t%0.4f',itr,x0,y0);
itr=itr+1;
end
fprintf('\nSolution by Runge Kutta 4th order Method=%8.4f',y1);
output
@(x,y)(y-x)
0
0.0200
0.0400
0.0600
0.0800
0.1000
0.1200
0.1400
0.1600
0.1800
0.2000
y=
2.0000
2.0402
2.0808
2.1218
2.1633
2.2052
2.2475
2.2903
2.3335
2.3772
2.4214
>>
%Solver
[x y]=ode23(f,[a xn],b)
dz/dx=(1.5*z-4.5*y+4.5)
dy/dx=(x+z)
subjected to x=0,y=0,z=0,h=0.1
evaluate y and z at x=0.2
output
@(x,y,z)(1.5*z-4.5*y+4.5)
@(x,y,z)(x+z)
1 0.10000.02750.4838
2 0.20000.11641.0191
Solution by Simultaneus Runge Kutta 2nd order Method
x0 y0 z0
0.2000 0.1164 1.0191 >>