You are on page 1of 51

Assignment1

Roots of Equations

1. Find roots of equation f(x)= e*cosx-1.4 initial guess x1=0, x2=1

%Program of Bisection method combined

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

Enter the value of initial approximation x2=1

1=Using the accuracy condition


2=Using iteration criterion
Enter your Choice of method to be used=1

Enter the accuracy=0.001

it x1 f(x1) x2 f(x2) xroot cal acc)


1 0.0000-0.4000 0.5000 0.0469 0.2500 1.0000
2 0.2500-0.1559 0.5000 0.0469 0.3750 0.3333
3 0.3750-0.0461 0.5000 0.0469 0.4375 0.1429
4 0.3750-0.0461 0.4375 0.0030 0.4063 0.0769
5 0.4063-0.0210 0.4375 0.0030 0.4219 0.0370
6 0.4219-0.0089 0.4375 0.0030 0.4297 0.0182
7 0.4297-0.0029 0.4375 0.0030 0.4336 0.0090
8 0.4297-0.0029 0.4336 0.0000 0.4316 0.0045
9 0.4316-0.0014 0.4336 0.0000 0.4326 0.0023
10 0.4326-0.0007 0.4336 0.0000 0.4331 0.0011
11 0.4331-0.0003 0.4336 0.0000 0.4333 0.0006
Solution by solver is as follows:

Program ans Solver ans


0.4333 0.4336>>

Numerical Methods and Optimization Techniques Page 2


%solver
fprintf('\nSolution by solver is as follows:\n');
rootsolver=fzero(f,x1,x2);
fprintf('\nProgram ans\t\tSolver ans');
fprintf('\n %8.4f\t %8.4f',xroot,rootsolver)

Numerical Methods and Optimization Techniques Page 3


2.Find roots of equation f(x)=x^2-8x+6=0 ..initial guess x1=0 using successive apprx.

%Program of Successive Approximation by Accuracy


% clc
% clear all
g=@(x)((x^2+6)/8);
g1=@(x)(x/4);
x1=input('\nEnter the Initial Approximation=');
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(abs(g1(x1))>1)
x1=input('\nReenter the Initial Approximation=');
end
xrootnew=g(x1);
xrootold=100;
it=1;
fprintf('\nIT\tX1\t\txrootnew\tcalculated accuracy');
while(abs((xrootnew-xrootold)/xrootnew)>acc)
xrootold=xrootnew;
fprintf('\n%d %8.4f %8.4f %12.4f',it,x1,xrootnew,abs((xrootnew-xrootold)/xrootnew));
x1=xrootnew;
xrootnew=g(x1);
it=it+1;
end
end
if(ch==2)
itr=input('\nEnter the no of iteration required=');
while(abs(g1(x1))>1)
x1=input('\nReenter the Initial Approximation=');
end
xrootnew=g(x1);
xrootold=100;
it=1;
fprintf('\nIT\tX1\t\txrootnew\tcalculated accuracy');
for i=1:itr
xrootold=xrootnew;
x1=xrootnew;
xrootnew=g(x1);
fprintf('\n%d %8.4f %8.4f %12.4f',it,x1,xrootnew,abs((xrootnew-xrootold)/xrootnew));
it=it+1;
end
end

Numerical Methods and Optimization Techniques Page 4


output
succ_combine

Enter the Initial Approximation=0

1=Using the accuracy condition


2=Using iteration criterion
Enter your Choice of method to be used=2

Enter the no of iteration required=10

IT X1 xrootnew calculated accuracy


1 0.7500 0.8203 0.0857
2 0.8203 0.8341 0.0165
3 0.8341 0.8370 0.0034
4 0.8370 0.8376 0.0007
5 0.8376 0.8377 0.0001
6 0.8377 0.8377 0.0000
7 0.8377 0.8377 0.0000
8 0.8377 0.8377 0.0000
9 0.8377 0.8377 0.0000
10 0.8377 0.8377 0.0000
Solution by Solver code is as follows
By Program By Solver
0.8377 7.1623
0.8377 >>
%Solver code

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);

Numerical Methods and Optimization Techniques Page 5


3.Find roots of equation using NR method of eq f(x)= e^x*cosx-1.4 with initial guess
x1=0.

%Newton Raphson Method


clc
clear all
fprintf('\nCombine program of Newton Raphson Method=');
f=@(x)(exp(x)*cos(x)-1.4);
df=@(x)(exp(x)*(cos(x)-sin(x)));
d2f=@(x)(-2*exp(x)*sin(x));

