0% found this document useful (0 votes)
16 views1 page

Newton Raphson (Main) .M

Uploaded by

Ucchash Dam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views1 page

Newton Raphson (Main) .M

Uploaded by

Ucchash Dam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

% 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 || tol > e
fa = eval(subs(y,x,a));
ga = eval(subs(g,x,a));
if ga == 0
disp('We cannot apply Newton Raphson Method since Derivative is zero.');
break;
end

b = a - fa/ga;
tol = abs(b-a)/2;
iterationData(step,:) = [step,tol];
fprintf('step=%d\ta=%f\tf(a)=%f\ttolerence = %f\n',step,a,fa,tol);
a = b;
if step>N
disp('Iteration number is not sufficient.');
break;
end
step = step + 1;
end
fprintf('Root is %f\n', a);
figure(1);
hold on;
plot(iterationData(:,1),iterationData(:,2),'r-o');
xlabel('Number of iteration');
ylabel('Error');
legend ('Newton Raphson Method');
hold off;

You might also like