ROOTS OF EQUATIONS

advertisement
ROOTS OF EQUATIONS
A. BRACKETING METHOD
1. BISECTION METHOD
THE CODE :
% this program use bisection method to find roots
% programmed by ramy salem
% you have to enter two valuse xl and xr which contain the
root
clc
clear all
fun= input('Enter the function =','s');
xl=input('Enter the lower limit =');
xu=input('Enter the upper limit =');
maxi=input('Enter the max. number of iterations:');
es=input('Enter the prespecified error(%) =');
x=xl;
yl=eval(fun);
x=xu;
yu=eval(fun);
if yl*yu<0
i=0;
ea=1.1*es;
fprintf('\n\n\n\n\n\n\n==================================')
fprintf('\nitr#\txr\t\t f(xr)\t\t Ea')
fprintf('\n==================================')
while i<maxi & ea>es
xr=(xu+xl)/2;
i=i+1;
if (xl+xu)~=0
ea=abs((xu-xl)/(xu+xl))*100;
end
x=xl;
yl=eval(fun);
x=xr;
yr=eval(fun);
test=yl*yr;
if test ==0
ea=0;
elseif test<0
xu=xr;
else
xl=xr;
end
fprintf('\n%0.0f %10.3f %10.3f %10.3f',i,xr,yr,ea)
end
fprintf('\n\nTHE ROOT IS %10.3f \n ',xr)
else
fprintf('\n \b There is no roots in the interval or odd
num. of roots')
end
THE RESULT :
2. FALSE POSITION METHOD :
THE CODE :
% this program use false position method to find roots
% programmed by ramy salem
% you have to enter two valuse xl and xr which contain the
root
clc
clear all
fun= input('Enter the function =','s');
xl=input('Enter the lower limit =');
xu=input('Enter the upper limit =');
maxi=input('Enter the max. number of iterations:');
es=input('Enter the prespecified error(%) =');
x=xl;
yl=eval(fun);
x=xu;
yu=eval(fun);
if yl*yu<0
i=0;
ea=1.1*es;
ea=1.1*es;
fprintf('\n\n\n\n\n\n\n==================================')
fprintf('\nitr#\txr\t\t f(xr)\t\t Ea')
fprintf('\n==================================')
while i<maxi & ea>es
x=xl; yl = eval(fun);
x=xu; yu=eval(fun);
xr=xu-((yu*(xl-xu))/(yl-yu));
i=i+1;
if xr~=0& i~=1
ea=abs((xr-xro)/xr)*100;
end
%x=xl;
%yl=eval(fun);
x=xr;
yr=eval(fun);
test=yl*yr;
if test ==0
ea=0;
elseif test<0
xu=xr;
xro=xr;
else
xl=xr;
xro=xr;
end
fprintf('\n%0.0f %10.3f %10.3f %10.3f',i,xr,yr,ea)
end
fprintf('\n\nTHE ROOT IS %10.3f \n\n ',xr)
else
fprintf('\n \b There is no roots inthe interval or odd
num. of roots')
end
THE RESULT :
B. OPEN METHOD
1- SIMPLE FIXED PONT ITERATION:
THE CODE:
% this program use simple fixed point method to find roots
% programmed by ramy salem
% you have to enter two valuse xo and the fun
clc
clear all
fun= input('Enter the function (x=g(x)) =','s');
xi=input('Enter the lower limit =');
maxi=input('Enter the max. number of iterations:');
es=input('Enter the prespecified error(%) =');
i=0;
ea=1.1*es;
fprintf('\n\n\n\n\n\n\n==================================')
fprintf('\nitr#\txr\t\t f(xr)\t\t Ea')
fprintf('\n==================================')
while (ea>es)&(i<maxi)
x=xi;
y= eval(fun);
xn=y;
i=i+1;
if xn~=0
ea=abs((xn-x)/xn)*100;
end
fprintf('\n%0.0f %10.6f %10.6f %10.6f ',i,xi,y,ea)
xi=xn;
end
fprintf('\n\n the root is %g \n the number of iterations =
%g \n the approx. error = %g \n\n ',xn,i,ea)
THE RESULT :
2- THE NEWTON RAMPHSON METHOD :
THE CODE :
% this program use simple fixed point method to find roots
% programmed by ramy salem
% you have to enter two valuse xo and the fun
clc
clear all
fun=input (' enter the fun =','s');
fun1=input (' enter div =','s');
xi=input('Enter the lower limit =');
maxi=input('Enter the max. number of iterations:');
es=input('Enter the prespecified error(%) =');
i=0;
ea=1.1*es;
fprintf('\n\n\n\n\n\n\n============================')
fprintf('\nitr#\txr\t\t Ea \t\t
')
fprintf('\n=============================')
while (ea>es)&(i<maxi)
x=xi;
y=eval(fun);
y1=eval(fun1);
xr= xi-(y/y1);
i=i+1;
if xr~=0
ea=abs((xr-x)/xr)*100;
end
fprintf('\n%0.0f %10.3f
xi=xr;
end
%10.3f \n\n ',i,xr,ea)
THE RESULT :
3-THE SECANT METHOD :
THE CODE :
% this program use simple fixed point method to find roots
% programmed by ramy salem
% you have to enter two valuse xo and the fun
clc
clear all
fun= input('Enter the function (x=g(x)) =','s');
xo=input(' enter the first =');
x1= input (' enter the second');
maxi=input('Enter the max. number of iterations:');
es=input('Enter the prespecified error(%) =');
i=0;
ea=1.1*es;
fprintf('\n\n\n\n\n\n\n====================================
===============================================')
fprintf('\nitr#\tx(i-1)\t\t f(xi-1)\t\txi \t\tf(xi)\t\t
xi+1\t\t Ea')
fprintf('\n================================================
===============================')
while (ea>es)&(i<maxi)
x=xo;
yo=eval(fun);
fprintf('\n%0.0f\t %10.3f\t %10.3f ',i,x,yo)
x=x1;
y1=eval(fun);
fprintf('%10.3f\t %10.3f ',x,y1)
xn= x1-(y1*(xo-x1))/(yo-y1);
fprintf('\t%10.6f ',xn)
i=i+1;
if xn~=0
ea=abs((xn-x)/xn)*100;
end
fprintf('%10.2f ',ea)
xo=x1;
x1=xn;
end
fprintf('\n\nTHE ROOT IS %10.3f',xn)
THE RESULT :
Download