x1=input('\nEnter the value of initial approximation x1=');


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)*d2f(x1)/(df(x1)^2))>1)
x1=input('\nEnter the value of initial approximation x1=');
end
xroot=(x1-(f(x1)/df(x1)));
it=1;
a=1;
fprintf('\nSolution of Program based on Accuracy criterion is as follows\n');
fprintf('\nit\tx1\t\tf(x1)\t\txroot\t\tcal acc)');
while (abs(a)>acc)
xrootold=xroot;
x1=xroot;
xroot=(x1-(f(x1)/df(x1)));
a=((xroot-xrootold)/xroot);
fprintf('\n%d\t%2.4f\t%2.4f\t%8.4f\t%8.4f',it,x1,f(x1),xroot,a);
it=it+1;
end
end
if (ch==2)
itr=input('\nEnter the number of iterations required=');
while ((f(x1)*d2f(x1)/(df(x1)^2))>1)
x1=input('\nEnter the value of initial approximation x1=');
end
xroot=(x1-(f(x1)/df(x1)));
it=1;
a=1;
fprintf('\nSolution of Program based on Iterations criterion is as follows\n');
fprintf('\nit\tx1\t\tf(x1)\t\txroot\t\tcal acc)');
for i=1:itr
xrootold=xroot;
Numerical Methods and Optimization Techniques Page 6
x1=xroot;
xroot=(x1-(f(x1)/df(x1)));
a=((xroot-xrootold)/xroot);
fprintf('\n%d\t%2.4f\t%2.4f\t%8.4f\t%8.4f',it,x1,f(x1),xroot,a);
it=it+1;
end
end

output

Combine program of Newton Raphson Method=


Enter the value of initial approximation x1=0

1=Using the accuracy condition


2=Using iteration criterion
Enter your Choice of method to be used=1

Enter the accuracy=0.001

Solution of Program based on Accuracy criterion is as follows

it x1 f(x1) xroot cal acc)


1 0.4000-0.0259 0.4327 0.0756
2 0.4327-0.0006 0.4336 0.0020
3 0.4336-0.0000 0.4336 0.0000

Solution by Newton Raphson Method is as follows:

By Program By Solver code


0.4336 0.4336>>

%Solver
rootsolver=fzero(f,x1);

fprintf('\n\nSolution by Newton Raphson Method is as follows:');


fprintf('\n\nBy Program\t\tBy Solver code');
fprintf('\n%8.4f\t %8.4f', xroot,rootsolver);

Numerical Methods and Optimization Techniques Page 7


Assignment2
Solution of Simultaneous Equations
1.Solve following simultaneous equation by guass elimination by partial pivoting
x1+3x2+5x3=2, 3x1+2x2+4x3=7, 2x1+x2+x3=4

%Gaussian Elimination with partial pivoting


