Document 4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Nepal engineering college

(Affiliated To Pokhara university)

Changunarayan, Bhaktapur

Report on

Lab sheet 2
To calculate the root of given linear equations using
bisection method and false position method

Submitted By: Submitted To :


Name: Jay Prakash Mahato department of electrical
CRN : 018-606
Date : 2078-01-30
Editor window

clc;
clear all;
close all;
xmin=input('enter the minimum value of function');
xmax=input('enter the maximum value of function');
x=[xmin:xmax];
itr=1;
y=x.*x.*x-x-11;
plot(x,y);
grid on;
sn=1:length(x);
Table (1,:)=sn;
table (2,:)=x(1,:);
table(3,:)=y(1,:);
table=table'
x1=input('please input first approximation');
x2=input('please input second approximation');
error=abs(x1-x2);
while error>0.0001
xm=(x1+x2)/2;
table_re(itr,1)=itr;
table_re(itr,2)=x1;
table_re(itr,3)=fn(x1);
table_re(itr,4)=x2;
table_re(itr,5)=fn(x2);
table_re(itr,6)=xm;
table_re(itr,7)=fn(xm);
table_re(itr,8)=error;
if fn(xm)==0
display('got it')
elseif fn(xm).*fn(x1)<0
x2=xm;
else
x1=xm;
end
error=abs(x1-x2);
itr=itr+1;
end
table_re
fprintf('\nthe root=%f\n',xm);

Editor window

function re=fn(x)
re=x.*x.*x-x-11;
end
Command window
enter the minimum value of function -5
enter the maximum value of function 5

Table:-

1 -5 -131
2 -4 -71
3 -3 -35
4 -2 -17
5 -1 -11
6 0 -11
7 1 -11
8 2 -5
9 3 13
10 4 49
11 5 109

please input first approximation 2


please input second approximation 3
table_re =

1.0000 2.0000 -5.0000 3.0000 13.0000 2.5000 2.1250 1.0000


2.0000 2.0000 -5.0000 2.5000 2.1250 2.2500 -1.8594 0.5000
3.0000 2.2500 -1.8594 2.5000 2.1250 2.3750 0.0215 0.2500
4.0000 2.2500 -1.8594 2.3750 0.0215 2.3125 -0.9460 0.1250
5.0000 2.3125 -0.9460 2.3750 0.0215 2.3438 -0.4691 0.0625
6.0000 2.3438 -0.4691 2.3750 0.0215 2.3594 -0.2256 0.0313
7.0000 2.3594 -0.2256 2.3750 0.0215 2.3672 -0.1025 0.0156
8.0000 2.3672 -0.1025 2.3750 0.0215 2.3711 -0.0406 0.0078
9.0000 2.3711 -0.0406 2.3750 0.0215 2.3730 -0.0096 0.0039
10.0000 2.3730 -0.0096 2.3750 0.0215 2.3740 0.0059 0.0020
11.0000 2.3730 -0.0096 2.3740 0.0059 2.3735 -0.0018 0.0010
12.0000 2.3735 -0.0018 2.3740 0.0059 2.3738 0.0021 0.0005
13.0000 2.3735 -0.0018 2.3738 0.0021 2.3737 0.0001 0.0002
14.0000 2.3735 -0.0018 2.3737 0.0001 2.3736 -0.0009 0.0001

xm =
the root= 2.3736
Editor window:
clc;
clear all;
close all;
xmin=input('enter the minimum value of function');
xmax=input('enter the maximum value of function');
x=[xmin:xmax];
itr=1;
y=x.*x.*x+4.*x-1;
plot(x,y);
grid on;
sn=1:length(x)
table(1,:)=sn;
table(2,:)=x(1,:);
table(3,:)=y(1,:);
table=table'
x1=input('please input first approximation');
x2=input('please input second approximation');
error=abs(x2-x1);
while(error>0.0001)
xm=(x1.*fn(x2)-x2.*fn(x1))/(fn(x2)-fn(x1));
table_re(itr,1)=itr;
table_re(itr,2)=x1;
table_re(itr,3)=fn(x1);
table_re(itr,4)=x2;
table_re(itr,5)=fn(x2);
table_re(itr,6)=xm;
table_re(itr,7)=fn(xm);
table_re(itr,8)=error;
if fn(xm)==0
break
elseif fn(xm)*fn(x1)<0
x2=xm;
else
x1=xm;
end
error=abs(x2-x1);
itr=itr+1;
end
table_re
fprintf('\nthe root=%f\n',xm);

Editor window:

function re=fn(x)
re=x.*x-4.*x-10;
end
Command window:
enter the minimum value of function -5
enter the maximum value of function 5
sn =
1 2 3 4 5 6 7 8 9 10 11
table =
1 -5 -146
2 -4 -81
3 -3 -40
4 -2 -17
5 -1 -6
6 0 -1
7 1 4
8 2 15
9 3 38
10 4 79
11 5 144

please input first approximation 0


please input second approximation 1
table_re =
1.0000 0 -1.0000 1.0000 4.0000 0.200 -0.1920 1.0000
2.0000 0.2000 -0.1920 1.0000 4.0000 0.2366 -0.0402 0.8000
3.0000 0.2366 -0.0402 1.0000 4.0000 0.2442 -0.0085 0.7634
4.0000 0.2442 -0.0085 1.0000 4.0000 0.2458 -0.0018 0.7558
5.0000 0.2458 -0.0018 1.0000 4.0000 0.2462 -0.0004 0.7542
6.0000 0.2462 -0.0004 1.0000 4.0000 0.2462 -0.0001 0.7538
7.0000 0.2462 -0.0001 1.0000 4.0000 0.2463 -0.0000 0.7538
8.0000 0.2463 -0.0000 1.0000 4.0000 0.2463 -0.0000 0.7537
9.0000 0.2463 -0.0000 1.0000 4.0000 0.2463 -0.0000 0.7537
10.0000 0.2463 -0.0000 1.0000 4.0000 0.2463 -0.0000 0.7537
11.0000 0.2463 -0.0000 1.0000 4.0000 0.2463 -0.0000 0.7537
12.0000 0.2463 -0.0000 1.0000 4.0000 0.2463 -0.0000 0.7537
13.0000 0.2463 -0.0000 1.0000 4.0000 0.2463 -0.0000 0.7537
14.0000 0.2463 -0.0000 1.0000 4.0000 0.2463 -0.0000 0.7537
15.0000 0.2463 -0.0000 1.0000 4.0000 0.2463 -0.0000 0.7537
16.0000 0.2463 -0.0000 1.0000 4.0000 0.2463 -0.0000 0.7537
17.0000 0.2463 -0.0000 1.0000 4.0000 0.2463 -0.0000 0.7537
18.0000 0.2463 -0.0000 1.0000 4.0000 0.2463 -0.0000 0.7537
19.0000 0.2463 -0.0000 1.0000 4.0000 0.2463 -0.0000 0.7537
20.0000 0.2463 -0.0000 1.0000 4.0000 0.2463 -0.0000 0.7537
21.0000 0.2463 -0.0000 1.0000 4.0000 0.2463 -0.0000 0.7537
22.0000 0.2463 -0.0000 1.0000 4.0000 0.2463 -0.0000 0.7537
23.0000 0.2463 -0.0000 1.0000 4.0000 0.2463 -0.0000 0.7537
24.0000 0.2463 -0.0000 1.0000 4.0000 0.2463 0 0.7537
the root=0.246266

You might also like