Professional Documents
Culture Documents
Document 4
Document 4
Document 4
Changunarayan, Bhaktapur
Report on
Lab sheet 2
To calculate the root of given linear equations using
bisection method and false position method
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
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