clc
clear all
fprintf('\nProgram of Gauss Elimination Method');
fprintf('\nYour Name');
n=input('Enter total no of equation n=');
for i=1:n
for j=1:n
a(i,j)=input ('Enter the values of a=');
c(i,j)=a(i,j);
end
b(i)=input ('Enter the values of b=');
d(i)=b(i);
end
fprintf('\nMatrix in Original 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
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

fprintf('\n\nSolution by Solver Code is as follows:');


solv=inv(c)*d'
fprintf('\nResult by Gauss Elimination Method is as follows:');
Numerical Methods and Optimization Techniques Page 9
fprintf('\nVariable\tBy Program\tBy Solver');
for i=1:n
fprintf('\n\tX(%d)\t%8.4f\t%8.4f',i,x(i),solv(i));
end

output

Program of Gauss Elimination Method


Your NameEnter total no of equation n=3
Enter the values of a=1
Enter the values of a=3
Enter the values of a=5
Enter the values of b=2
Enter the values of a=3
Enter the values of a=2
Enter the values of a=4
Enter the values of b=7
Enter the values of a=2
Enter the values of a=1
Enter the values of a=1
Enter the values of b=4

Matrix in Original Format is=


1.00 3.00 5.00 2.00
3.00 2.00 4.00 7.00
2.00 1.00 1.00 4.00

Matrix in Partially Pivoted Format is=


3.00 2.00 4.00 7.00
1.00 3.00 5.00 2.00
2.00 1.00 1.00 4.00

Matrix in Upper Triangular Format is=


3.00 2.00 4.00 7.00
0.00 2.33 3.67 -0.33
0.00 0.00 -1.14 -0.71

The values of x(1)= 2.25


The values of x(2)= -1.12
The values of x(3)= 0.62

Solution by Solver Code is as follows:


solv =

2.2500
-1.1250
Numerical Methods and Optimization Techniques Page 10
0.6250

Result by Gauss Elimination Method is as follows:


Variable By Program By Solver
X(1) 2.2500 2.2500
X(2) -1.1250 -1.1250
X(3) 0.6250 0.6250>> 0.001

Numerical Methods and Optimization Techniques Page 11


2.Solve following set of simultaneous equation using guass seidal method for 10
iterations 10x1-5x2-2x3=3, -4x1+10x2-3x3=3, -x1-6x2+10x3=3

%Simultaneous Equations by Gauss Seidal Method


clc
clear all
n=input('Enter no.of equations=');
for i=1:n
for j=1:n
a(i,j)=input('Enter A matrix element row-wise=');
end
end
for i=1:n
b(i)=input('Enter B matrix element=');
end
n1=input('Enter the number of iterations n1=');
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
for i=1:n
x(i)=0;
end
for k=1:n1
for i=1:n
const=b(i);
for j=1:n
if(i~=j)
const=const-(x(j)*a(i,j));
end
end
x(i)=const/a(i,i);
fprintf('x%d=%f',i,x(i));
end
fprintf('\n\n');
Numerical Methods and Optimization Techniques Page 12
end
output

Enter no.of equations=3


Enter A matrix element row-wise=10
Enter A matrix element row-wise=-5
Enter A matrix element row-wise=-2
Enter A matrix element row-wise=-4
Enter A matrix element row-wise=10
Enter A matrix element row-wise=-3
Enter A matrix element row-wise=-1
Enter A matrix element row-wise=-6
Enter A matrix element row-wise=10
Enter B matrix element=3
Enter B matrix element=3
Enter B matrix element=3
Enter the number of iterations n1=10
x1=0.300000x2=0.420000x3=0.582000

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

>>

Numerical Methods and Optimization Techniques Page 13


3.Solve following simultaneous equation by using guass Jacobi for five iterations
4x1+x2+x3=2, x1+5x2+2x3=-6, x1+2x2+3x3=-4

%Jacob Iteration Method


clc
clear all
fprintf('\nJacob Iteration Method\n');
n=input('Enter total no of equation n=');
for i=1:n
for j=1:n
a(i,j)=input ('Enter the values of a=');
c(i,j)=a(i,j);
end
b(i)=input ('Enter the values of b=');
d(i)=b(i);
end
n1=input('Enter the number of iterations n1=');
fprintf('\nMatrix in Original 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 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

Jacob Iteration Method


Enter total no of equation n=3
Enter the values of a=4
Enter the values of a=1
Enter the values of a=1
Enter the values of b=2
Enter the values of a=1
Enter the values of a=5
Enter the values of a=2
Enter the values of b=-6
Enter the values of a=1
Enter the values of a=2
Enter the values of a=3
Numerical Methods and Optimization Techniques Page 15
Enter the values of b=-4
Enter the number of iterations n1=5

Matrix in Original Format is=


4.00 1.00 1.00 2.00
1.00 5.00 2.00 -6.00
1.00 2.00 3.00 -4.00

Matrix in Partially Pivoted Format is=


4.00 1.00 1.00 2.00
1.00 5.00 2.00 -6.00
1.00 2.00 3.00 -4.00

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

Solution by Solver Code is as follows:


solv =

1.0000
-1.0000
-1.0000

Result by Jacob Iteration Method is as follows:


Variable By Program By Solver
X(1) 0.9378 1.0000
X(2) -1.0742 -1.0000
X(3) -1.1000 -1.0000>>

fprintf('\n\nSolution by Solver Code is as follows:');


solv=inv(c)*d'
fprintf('\nResult by Jacob Iteration Method is as follows:');
fprintf('\nVariable\tBy Program\tBy Solver');
for i=1:n
fprintf('\n\tX(%d)\t%8.4f\t%8.4f',i,x(i),solv(i));
end

Numerical Methods and Optimization Techniques Page 16


4.Solve following simultaneous equations by using
Thomas algorithm method 2.04x1-x2=40.8, -x1+2.04x2-x3=0.8, -x2+2.04x3-x4=0.8, -
x3+2.04x4=200.8

%Thomas Algorithm for Tridiagonal Matrices


clc
clear all
fprintf('\nSolution using Thomas Algorithm');
n=input('\nEnter total no of equation n=');
for i=2:n
e(i)=input('\nEnter the Subdiagonal Elements e=');
end
for i=1:n
f(i)=input('\nEnter the Major-diagonal Elements f=');
end
for i=1:n-1
g(i)=input('\nEnter the Super-diagonal Elements g=');
end
for i=1:n
r(i)=input('\nEnter the Constant of Equation r=');
end
for k=2:n
e(k)=e(k)/f(k-1);
f(k)=f(k)-(e(k)*g(k-1));
end
for k=2:n
r(k)=r(k)-(e(k)*r(k-1));
end
x(n)=r(n)/f(n);
for k=(n-1):-1:1
x(k)=(r(k)-(g(k)*x(k+1)))/f(k);
end
fprintf('\nNew values of Subdiagonal Elements are as follows=');
for k=2:n
fprintf('\ne(%d)=%8.4f',k,e(k));
end
fprintf('\nNew values of Major-diagonal Elements are as follows=');
for k=1:n
fprintf('\nf(%d)=%8.4f',k,f(k));
end
fprintf('\nNew values of Super-diagonal Elements are as follows=');
for k=1:n-1
fprintf('\ng(%d)=%8.4f',k,g(k));
end
Numerical Methods and Optimization Techniques Page 17
fprintf('\nNew values of Constants are as follows=');
for k=1:n
fprintf('\nr(%d)=%8.4f',k,r(k));
end
fprintf('\nFinal Solution is as follows=');
for k=1:n
fprintf('\nx(%d)=%8.4f',k,x(k));
end

output

Solution using Thomas Algorithm


Enter total no of equation n=4

Enter the Subdiagonal Elements e=-1

Enter the Subdiagonal Elements e=-1

Enter the Subdiagonal Elements e=-1

Enter the Major-diagonal Elements f=2.04

Enter the Major-diagonal Elements f=2.04

Enter the Major-diagonal Elements f=2.04

Enter the Major-diagonal Elements f=2.04

Enter the Super-diagonal Elements g=-1

Enter the Super-diagonal Elements g=-1

Enter the Super-diagonal Elements g=-1

Enter the Constant of Equation r=40.8

Enter the Constant of Equation r=0.8

Enter the Constant of Equation r=0.8

Enter the Constant of Equation r=200.8

New values of Subdiagonal Elements are as follows=


e(2)= -0.4902
e(3)= -0.6452
e(4)= -0.7170
Numerical Methods and Optimization Techniques Page 18
New values of Major-diagonal Elements are as follows=
f(1)= 2.0400
f(2)= 1.5498
f(3)= 1.3948
f(4)= 1.3230
New values of Super-diagonal Elements are as follows=
g(1)= -1.0000
g(2)= -1.0000
g(3)= -1.0000
New values of Constants are as follows=
r(1)= 40.8000
r(2)= 20.8000
r(3)= 14.2211
r(4)=210.9961
Final Solution is as follows=
x(1)= 65.9698
x(2)= 93.7785
x(3)=124.5382
x(4)=159.4795>>

Numerical Methods and Optimization Techniques Page 19


Assignment 03
Numerical Integration
1.Trapezoidal rule
Evaluate integration I= for 6 no of strips
% Program for Numerical Integration using Trapezoidal Rule
clc
clear all
fprintf('\nProgram for Treapezoidal Rule');
f=@(x)(sin(x));
x0=input('\nEnter the lower limit x0=');
xn=input('\nEnter the Upper limit xn=');
n=input('\nEnter the total no of major strips n=');
a1=x0;b1=xn;
h=(xn-x0)/n;
area=0;
it=1;
fprintf('\nFunction used for the program is as follows\n');
disp(f);
fprintf('\nIt\tx0\t\tx1\t\tstrip_area\t total_area');
for i=1:n
area1=(f(x0)+f(x0+h));
area=area+area1;
fprintf('\n%d\t%0.4f\t%0.4f\t%0.4f\t\t%0.4f',it,x0,x0+h,area1, area);
x0=x0+h;
it=it+1;
end
area=area*h/2;
fprintf('\nTotal Area=%8.4f',area);

output

Program for Treapezoidal Rule


Enter the lower limit x0=0

Enter the Upper limit xn=3.14

Enter the total no of major strips n=6

Function used for the program is as follows


@(x)(sin(x))

It x0 x1 strip_area total_area
1 0.00000.52330.4998 0.4998

Numerical Methods and Optimization Techniques Page 20


2 0.52331.04671.3655 1.8653
3 1.04671.57001.8658 3.7311
4 1.57002.09331.8666 5.5976
5 2.09332.61671.3677 6.9653
6 2.61673.14000.5027 7.4681
Total Area= 1.9541
solver =

2.0000

Program Ans Solver Ans


1.9541 2.0000>>

%Solution by Solver
solver=quadv(f,a1,b1)
fprintf('\nProgram Ans\tSolver Ans');
fprintf('\n%8.4f %8.4f',area,solver);

Numerical Methods and Optimization Techniques Page 21


2. Simpson’s 1/3rd
Evaluate I= for 2 major strips.
Programe
clc
clear all
fprintf('\nProgram for Simpsons 1/3rd Rule');
f=@(x)(1/x);
%f=@(x)(sin(x));
x0=input('\nEnter the lower limit x0=');
xn=input('\nEnter the Upper limit xn=');
n=input('\nEnter the total no of major strips n=');
a1=x0;b1=xn;
h=(xn-x0)/(2*n);
area=0;
it=1;
fprintf('\nFunction used for the program is as follows\n');
disp(f);
fprintf('\nIt\tx0\t\tx1\t\tstrip_area\t total_area');
for i=1:n
area1=(f(x0)+4*f(x0+h)+f(x0+2*h));
area=area+area1;
fprintf('\n%d\t%0.4f\t%0.4f\t%0.4f\t\t%0.4f',it,x0,x0+h,area1, area);
x0=x0+2*h;
it=it+1;
end
area=area*h/3;
fprintf('\nTotal Area=%8.4f',area);

output

Program for Simpsons 1/3rd Rule


Enter the lower limit x0=1

Enter the Upper limit xn=7

Enter the total no of major strips n=2

Function used for the program is as follows


@(x)(1/x)

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

Numerical Methods and Optimization Techniques Page 22


solver =

1.9459

Program Ans Solver Ans


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);

Numerical Methods and Optimization Techniques Page 23


3.Simpsons 3/8 rule
Evaluate I= for 1 major strips.

% Program for Numerical Integration using Simpson's 3/8th Rule


clc
clear all
fprintf('\nProgram for Simpsons 3/8th Rule');
f=@(x)((sin(x)+cos(x))^0.5);
x0=input('\nEnter the lower limit x0=');
xn=input('\nEnter the Upper limit xn=');
n=input('\nEnter the total no of major strips n=');
a1=x0;b1=xn;
h=(xn-x0)/(3*n);
area=0;
it=1;
fprintf('\nFunction used for the program is as follows\n');
disp(f);
fprintf('\nIt\tx0\t\tx1\t\tstrip_area\t total_area');
for i=1:n
area1=(f(x0)+3*f(x0+h)+3*f(x0+2*h)+f(x0+3*h));
area=area+area1;
fprintf('\n%d\t%0.4f\t%0.4f\t%0.4f\t\t%0.4f',it,x0,x0+h,area1, area);
x0=x0+3*h;
it=it+1;
end
area=area*3*h/8;
fprintf('\nTotal Area=%8.4f',area);

output

Program for Simpsons 3/8th Rule


Enter the lower limit x0=0

Enter the Upper limit xn=1.57

Enter the total no of major strips n=1

Function used for the program is as follows


@(x)((sin(x)+cos(x))^0.5)

It x0 x1 strip_area total_area
1 0.00000.52339.0131 9.0131

Numerical Methods and Optimization Techniques Page 24


Total Area= 1.7688
solver =

1.7695

Program Ans Solver Ans


1.7688 1.7695>>

%Solution by Solver
solver=quadv(f,a1,b1)
fprintf('\nProgram Ans\tSolver Ans');
fprintf('\n%8.4f %8.4f',area,solver);

Numerical Methods and Optimization Techniques Page 25


4.Gauss 2 point
Evaluate I=

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

Program for Gauss Legendre 2-point formula


Enter the lower limit x0=1

Enter the Upper limit xn=5


@(x)(x^3+1)

a b x1 x2 area
2.00003.00001.84534.1547 160.0000
Total area=160.0000
solver =

160.0000

Program Ans Solver Ans


160.0000 160.0000>>
%Solution by Solver
solver=quadv(f,x0,xn)
fprintf('\nProgram Ans\tSolver Ans');
fprintf('\n%8.4f %8.4f',area,solver);

Numerical Methods and Optimization Techniques Page 26


5. Gauss 3 point:

Evaluate I=

% Program for NI using Gauss Legendre 3-point formula


clc
clear all
fprintf('\nProgram for Gauss Legendre 3-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/5))+b;
x2=b;
x3=(a*sqrt(3/5))+b;
area=a*((5/9)*f(x1)+(8/9)*f(x2)+(5/9)*f(x3));
disp(f);
fprintf('\n\ta\t\tb\t\tx1\t\tx2\t\tx3\t\tarea');
fprintf('\n%0.4f\t%0.4f\t%0.4f\t%0.4f\t%0.4f\t%0.4f',a,b,x1,x2,x3,area);
fprintf('\nTotal area=%8.4f',area);

output

Program for Gauss Legendre 3-point formula


Enter the lower limit x0=1

Enter the Upper limit xn=5


@(x)(x^3+1)

a b x1 x2 x3 area
2.00003.00001.45083.0000 4.5492160.0000
Total area=160.0000
solver =

160.0000

Program Ans Solver Ans


160.0000 160.0000>>

Numerical Methods and Optimization Techniques Page 27


%Solution by Solver
solver=quadv(f,x0,xn)
fprintf('\nProgram Ans\tSolver Ans');
fprintf('\n%8.4f %8.4f',area,solver);

Numerical Methods and Optimization Techniques Page 28


6.Double integration by trapezoidal

Evaluate integration I= dx dy

%Program for Double Integration using Trapezoidal Rule


clc
clear all
fprintf('\nDouble Integration using Trapezoidal Rule');
%f=@(x,y)(x+y);
%f=@(x,y)(exp(x+y));
f=@(x,y)(x*x+y*y+5);
%f=@(x,y)(exp(x+(2*y)));
x0=input('\nEnter lower limit in direction of x x0=');
xn=input('\nEnter upper limit in direction of x xn=');
N=input('\nEnter no of strips along x N=');
y0=input('\nEnter lower limit in direction of y y0=');
yn=input('\nEnter upper limit in direction of y yn=');
M=input('\nEnter no of strips along y M=');
a1=x0; b1=xn;c=y0;d=yn;
h=(xn-x0)/N;
k=(yn-y0)/M;
b=y0;
disp(f);
for i=1:(N+1)
for j=1:(M+1)
a(i,j)=f(x0,y0);
y0=y0+k;
end
x0=x0+h;
y0=b;
end
for i=1:(M+1)
s(i)=0;
for j=1:N
s(i)=s(i)+(a(j,i)+a((j+1),i));
end
end
for i=1:(N+1)
for j=1:(M+1)
fprintf('%8.4f',a(i,j));
end
fprintf('\n');
end
for i=1:(M+1)

Numerical Methods and Optimization Techniques Page 29


fprintf('%8.4f',s(i));
end
area=0;
for i=1:M
area=area+(s(i)+s(i+1));
end
area=(h*k/4)*area;
fprintf ('\nThe area by trapezoidal rule=%f',area)

output

Double Integration using Trapezoidal Rule


Enter lower limit in direction of x x0=0

Enter upper limit in direction of x xn=1

Enter no of strips along x N=2

Enter lower limit in direction of y y0=0

Enter upper limit in direction of y yn=1

Enter no of strips along y M=2


@(x,y)(exp(x+y))

1.0000 1.6487 2.7183


1.6487 2.7183 4.4817
2.7183 4.4817 7.3891
7.0157 11.5670 19.0707
The area by trapezoidal rule=3.076274
solver =

2.9525

Program Ans Solver Ans


3.0763 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);

Numerical Methods and Optimization Techniques Page 30


7.Simpson’s double integration
Evaluate

% Double Integration by Simpsons 1/3 rule


clc
clear all
f=@(x,y)(x.*x+y.*y+5);
%f=@(x,y)(exp(x+(2*y)));
fprintf('\nDouble Integration using Simpsons 1/3 rule\n');
x0=input('enter lower limit in direction of x x0=');
xn=input('enter upper limit in direction of x xn=');
N=input('enter no of strips along x N=');
y0=input('enter lower limit in direction of y y0=');
yn=input('enter upper limit in direction of y yn=');
M=input('enter no of strips along y M=');
a1=x0; b1=xn;c=y0;d=yn;
h=(xn-x0)/(2*N);
k=(yn-y0)/(2*M);
b=y0;
disp(f);
for i=1:((2*N)+1)
for j=1:((2*M)+1)
a(i,j)=f(x0,y0);
y0=y0+k;
end
x0=x0+h;
y0=b;
end
for i=1:((2*M)+1)
s(i)=0;
for j=1:2:((2*N)-1)
s(i)=s(i)+(a(j,i)+4*a((j+1),i))+a((j+2),i);
end
end
area=0;
for i=1:2:((2*M)-1)
area=area+(s(i)+4*s(i+1)+s(i+2));
end
area=area*h*k/9;
fprintf ('the area by simpsons rule=%f',area)

output

Double Integration using Simpsons 1/3 rule


enter lower limit in direction of x x0=0

Numerical Methods and Optimization Techniques Page 31


enter upper limit in direction of x xn=2
enter no of strips along x N=2
enter lower limit in direction of y y0=0
enter upper limit in direction of y yn=2
enter no of strips along y M=2
@(x,y)(x.*x+y.*y+5)

the area by simpsons rule=30.666667


solver =

30.6667

Program Ans Solver Ans


30.6667 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);

