Professional Documents
Culture Documents
Newton Raphson Method
Newton Raphson Method
Algorithm:
Step 1. Start
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)