You are on page 1of 3

INCREMENTAL SEARCH

f=input('give the input function. example:to givef(x)=2x+5,


write this way: @(x)2*x+5 \n')
xmax=input('give the value of xmax\n')
xmin=input('give the value of xmin \n')
n=input('give the value of number of division n \n')
x=linspace(xmin,xmax,n);
y=f(x);
fprintf('There must be a root in between the two column values
of x \n')
for k=1:length(x)-1
if sign(y(k))~=sign(y(k+1))
fprintf('%f %f \n',[x(k);x(k+1)])
end
end

BISECTION METHOD

f=input('give your input function to get root of.example:to


give f(x)=2x+5, write this way: @(x)2*x+5 \n')
xl=input('give lower limit xl: \n')
xu=input('give upper limit xu: \n')
error_limit=input('give your error limit value: \n')
max_iteration=input('give the maximum iteration value: \n')
iter = 0; xm = xl;
while (1)
iter=iter+1;
xmold = xm;
xm = (xl + xu)/2;
iter = iter + 1;
error = abs((xm - xmold)/xm) * 100;
test = f(xl)*f(xm);
if test < 0
xu = xm;
elseif test > 0
xl = xm;
else
error = 0;
end
if error <= error_limit | iter >= max_iteration,break,end
end
disp(xm)
FALSE POSITION METHOD
f=input('give your input function to get root of.example:to
give f(x)=2x+5, write this way: @(x)2*x+5 \n')
xl=input('give lower limit xl: \n')
xu=input('give upper limit xu: \n')
error_limit=input('give your error limit value: \n')
max_iteration=input('give the maximum iteration value: \n')
iter = 0; xr = xl;
while (1)
iter=iter+1;
xrold = xr;
xr = xu-((f(xu)*(xl-xu))/(f(xl)-f(xu)));
iter = iter + 1;
error = abs((xr - xrold)/xr) * 100;
test = f(xl)*f(xr);
if test < 0
xu = xr;
elseif test > 0
xl = xr;
else
error = 0;
end
if error <= error_limit | iter >= max_iteration,break,end
end
fprintf('The root is: ')
disp(xr)

NEWTON-RAPHSON METHOD
f=input('give your input function to get root of.example:to
give f(x)=2x+5, write this way: @(x)2*x+5: \n')
df=input('give your derivative of the function: \n')
xr=input('give your first assumption of root , xr : \n')
error_limit=input('give your error limit value: \n')
max_iteration=input('give the maximum iteration value: \n')
iter = 0;
while (1)
iter=iter+1;
xrold = xr;
xr = xrold - f(xrold)/df(xrold);
iter = iter + 1;
error = abs((xr - xrold)/xr) * 100;
if error <= error_limit | iter >= max_iteration,break,end
end
fprintf('The root is: ')
disp(xr)
SECANT METHOD
f=input('give your input function to get root of.example:to
give f(x)=2x+5, write this way: @(x)2*x+5 \n')
x0=input('give lower limit x0: \n')
x1=input('give upper limit x1: \n')
error_limit=input('give your error limit value: \n')
max_iteration=input('give the maximum iteration value: \n')
iter =0;x2=x1;
while (1)
iter=iter+1;
x2old = x2;
x2 = x1 - (f(x1)*(x0-x1))/(f(x0)-f(x1));
error = abs((x2 - x2old)/x2) * 100;
if error <= error_limit | iter >= max_iteration,break,end
x0=x1; x1=x2;
end
fprintf('The root is: ')
disp(x2)

FIXED POINT ITERATION

f=input('give your input function to get root of.example:to


give f(x)=2x+5, write this way: @(x)2*x+5 \n')
% f can be [exp(-x)-x]
g=@(x) f(x)+ x;
error_limit=input('give your error limit value: \n')
max_iteration=input('give the maximum iteration value: \n')
iter = 0; xr =input('give the first guess of your root: ');
while (1)
iter=iter+1;
xrold = xr;
xr =g(xrold);
iter = iter + 1;
error = abs((xr - xrold)/xr) * 100;
if error <= error_limit | iter >= max_iteration,break,end
end
fprintf('The root is: ')
disp(xr)

You might also like