Numerical Methods and Optimization Techniques Page 32


Assignment4
Curve fitting
1.Straught line
X 0 1 2 3 4 5 6 7
Y -5 -3 -1 1 3 5 7 9

%Program for Curve Fitting Straight Line y=ax+b


clc
clear all
n=input('enter total no points n=');
sx=0;
sx2=0;
sy=0;
sxy=0;
for i=1:n
x(i)=input('enter values of x=');
y(i)=input('enter value of y=');
end
for i=1:n
sx=sx+x(i);
sy=sy+y(i);
sxy=sxy+(x(i)*y(i));
sx2=sx2+(x(i)*x(i));
end
d1=[n sx;sx sx2];
da1=[n sy;sx sxy];
db1=[sy sx;sxy sx2];
d=det(d1);
da=det(da1);
db=det(db1);
a=da/d;
b=db/d;
fprintf('y=%f*x+%f',a,b)

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

Numerical Methods and Optimization Techniques Page 33


enter value of y=3
enter values of x=5
enter value of y=5
enter values of x=6
enter value of y=7
enter values of x=7
enter value of y=9
y=2.000000*x+-5.000000
Solution by solver code
solver =

2 -5

>>

%solver code
fprintf('\nSolution by solver code');
solver=polyfit(x,y,1)

