Docsity

Finding the root using newton raphson method, Cheat Sheet of Numerical Methods in Engineering

Toggle sidebar
bg1
Clint Ponce Allanic
BSME II ENS184
% Fixed Point Method
a=input('[Note: Set f(x)=0 then x=g(x)], Enter g(x)\n', 's');
f=inline(a);
x0=0;
tol=input('Enter Acceptable Error in Percent\n');
xr=x0;
iter=0;
fprintf('Attempt Root Error\n');
for i=2:1000
xrold=xr;
xr=f(xrold);
iter=iter+1;
format longg
if xr~=0
e=abs(xr-xrold)/xr*100;
end
fprintf('\t%d\t\t%.9f\t%.2f\n',i-1, xr, e)
if e<tol; break,end
end
fprintf('Final Attempt Root Error\n');
fprintf('\t%d\t\t\t%.9f\t\t%.2f\n',i-1, xr, e)
Command Window Log
>> findroot
[Note: Set f(x)=0 then x=g(x)], Enter g(x)
[-[4*x^3]+[6*x^2]+[2.3]]/7
Enter Acceptable Error in Percent
1
Attempt Root Error
1 0.328571429 100.00
2 0.400837984 18.03
3 0.429487748 6.67
4 0.441409369 2.70
5 0.446433235 1.13
6 0.448559217 0.47
Final Attempt Root Error
6 0.448559217 0.47

Partial preview of the text

Download Finding the root using newton raphson method and more Cheat Sheet Numerical Methods in Engineering in PDF only on Docsity!

Clint Ponce Allanic

BSME II ENS

% Fixed Point Method a=input('[Note: Set f(x)=0 then x=g(x)], Enter g(x)\n', 's'); f=inline(a); x0=0; tol=input('Enter Acceptable Error in Percent\n'); xr=x0; iter=0; fprintf('Attempt Root Error\n'); for i=2: xrold=xr; xr=f(xrold); iter=iter+1; format longg if xr~= e=abs(xr-xrold)/xr*100; end fprintf('\t%d\t\t%.9f\t%.2f\n',i-1, xr, e) if e<tol; break,end end fprintf('Final Attempt Root Error\n'); fprintf('\t%d\t\t\t%.9f\t\t%.2f\n',i-1, xr, e)

Command Window Log

>> findroot

[Note: Set f(x)=0 then x=g(x)], Enter g(x)

[-[4x^3]+[6x^2]+[2.3]]/

Enter Acceptable Error in Percent

Attempt Root Error

Final Attempt Root Error