Matlab program for method of Bisection Method when no.
of Iterations is given
PROGRAM
f = inline('(-0.9*x*x)+(1.7*x)+2.5');
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=');
y1=f(x1);
y2=f(x2);
while ((y1*y2)>0)
x1=input('Enter the value of initial guess x1 again=');
x2=input('Enter the value of initial guess x2 again=');
y1=f(x1);
y2=f(x2);
end
for i=1:n
x3=((x1+x2)/2);
y3=f(x3);
if ((y1*y3)<0)
x2=x3;
y2=y3;
else
x1=x3;
y1=y3;
end
fprintf('The root x3=%f\n',x3);
end
OUTPUT
Enter the value of initial guess x1= 2.8
Enter the value of initial guess x2= 3
Enter the number of iterations= 3
The root x3= 2.875000
Matlab program for method of Successive Approximation Method when no. of
Iterations is given
PROGRAM
clc;
g = inline('((x*x*x)+8)/15');
dg = inline('(x*x)/5');
x1=input('Enter the value of initial guess x1= ');
n=input('\n Enter the otal no of iteration to be performed= ');
dg(x1);
while(abs(dg(x1))>1)
fprintf('\n Enter value of x1 again= ');
dg(x1);
end
for i=1:1:n
x2=g(x1);
x1=x2;
end
fprintf('\n The root of given equation is = %f\n',x2);
OUTPUT
Enter the value of initial guess x1= 0
Enter the otal no of iteration to be performed= 5
The root of given equation is = 0.544070
Matlab program for method of Newton- Raphson Method when no. of Iterations is
given
PROGRAM
clc;
f = inline('x^3-5*x+3');
df = inline('3*x^2-5');
ddf = inline('6*x');
x1=input('Enter the value of initial guess x1= ');
n=input('\n Enter the total no. of iterations= ');
f(x1);
df(x1);
ddf(x1);
while(abs(f(x1)*ddf(x1))<0)
fprintf('\n Enter value of x1 again= ');
f(x1);
d(x1);
ddf(x1);
end
x2=x1-(f(x1)/df(x1));
for i=1:1:n
x1=x2;
f(x1);
df(x1);
x2=x1-(f(x1)/df(x1));
end
fprintf('\n The root of given equation is = %f\n',x2);
OUTPUT
Enter the value of initial guess x1= 0
Enter the total no. of iterations= 3
The root of given equation is = 0.656620
Matlab program for method of False Position Method when Accuracy is given
PROGRAM
clc;
f=inline('x*exp(x)-cos(3*x)-0.5');
x1=input('Enter the value of initial guess x1= ');
x2=input('Enter the value of initial guess x2= ');
acc=input('Enter the value accuracy=');
y1=f(x1);
y2=f(x2);
while((y1*y2)>0)
fprintf('Enter the value of initial guess x1 again= ');
fprintf('Enter the value of initial guess x2 again= ');
y1=f(x1);
y2=f(x2);
end
while ((abs(x1-x2))>acc)
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
x3=(((x2*y1)-(x1*y2))/(y1-y2));
fprintf('The root x3= %f',x3);
OUTPUT
Enter the value of initial guess x1= 0
Enter the value of initial guess x2= 1
Enter the value of accuracy= 0.01
The root of given equation is = 0.452547
Matlab program for method of Gauss Elimination Method
PROGRAM
clc;
n=input('Enter the given no. of equations i.e. equal to no. of variables=
');
for i=1:1:n
for j=1:1:n
a(i,j)=input('\nEnter matrix element row wise: ');
end
a(i,n+1)=input('\nEnter second matrix element: ');
x(i)=0;
end
for k=1:1:n
for i=k+1:1:n
ratio=a(i,k)/a(k,k);
for j=k+1:1:n+1
a(i,j)=a(i,j)-ratio*a(k,j);
end
end
for i=k+1:1:n
a(i,k)=0;
end
end
x(n)=a(n,n+1)/a(n,n);
for i=n-1:-1:1
coefsum=0;
for j=i+1:1:n
coefsum=coefsum+a(i,j)*x(j);
x(i)=(a(i,n+1)-coefsum)/a(i,i);
end
end
fprintf('\n \n The values of variables are= ');
for i=1:1:n
fprintf('\n %3.2f\n',x(i));
end
OUTPUT
Enter the given no. of equations i.e. equal to no. of variables= 3
Enter matrix element row wise: 2
Enter matrix element row wise: 4
Enter matrix element row wise: -6
Enter second matrix element: -4
Enter matrix element row wise: 1
Enter matrix element row wise: 5
Enter matrix element row wise: 3
Enter second matrix element: 10
Enter matrix element row wise: 1
Enter matrix element row wise: 3
Enter matrix element row wise: 2
Enter second matrix element: 5
The values of variables are=
-3.00
2.00
1.00
Matlab program for method of Gauss Seidal Method
PROGRAM
n=input('\n Enter the number 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('\n Enter second matrix elements: ');
end
n1=input('Enter the number 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('x%d=%f',i,x(i));
end
fprintf('\n\n');
end
OUTPUT
Enter the number of equations: 3
Enter matrix elements row-wise: 7
Enter matrix elements row-wise: 20
Enter matrix elements row-wise: 3
Enter matrix elements row-wise: 23
Enter matrix elements row-wise: -11
Enter matrix elements row-wise: 7
Enter matrix elements row-wise: 10
Enter matrix elements row-wise: 13
Enter matrix elements row-wise: 22
Enter second matrix elements: 111
Enter second matrix elements: 161.5
Enter second matrix elements: 190.5
Enter the number of iterations: 6
x1=7.021739x2=3.092391x3=3.640069
x1=7.392862x2=2.416488x3=3.870774
x1=6.999389x2=2.519598x3=3.988697
x1=7.012813x2=2.497211x3=3.995824
x1=6.999937x2=2.500648x3=3.999645
x1=7.000418x2=2.499907x3=3.999865
Matlab program for method of Thomas Algorithm Method
PROGRAM
clc;
n=input('\nEnter number of element n: ');
for i=1:n
for j=1:n
ar(i,j)=input('\nEnter matrix element row wise: ');
end
end
for i=1:n
ar1(i)=input('\nEnter 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('\nx%d= %f',j,a(j));
end
OUTPUT
Enter number of element n: 3
Enter matrix element row wise: 1
Enter matrix element row wise: 2
Enter matrix element row wise: 0
Enter matrix element row wise: 2
Enter matrix element row wise: 3
Enter matrix element row wise: 1
Enter matrix element row wise: 0
Enter matrix element row wise: 2
Enter matrix element row wise: -1
Enter second matrix elements :3
Enter second matrix elements :4
Enter second matrix elements :1
x1= 5.000000
x2= -1.000000
x3= -3.000000
Matlab program for Curve Fitting of Straight Line Method
PROGRAM
function cf()
clc
n = input(' Enter the number of points: ');
for i = 1:1:n
x(i) = input(' Enter the value of x: ');
y(i) = input(' Enter the value of y: ');
end
sx = 0;
sy = 0;
sxx = 0;
sxy = 0;
for i = 1:1:n
sx = sx + x(i);
sy = sy + y(i);
sxx = sxx + (x(i)*x(i));
sxy = sxy + (x(i)*y(i));
end
m = ((n*sxy)-(sx*sy))/((n*sxx)-(sx^2));
c = (sy - m*sx)/n;
fprintf('\n The equation of the straight line is y = %f x + %f \n',
m,c);
end
OUTPUT
Enter the number of points: 7
Enter the value of x: 1
Enter the value of y: .5
Enter the value of x: 2
Enter the value of y: 2.5
Enter the value of x: 3
Enter the value of y: 2
Enter the value of x: 4
Enter the value of y: 4
Enter the value of x: 5
Enter the value of y: 3.5
Enter the value of x: 6
Enter the value of y: 6
Enter the value of x: 7
Enter the value of y: 5.5
The equation of the straight line is y = 0.839286 x + 0.071429
Matlab program for Curve Fitting of Power Equation Method
PROGRAM
clc;
n=input('\n Enter the value no of data entries,n=');
for i=1:1:n
x(i)=input('\n enter the values of x ');
y(i)=input('\n enter the values of y ');
Y(i)=log10(y(i));
X(i)=x(i);
end
sx=0;
sy=0;
sxx=0;
sxy=0;
for i=1:1:n
sx=sx+X(i);
sy=sy+Y(i);
sxx=sxx+X(i)*X(i);
sxy=sxy+X(i)*Y(i);
end
delta=sxx*n-sx*sx;
delta1=sxy*n-sy*sx;
delta2=sxx*sy-sx*sxy;
a1=delta1/delta;
b1=delta2/delta;
a=10^b1;
b=10^a1;
fprintf('\n The values of constants a1=%f and b1=%f',a1,b1);
fprintf('\n The values of constants a=%f and b=%f',a,b);
fprintf('\n The power equation is y=%2.4f*(%2.4f)^x',a,b);
for i=1:1:n
ya(i)=a*power(b,x(i));
end
OUTPUT
Enter the value no of data entries,n=5
enter the values of x 2
enter the values of y 144
enter the values of x 3
enter the values of y 172.8
enter the values of x 4
enter the values of y 207.4
enter the values of x 5
enter the values of y 248.8
enter the values of x 6
enter the values of y 298.5
The values of constants a1=0.079147 and b1=2.000114
The values of constants a=100.026209 and b=1.199905
The power equation is y=100.0262*(1.1999)^x
Matlab program for Curve Fitting of Exponential Equation Method
PROGRAM
function cf4()
clc
n = input(' Enter the number of points: ');
for i = 1:1:n
X(i) = input(' \n Enter the value of x: ');
Y(i) = input(' Enter the value of y: ');
x(i) = X(i);
y(i) = log(Y(i));
end
sx = 0;
sy = 0;
sxx = 0;
sxy = 0;
for i = 1:1:n
sx = sx + x(i);
sy = sy + y(i);
sxx = sxx + (x(i)*x(i));
sxy = sxy + (x(i)*y(i));
end
m = ((n*sxy)-(sx*sy))/((n*sxx)-(sx^2));
c = (sy - m*sx)/n;
a = exp(c);
b = m ;
fprintf('\n The equation of the curve is y = %f * exp(%f *x) \n',
a,b);
end
OUTPUT
Enter the number of points: 4
Enter the value of x: 0
Enter the value of y: 2
Enter the value of x: 1
Enter the value of y: 2.2103
Enter the value of x: 2
Enter the value of y: 2.4428
Enter the value of x: 3
Enter the value of y: 2.6997
The equation of the curve is y = 1.999987 * exp(0.100000 *x)
Matlab program for Curve Fitting of 2nd Order Equation Method
PROGRAM
clc;
n=input('Enter the value no. of data entries n=');
for i=1:1:n
x(i)=input('\n Enter the values of x: ');
y(i)=input('Enter the values of y: ');
end
sx=0;
sy=0;
sxx=0;
sxxx=0;
sxxxx=0;
sxy=0;
sxxy=0;
for i=1:1:n
sx=sx+x(i);
sy=sy+y(i);
sxx=sxx+x(i)*x(i);
sxxx=sxxx+x(i)*x(i)*x(i);
sxxxx=sxxxx+x(i)*x(i)*x(i)*x(i);
sxy=sxy+x(i)*y(i);
sxxy=sxxy+x(i)*x(i)*y(i);
end
delta=sxxxx*(sxx*n-sx*sx)-sxxx*(sxxx*n-sxx*sx)+sxx*(sxxx*sx-sxx*sxx);
delta1=sxxy*(sxx*n-sx*sx)-sxxx*(sxy*n-sy*sx)+sxx*(sxy*sx-sy*sxx);
delta2=sxxxx*(sxy*n-sy*sx)-sxxy*(sxxx*n-sxx*sx)+sxx*(sxxx*sy-sxx*sxy);
delta3=sxxxx*(sxx*sy-sx*sxy)-sxxx*(sxxx*sy-sxx*sxy)+sxxy*(sxxx*sx-sxx*sxx);
a=delta1/delta;
b=delta2/delta;
c=delta3/delta;
fprintf('\n The values of constants a= %f ,b= %f and c= %f',a,b,c);
fprintf('\n The equation of second degree curve is y=
%2.3fx^2+%2.3fx+%2.3f',a,b,c);
for i=1:1:n
ya(i)=a*x(i)*x(i)+b*x(i)+c;
end
OUTPUT
Enter the value no. of data entries n=7
Enter the values of x: -3
Enter the values of y: 12
Enter the values of x: -2
Enter the values of y: 4
Enter the values of x: -1
Enter the values of y: 1
Enter the values of x: 0
Enter the values of y: 2
Enter the values of x: 1
Enter the values of y: 7
Enter the values of x: 2
Enter the values of y: 15
Enter the values of x: 3
Enter the values of y: 30
The values of constants a= 2.119048 ,b= 2.928571 and c= 1.666667
The equation of second degree curve is y= 2.119x^2+2.929x+1.66
Matlab program for Lagrange’s Interpolation Method
PROGRAM
clc;
n=input('\n Enter the no. of data entries( data points)=');
for i=1:1:n
x(i)=input('\n Enter the given value of x ordinate=');
y(i)=input('\n Enter the given value of y ordinate=');
end
xg=input('\n Enter the value of xg at which value of function is to be
interpolated=');
yr=0;
for k=1:1:n
num=1;
den=1;
for i=1:1:n
if i~=k
num=num*(xg-x(i));
den=den*(x(k)-x(i));
end
end
yr=yr+((num/den)*y(k));
end
fprintf('\n The value of yg at xg=%f is =%f',xg,yr);
OUTPUT
Enter the no. of data entries( data points)=4
Enter the given value of x ordinate=0
Enter the given value of y ordinate=2
Enter the given value of x ordinate=1
Enter the given value of y ordinate=3
Enter the given value of x ordinate=2
Enter the given value of y ordinate=12
Enter the given value of x ordinate=5
Enter the given value of y ordinate=147
Enter the value of xg at which value of function is to be interpolated=1.5
The value of yg at xg=1.500000 is =6.125000
Matlab program for Newton’s Forward Difference Method
PROGRAM
clc;
n=input('\n Enter the no. of data entries( data points)=');
for i=1:1:n
x(i)=input('\n Enter the given value of x ordinate=');
y(i,1)=input('\n Enter the given value of y ordinate=');
end
xr=input('\n Enter the value of xr at which value of function is to be
interpolated=');
k=n;
for j=2:1:n
k=k-1;
for i=1:1:k
y(i,j)=y(i+1,j-1)-y(i,j-1);
end
end
k=n;
fprintf('\n The Newton’s forward difference table is as follows');
for i=1:1:n
fprintf('\n %3.3f',x(i));
for j=1:1:k
fprintf('\t %3.3f',y(i,j));
end
k=k-1;
end
h=x(2)-x(1);
r=(xr-x(1))/h;
fprintf('\n The value h=%f',h);
fprintf('\n The value of r=%f',r);
sum=0;
for t=2:1:n
fy=1;
facto=1;
for m=0:1:t-2
fy=fy*(r-m);
facto=facto*(m+1);
end
fy=(fy/facto)*y(1,t);
sum=sum+fy;
end
yr=sum+y(1,1);
fprintf('\n The value of y=yr at x=xr=%f is yr=%f',xr,yr);
OUTPUT
Enter the no. of data entries( data points)=8
Enter the given value of x ordinate=2
Enter the given value of y ordinate=19
Enter the given value of x ordinate=3
Enter the given value of y ordinate=48
Enter the given value of x ordinate=4
Enter the given value of y ordinate=99
Enter the given value of x ordinate=5
Enter the given value of y ordinate=178
Enter the given value of x ordinate=6
Enter the given value of y ordinate=291
Enter the given value of x ordinate=7
Enter the given value of y ordinate=444
Enter the given value of x ordinate=8
Enter the given value of y ordinate=643
Enter the given value of x ordinate=9
Enter the given value of y ordinate=894
Enter the value of xr at which value of function is to be interpolated=3.5
The Newton’s forward difference table is as follows
2.000 19.000 29.000 22.000 6.000 0.000
0.000 0.000 0.000
3.000 48.000 51.000 28.000 6.000 0.000
0.000 0.000
4.000 99.000 79.000 34.000 6.000 0.000
0.000
5.000 178.000 113.000 40.000 6.000 0.000
6.000 291.000 153.000 46.000 6.000
7.000 444.000 199.000 52.000
8.000 643.000 251.000
9.000 894.000
The value h=1.000000
The value of r=1.500000
The value of y=yr at x=xr=3.500000 is yr=70.375000
Matlab program for Inverse Interpolation Method
PROGRAM
clc;
n=input('\n Enter the no. of data entries( data points)=');
for i=1:1:n
x(i)=input('\n Enter the given value of x ordinate=');
y(i)=input('\n Enter the given value of y ordinate=');
end
yr=input('\n Enter the value of yr at which value of xr is to be
interpolated=');
xr=0;
for k=1:1:n
num=1;
den=1;
for i=1:1:n
if i~=k
num=num*(yr-y(i));
den=den*(y(k)-y(i));
end
end
xr=xr+((num/den)*x(k));
end
fprintf('\n The value of xr at y=yr is =%f',xr);
OUTPUT
Enter the no. of data entries( data points)=4
Enter the given value of x ordinate=0
Enter the given value of y ordinate=0
Enter the given value of x ordinate=1
Enter the given value of y ordinate=1
Enter the given value of x ordinate=2
Enter the given value of y ordinate=7
Enter the given value of x ordinate=3
Enter the given value of y ordinate=25
Enter the value of yr at which value of xr is to be interpolated=2
The value of xr at y=yr is =1.716138
Matlab program for Trapezoidal Double Integration
PROGRAM
clc;
f=inline('(x+y)');
x0=input('\n Enter the lower limit of integral x0 ');
xn=input('\n Enter the upper limit of integral xn ');
y0=input('\n Enter the lower limit of integral y0 ');
ym=input('\n Enter the upper limit of integral ym ');
h=input('\n Enter the strip width in X-direction ');
k=input('\n Enter the strip width in Y-direction ');
n=(xn-x0)/h;
m=(ym-y0)/k;
fprintf('\n The no. of strips in X-direction are=%d',n);
fprintf('\n The no. of strips in Y-direction are=%d',m);
volume=0;
for i=1:1:n
xi=x0+((i-1)*h);
for j=1:1:m
yj=y0+((j-1)*k);
f00=f(xi,yj);
f01=f(xi,yj+k);
f10=f(xi+h,yj);
f11=f(xi+h,yj+k);
volume1=((h*k)/4)*(f00+f01+f10+f11);
volume=volume+volume1;
end
end
fprintf('\n The total volume under the curve is=%f',volume);
OUTPUT
Enter the lower limit of integral x0 1
Enter the upper limit of integral xn 3
Enter the lower limit of integral y0 0
Enter the upper limit of integral ym 2
Enter the strip width in X-direction 2
Enter the strip width in Y-direction 2
The no. of strips in X-direction are=1
The no. of strips in Y-direction are=1
The total volume under the curve is=12.000000
Matlab program for Trapezoidal Single Integration
PROGRAM
clc;
f=inline('4*x+2');
x0=input('\n Enter the value of first limit= ');
xn=input('\n Enter the value of second limit= ');
h=input('\n Enter the value of width of strip= ');
n=(xn-x0)/h;
fprintf('\n The total no. of strips are= %d',n);
area=0;
for i=1:1:n
xi=x0+(i-1)*h
y0=f(xi);
y1=f(xi+h);
area1=(h/2)*(y0+y1);
area=area+area1;
end
fprintf('\n The area is= %f',area);
OUTPUT
Enter the value of first limit= 1
Enter the value of second limit= 4
Enter the value of width of strip= .5
The total no. of strips are= 6
xi =1
xi =1.5000
xi =2
xi =2.5000
xi =3
xi =3.5000
The area is= 36.000000
Matlab program for Simpson’s 1/3 rd Single Integration
PROGRAM
clc;
f=inline('exp(x)');
x0=input('\n Enter the value of first limit= ');
xn=input('\n Enter the value of second limit= ');
h=input('\n Enter the value of strip width ');
n=(xn-x0)/h;
fprintf('\n The no of strips are = %f',h);
area=0;
for i=2:2:n
xi=x0+((i-2)*h)
y0=f(xi);
y1=f(xi+h);
y2=f(xi+(2*h));
area1=(h/3)*(y0+(4*y1)+y2);
area=area+area1;
end
fprintf('\n The area= %f',area);
OUTPUT
Enter the value of first limit= 0
Enter the value of second limit= 4
Enter the value of strip width h= 1
The no of strips are = 1.000000
xi =0
xi =2
The area= 53.863846
Matlab program for Gauss Legendre 3 point Formula
PROGRAM
clc;
f=inline('x^2-5*x+2');
x0=input('\n Enter the lower limit of integral x0= ');
xn=input('\n Enter the upper limit of integral 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/9)*(y1+y2))+((8/9)*y3))*c;
fprintf('\n The total area under the given function is= %f',area);
OUTPUT
Enter the lower limit of integral x0= 3
Enter the upper limit of integral xn= 5
The total area under the given function is= -3.333333
Matlab program for Euler’s Method
PROGRAM
clc;
f=inline('x-y^2');
x0=input('\n Enter the given value of x0= ');
y0=input('\n Enter the given value of y0= ');
h=input('\n Enter the given value of step size h= ');
xn=input('\n Enter the given value of xn at which yn is to be calculated=
');
n=(xn-x0)/h;
fprintf('\n The total no. of steps are= %d',n);
x(1)=x0;
y(1)=y0;
fprintf('\n\t X \t\t\ty ');
for i=1:1:n
x(i+1)=x0+i*h;
y(i+1)=y(i)+h*f(x(i),y(i));
fprintf('\n%f \t%f \t%f',x(i+1),y(i+1));
end
OUTPUT
Enter the given value of x0= 0
Enter the given value of y0= 1
Enter the given value of step size h= 1
Enter the given value of xn at which yn is to be calculated= 4
The total no. of steps are= 4
X y
1.000000 0.000000
2.000000 1.000000
3.000000 2.000000
4.000000 1.000000
Matlab program for Runge-Kutta 4th
PROGRAM
clc;
f=inline('(x^2+y^2)');
x0=input('\n Enter the given value of x0=');
y0=input('\n Enter the given value of y0=');
h=input('\n Enter the given value of step size h=');
xn=input('\n Enter the given value of xn at which yn is to be
calculated=');
n=(xn-x0)/h;
fprintf('\n The total no. of steps are=%d',n);
x(1)=x0;
y(1)=y0;
fprintf('\n\t X \t\t\ty ');
for i=1:1:n
x(i+1)=x0+i*h;
m1=f(x(i),y(i));
m2=f(x(i)+(h/2),y(i)+((h*m1)/2));
m3=f(x(i)+(h/2),y(i)+((h*m2)/2));
m4=f(x(i)+h,y(i)+(h*m3));
y(i+1)=y(i)+((h/6)*(m1+2*m2+2*m3+m4));
fprintf('\n%f \t%f \t%f',x(i+1),y(i+1));
end
xspan=[x0:h:xn];
[xe,ye]=ode45(f,xspan,y0);
OUTPUT
Enter the given value of x0=0
Enter the given value of y0=0
Enter the given value of step size h=0.2
Enter the given value of xn at which yn is to be calculated=0.4
The total no. of steps are=2
X y
0.200000 0.002667
0.400000 0.0213
Assignment No. 7
Title:- Matlab Syntax
1. Matlab Syntax for Roots of Equations
Eg.1
X=fzero(inline('cos(x)-(1.3*x)'),0);
X =0.624
Eg.2
X=fzero(inline('x*exp(x)-cos(3*x)-0.5'),0);
X
X =0.4525
2. Matlab Syntax for Simultaneous Equations
Eg.1
A=[5 -2 1 -3;1 -10 -2 -1;3 -3 10 1;2 1 -3 5];
B=[5;-8;-29;31];
x=A\B;
x
x =
3.7106
1.7001
-3.7177
2.1451
Eg.2
A=[4 1 1;1 6 2;-1 -2 -5];
B=[5;19;10];
x=A\B;
x
x =
1.1649
4.2887
-3.9485
3. Matlab Syntax for Numerical Integration
Eg.1
Q = quadl('1./(1+(x.^2))',0,1);
Q
Q =0.7854
Eg.2
Q=dblquad(inline('(max(5+(x.^2+y.^2),0))'),0,2,0,2);
Q
Q =30.6667
4. Matlab Syntax for Curve Fitting
Eg.1
x=[0 2 4 6 8 12 20];
y=[10 12 18 22 20 30 30];
z=polyfit(x,y,1);
a=z(1);
b=z(2);
a
b
a =1.0556
b =12.4444
Eg.2
x=[1 2 3 4 5];
y=[1.4 1.6 2 2.4 2.5];
z=polyfit(x,y,2);
a=z(1);
b=z(2);
c=z(3);
a
b
c
a = -0.0143
b =0.3857
c =0.9800
5. Matlab Syntax for Interpolation
Eg.1
x=[50 51 52 53 54];
y=[39.1961 39.7981 40.3942 40.9843 41.5687];
yr=interp1(x,y,50.5);
yr
yr =39.4971
6. Matlab Syntax for Numerical Solutions of Differential Equations
Eg.1
f=inline('x+(2*y)');
x0=1;
xn=1.4;
y0=1;
h=0.1;
xspan=[x0:h:xn];
[xe,ye]=ode45(f,xspan,y0);
[xe,ye]
ans =
1.0000 1.0000
1.1000 1.3375
1.2000 1.7607
1.3000 2.2887
1.4000 2.9447
Eg.2
f=inline('(x*x)/(2*y)');
x0=0;
xn=0.4;
y0=1.2;
h=0.4;
xspan=[x0:h:xn];
[xe,ye]=ode45(f,xspan,y0);
[xe,ye]
ans =
0.4000 1.2089