Numerical Methods and Optimization Techniques Page 34


2.Power fit
y=a.b^x
X 2000 3000 4000 5000 6000
Y 15 15.5 16 17 18

%Program for Curve Fitting Power Equation y=a.b^x


clc
clear all
fprintf('Program for Curve Fitting Power Equation y=a.b^x');
n=input('enter 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=');
y1(i)=log(y(i));
end
sx=0;
sy1=0;
sx2=0;
sxy1=0;
for i=1:n
sx=sx+(x(i));
sy1=sy1+(y1(i));
sxy1=sxy1+(x(i)*y1(i));
sx2=sx2+(x(i)*x(i));
end
d1=[n sx;sx sx2];
da1=[n sy1;sx sxy1];
db1=[sy1 sx;sxy1 sx2];
d=det(d1);
da=det(da1);
db=det(db1);
A=da/d;
B=db/d;
a=exp(B);
b=exp(A);
fprintf('y=%f*(%f)^x)',a,b);

Numerical Methods and Optimization Techniques Page 35


3.Exponential
Y=a*e^x
Time 1 2 3 4
temp 70 83 100 124

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

Program for Curve Fitting Exponential Equation y=a.x^b


enter total no of points n=4
enter the value of x=1
enter the value of y=70
enter the value of x=2

