Professional Documents
Culture Documents
BISECTION METHOD
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)