Newton Raphson Method MATLAB Program with Output
This program implements Newton Raphson Method for finding real root of nonlinear equation in MATLAB.
In this MATLAB program, y is nonlinear function, a is initial guess, N is maximum number of permitted itertaion steps and e is tolerable error.
MATLAB Source Code: Newton-Raphson Method
% Clearing Screen
clc
% Setting x as symbolic variable
syms x;
% Input Section
y = input('Enter non-linear equations: ');
a = input('Enter initial guess: ');
e = input('Tolerable error: ');
N = input('Enter maximum number of steps: ');
% Initializing step counter
step = 1;
% Finding derivate of given function
g = diff(y,x);
% Finding Functional Value
fa = eval(subs(y,x,a));
while abs(fa)> e
fa = eval(subs(y,x,a));
ga = eval(subs(g,x,a));
if ga == 0
disp('Division by zero.');
break;
end
b = a - fa/ga;
fprintf('step=%d\ta=%f\tf(a)=%f\n',step,a,fa);
a = b;
if step>N
disp('Not convergent');
break;
end
step = step + 1;
end
fprintf('Root is %f\n', a);
Bisection Method MATLAB Output
Enter non-linear equations: cos(x)-x*exp(x) Enter initial guess: 1 Tolerable error: 0.00001 Enter maximum number of steps: 20 step=1 a=1.000000 f(a)=-2.177980 step=2 a=0.653079 f(a)=-0.460642 step=3 a=0.531343 f(a)=-0.041803 step=4 a=0.517910 f(a)=-0.000464 step=5 a=0.517757 f(a)=-0.000000 Root is 0.517757