Numerical Methods and Optimization Techniques Page 36


enter the value of y=83
enter the value of x=3
enter the value of y=100
enter the value of x=4
enter the value of y=124
y=67.249259*x^(0.396206))>>
>> cftool
>> x=[1 2 3 4];
>> y=[70 80 100 124];
>>

%solution by solver
%solver=polyfit(x,y,'spline')

Numerical Methods and Optimization Techniques Page 37


5.Quadratic

y=ax^2+bx+c
x 1 2 3 4
y 6 11 18 27

%Program for Curve Fitting Quadratic Equation y=ax^2+bx+c


clc
clear all
fprintf('\nProgram for Curve Fitting Quadratic Equation y=ax^2+bx+c');
n=input('\nenter total no of points n=');
for i=1:n
x(i)=input ('enter the values of x=');
y(i)=input('enter the values of y=');
end
sx=0;
sy=0;
sx2=0;
sx3=0;
sx4=0;
sxy=0;
sx2y=0;
for i=1:n
sx=sx+x(i);
sy=sy+y(i);
sx2=sx2+(x(i)*x(i));
sx3=sx3+(x(i)*x(i)*x(i));
sx4=sx4+(x(i)*x(i)*x(i)*x(i));
sxy=sxy+(x(i)*y(i));
sx2y=sx2y+(x(i)*x(i)*y(i));
end
d1=[n sx sx2;sx sx2 sx3;sx2 sx3 sx4];
da1=[n sx sy;sx sx2 sxy;sx2 sx3 sx2y];
db1=[n sy sx2;sx sxy sx3;sx2 sx2y sx4];
dc1=[sy sx sx2;sxy sx2 sx3;sx2y sx3 sx4];
d=det(d1);
da=det(da1);
db=det(db1);
dc=det(dc1);
a=da/d;
b=db/d;
c=dc/d;
fprintf('y=%f*x*x+%f*x+%f',a,b,c);

