Docsity

Finding root using newton raphson method, Assignments of Numerical Methods in Engineering

Toggle sidebar
bg1
Clint Ponce Allanic
BSME II ENS184
I found out that Matlab m-files
cannot run indefinite
differentiation. Thus, I made 2
programs.
PROGRAM 1
% Newton-Raphson Method
fprintf('Note: This program is
using Symbolic Math Toolbox from
Mathworks. \n Thus, make sure to
install the toolbox before using
this code.\n -Clint\n');
a=input('[Note: Set f(x)=0 then
x=g(x)], Enter g(x)\n', 's');
f=inline(a);
syms f(x);
f(x)=str2sym(a);
g=diff(f);
tol=input('Enter Acceptable Error
in Percent\n');
iter=0;
x=0;
fprintf('Attempt Root
Error\n');
for i=2:1000
xr=x-(f(x)/g(x));
iter=iter+1;
format longg
if xr~=0
e=abs(xr-x)/xr*100;
end
if xr~=0
e=abs(xr-x)/xr*100;
end
if e<tol; break,end
fprintf('\t%d\t\t%.9f\t%.2f\n',i-1,
xr, e)
x=xr;
end
fprintf('Final Attempt Root
Error\n');
fprintf('\t%d\t
\t%.9f\t\t%.2f\n',i-1, xr, e)
Command Window Log
>> findroot
Note: This code is using Symbolic Math Toolbox
from Mathworks.
Thus, make sure to install the toolbox before
using this code.
-Clint
[Note: Set f(x)=0 then x=g(x)], Enter g(x)
[4*x^3]-[6*x^2]+[7*x]-[2.3]
Enter Acceptable Error in Percent
1
Attempt Root Error
1 0.328571429 100.00
2 0.444791555 26.13
3 0.450119555 1.18
Final Attempt Root Error
4 0.450124072 0.00
pf2

Partial preview of the text

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

Clint Ponce Allanic

BSME II ENS

I found out that Matlab m-files cannot run indefinite differentiation. Thus, I made 2 programs.

PROGRAM 1

% Newton-Raphson Method fprintf('Note: This program is using Symbolic Math Toolbox from Mathworks. \n Thus, make sure to install the toolbox before using this code.\n -Clint\n'); a=input('[Note: Set f(x)=0 then x=g(x)], Enter g(x)\n', 's'); f=inline(a); syms f(x); f(x)=str2sym(a); g=diff(f); tol=input('Enter Acceptable Error in Percent\n'); iter=0; x=0;

fprintf('Attempt Root Error\n');

for i=2: xr=x-(f(x)/g(x)); iter=iter+1; format longg if xr~= e=abs(xr-x)/xr100; end if xr~= e=abs(xr-x)/xr100; end if e<tol; break,end

fprintf('\t%d\t\t%.9f\t%.2f\n',i-1, xr, e) x=xr; end fprintf('Final Attempt Root Error\n'); fprintf('\t%d\t \t%.9f\t\t%.2f\n',i-1, xr, e)

Command Window Log

>> findroot

Note: This code is using Symbolic Math Toolbox

from Mathworks.

Thus, make sure to install the toolbox before

using this code.

-Clint

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

[4x^3]-[6x^2]+[7*x]-[2.3]

Enter Acceptable Error in Percent

Attempt Root Error

Final Attempt Root Error

Clint Ponce Allanic

BSME II ENS

PROGRAM 2

% Newton-Raphson Method fprintf('Note: This code is not using Symbolic Math ToolBox.\n Thus,find the First Order Derivative of your function first\n -Clint \n'); a=input('Enter Function if f(x)= \n', 's'); f=inline(a); df=input('Enter First Derivative of Function \n', 's'); g=inline(df); tol=input('Enter Acceptable Error in Percent\n'); iter=0; x=0;

fprintf('Attempt Root Error\n');

for i=2: xr=x-(f(x)/g(x)); iter=iter+1; format longg if xr~= e=abs(xr-x)/xr*100; end if e<tol; break,end

fprintf('\t%d\t\t%.9f\t%.2f\n',i-1, xr, e) x=xr; end fprintf('Final Attempt Root Error\n'); fprintf('\t%d\t \t%.9f\t\t%.2f\n',i-1, xr, e)

Command Window Log

>> Untitled

Note: This code is not using Symbolic Math

ToolBox. Thus,find the First Order Derivative of

your function first

-Clint

Enter Function if f(x)=

[4x^3]-[6x^2]+[7*x]-[2.3]

Enter First Derivative of Function

12x^2 - 12x + 7

Enter Acceptable Error in Percent

Attempt Root Error

Final Attempt Root Error