Newton Raphson Method Algorithm:
1. Start
2. Read x, e, n, d
*x is the initial guess
e is the absolute error i.e the desired degree of accuracy
n is for operating loop
d is for checking slope*
3. Do for i =1 to n in step of 2
4. f = f(x)
5. f1 = f'(x)
6. If ( [f1] < d), then display too small slope and goto 11.
*[ ] is used as modulus sign*
7. x1 = x – f/f1
8. If ( [(x1 – x)/x1] < e ), the display the root as x1 and goto 11.
*[ ] is used as modulus sign*
9. x = x1 and end loop
10. Display method does not converge due to oscillation.
11. Stop
% Newton Raphson Method
clear all
close all
clc
% Change here for different functions
f=@(x) cos(x)-3*x+1
%this is the derivative of the above function
df=@(x) -sin(x)-3
% Change lower limit 'a' and upper limit 'b'
a=0; b=1;
x=a;
for i=1:1:100
x1=x-(f(x)/df(x));
x=x1;
end
sol=x;
fprintf('Approximate Root is %.15f',sol)
a=0;b=1;
x=a;
er(5)=0;
for i=1:1:5
x1=x-(f(x)/df(x));
x=x1;
er(i)=x1-sol;
end
plot(er)
xlabel('Number of iterations')
ylabel('Error')
title('Error Vs. Number of iterations')
ANSWER :
f=
@(x)cos(x)-3*x+1
df =
@(x)-sin(x)-3
Approximate Root is 0.6071016481031231
Algorithm for Bisection Method:
1. Input function and limits.
2. Repeat steps 3 and 4 100 times.
3. x=(a+b)/2
4. If f(x0)<0, a=x else b=x
5. Display x
6. Repeat steps 7 and 8 10 times.
7. Error = x-(a+b)/2
8. Store error values in array
9. Plot error
10. STOP.
MATLAB program for bisection method:
% Crated by mayclassbook.org auther: mayuresh
% Created on 12 june 2013
% Bisection method
% Find the roots of x^3-x-1 using bisection method and plot the
error
f=@(x) x^3-x-1;
a=1;
b=2;
for i=1:100
c=(a+b)/2;
if f(c)>0
b=c;
else a=c;
end
end
a=1; b=2; p=c;
for i=1:100
c=(a+b)/2;
er(i)=f(c)-f(p);
if f(c)>0
b=c;
else a=c;
end
end
fprintf('Root of given equation is %f',c)
plot(er);
title('Plot of error')
xlabel('Number of iterations')
ylabel('Error')
grid on;