output

Numerical Methods and Optimization Techniques Page 38


Program for Curve Fitting Quadratic Equation y=ax^2+bx+c
enter total no of points n=4
enter the values of x=1
enter the values of y=6
enter the values of x=2
enter the values of y=11
enter the values of x=3
enter the values of y=18
enter the values of x=4
enter the values of y=27
y=1.000000*x*x+2.000000*x+3.000000
solver =

1.0000 2.0000 3.0000


%Solver code
solver=polyfit(x,y,2)

Numerical Methods and Optimization Techniques Page 39


Assignment 5
Interpolation

1.Langranes interpolation

Evaluate y xr=9
X 5 7 11 13 17
Y 150 392 1452 2366 5202

%Program on Lagranges Interpolation


clc
clear all
fprintf('\nProgram on Lagranges Interpolation');
n=input('\nenter total no of points n=');
xr=input('enter value of xr=');
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=y(i);
for j=1:n
if(i~=j)
prod=((xr-x(j))/(x(i)-x(j)))*prod;
end
end
prod1=prod1+prod;
end
fprintf('the value of y=%f',prod1);

output

Program on Lagranges Interpolation


enter total no of points n=5
enter value of xr=9
enter values of x=5
enter values of y=150
enter values of x=7
enter values of y=392
enter values of x=11
enter values of y=1452
enter values of x=13

Numerical Methods and Optimization Techniques Page 40


enter values of y=2366
enter values of x=17
enter values of y=5202
the value of y=810.000000
solver =

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);

Numerical Methods and Optimization Techniques Page 41


2.Newtons forward
Evaluate y at x=25
X 10 20 30 40 50
Y 0.1736 0.3420 0.5 0.6428 0.7660

%Program on Forward Interpolation


clc
clear all
fprintf('\nProgram on Forward Interpolation');
n=input('\nenter total no. of points n=');
xr=input('enter value of xr=');
for i=1:n
x(i)=input('enter values of x=');
y(i)=input('enter values of y=');
end
h=x(2)-x(1);
u=(xr-x(1))/h;
value=y(1);
for j=1:(n-1)
for i=1:(n-j)
if (j==1)
d(i,j)=y(i+1)-y(i);
else
d(i,j)=d((i+1),(j-1))-d(i,(j-1));
end
end
end
for i=1:(n-1)
prod=d(1,i);
for j=1:i
prod=prod*(u-(j-1))/j;
end
value=value+prod;

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

