Newton Raphson Method

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

NEWTON RAPHSON METHOD

Algorithm:

Step 1. Start

Step 2. Define function as f (x)

Step 3. Define first derivative of f(x) as g(x)

4. Input initial guess (x0), tolerable error (e)


and maximum iteration (N)

5. Initialize iteration counter i = 1

6. If g(x0) = 0 then print "Mathematical Error"


and goto (12) otherwise goto (7)

7. Calcualte x1 = x0 - f(x0) / g(x0)

8. Increment iteration counter i = i + 1

9. If i >= N then print "Not Convergent"


and goto (12) otherwise goto (10)

10. If |f(x1)| > e then set x0 = x1


and goto (6) otherwise goto (11)

11. Print root as x1

12. Stop

MATLAB CODE:
clc
clear all
syms x
f(x)=input(‘Enter your function= ’);
df(x)=diff(f)
x0=input(‘Enter the initial guess=’);
n=input(‘Enter the number of iterations= ’)
e=input(‘Enter tolerance=’)
if df(x0)~=0
for i=1:n
x1=x0-(f(x0)/df(x0))
fprintf(‘x%d = %0.6f\n’, i,x1)
if abs(x1-x0) < e
break
end
x0=x1
end
else
disp(‘Newton method fails’)
end
fprintf('The root of the equation is %d=' , x1)

Example
clc
clear all
syms x
f(x)=2x^2-3
df(x)=4x
x0=1
n=10
e=10^-9
if df(x0)~=0
for i=1:n
x1=x0-(f(x0)/df(x0))
fprintf(‘x%d = %0.6f\n’, i,x1)
if abs(x1-x0) < e
break
end
x0=x1
end
else
disp(‘Newton method fails’)
end
fprintf('The root of the equation is %d=' , x1)

You might also like