Matlab Program For Method of Bisection Method When No. of Iterations Is Given
Matlab Program For Method of Bisection Method When No. of Iterations Is Given
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
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
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
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
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
2.00
1.00
Matlab program for method of Gauss Seidal Method
PROGRAM
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
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
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
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
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
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
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
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
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
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
xi =1
xi =1.5000
xi =2
xi =2.5000
xi =3
xi =3.5000
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
xi =0
xi =2
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
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
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
X y
0.200000 0.002667
0.400000 0.0213
Assignment No. 7
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
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
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
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