Numerical Methods and Optimization Techniques Page 42


Program on Forward Interpolation
enter total no. of points n=5
enter value of xr=25
enter values of x=10
enter values of y=0.1736
enter values of x=20
enter values of y=0.3420
enter values of x=30
enter values of y=0.5
enter values of x=40
enter values of y=0.6425
enter values of x=50
enter values of y=0.766
0.17 -0.01 -0.01 0.00
0.16 -0.02 -0.00
0.14 -0.02
0.12
Ans by program is =0.422656
solver =

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);

Numerical Methods and Optimization Techniques Page 43


3. Inverse interpolation

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

Program on Inverse Lagranges Interpolation


enter total no of points n=5
enter value of yr=810
enter values of x=5
enter values of y=150
enter values of x=7
enter values of y=392
enter values of x=11
enter values of y=1452
enter values of x=13
enter values of y=2366
enter values of x=17
enter values of y=5202
the value of y=9.233106
solver =

Numerical Methods and Optimization Techniques Page 44


9.2391

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);

Numerical Methods and Optimization Techniques Page 45


Assignment 6
Programm on ODE
1.Eulers method
Find solution subjected to B.C.Y=1 @ x=0,h=0.1
%Program for Eulers Method
clc
clear all
fprintf('\nProgram for Eulers Method');
f=@(x,y)(-1000/(0.5*(1+0.01*y)))
% f=@(x,y)(1+x*y)
% f=@(x,y)((x+y)/(y*y-sqrt(x*y)))
% f=@(x,y)(3*x*x+1)
%f=@(x,y)(-2*x^3+12*x^2-20*x+8.5)
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)
%fprintf('\n%d\t%0.4f\t%0.4f',itr,x0,y0);
y1=y0+h*f(x0,y0);
x0=x0+h;
y0=y1;
fprintf('\n%d\t%0.4f\t%0.4f',itr,x0,y0);
itr=itr+1;
end
fprintf('\nSolution by Eulers Method=%8.4f',y1);

output

Program for Eulers Method


f=

@(x,y)(1+x*y)

Enter the value of x0=0

Enter the value of y0=1

Enter the value of xn=0.5

Enter the value of h=0.1

Numerical Methods and Optimization Techniques Page 46


1 0.10001.1000
2 0.20001.2110
3 0.30001.3352
4 0.40001.4753
5 0.50001.6343
Solution by Eulers Method= 1.6343
x=

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)

Numerical Methods and Optimization Techniques Page 47


2.RK 4th
Evaluate subjected to y=2 @ x=0,h=0.1

%Program for Runge Kutta 4th order Method

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

Program for Runge Kutta 4th order Method


f=

@(x,y)(y-x)

Enter the value of x0=0

Enter the value of y0=2

Enter the value of xn=0.2

Enter the value of h=0.1

Numerical Methods and Optimization Techniques Page 48


1 0.10002.2052
2 0.20002.4214
Solution by Runge Kutta 4th order Method= 2.4214
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)

Numerical Methods and Optimization Techniques Page 49


3. Simultaneous eqn RK 2nd order

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

%Program for Simultaneus Runge Kutta 2nd order Method


clc
clear all
fprintf('\nProgram for Simultaneus Runge Kutta 2nd order Method');
f=@(x,y,z)(1.5*z-4.5*y+4.5)
g=@(x,y,z)(x+z)
% f=@(x,y,z)(1.5*x*z-4.5*y+4.5)
% g=@(x,y,z)(z)
x0=input('\nEnter the value of x0=');
y0=input('\nEnter the value of y0=');
z0=input('\nEnter the value of z0=');
xn=input('\nEnter the value of xn=');
h=input('\nEnter the value of h=');
itr=1;
while(x0<xn)
kz1=h*f(x0,y0,z0);
ky1=h*g(x0,y0,z0);
kz2=h*f(x0+h,y0+ky1,z0+kz1);
ky2=h*g(x0+h,y0+ky1,z0+kz1);
z1=z0+((kz1+kz2)/2);
y1=y0+((ky1+ky2)/2);
x0=x0+h;
z0=z1;
y0=y1;
fprintf('\n%d\t%0.4f\t%0.4f\t%0.4f',itr,x0,y0,z0);
itr=itr+1;
end
fprintf('\nSolution by Simultaneus Runge Kutta 2nd order Method');
fprintf('\nx0\ty0\tz0');
fprintf('\n%8.4f\t%8.4f\t%8.4f\t',x0,y0,z0);

output

Program for Simultaneus Runge Kutta 2nd order Method


f=

@(x,y,z)(1.5*z-4.5*y+4.5)

Numerical Methods and Optimization Techniques Page 50


g=

@(x,y,z)(x+z)

Enter the value of x0=0

Enter the value of y0=0

Enter the value of z0=0

Enter the value of xn=0.2

Enter the value of h=0.1

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 >>

Numerical Methods and Optimization Techniques Page 51

You might also like