How approximate root in Newton Method ?

2 views (last 30 days)
Nourah Ibrahim
Nourah Ibrahim on 5 Apr 2014
Commented: Walter Roberson on 5 Apr 2014
Writing a MATLAB program to approximate a zero of the following function using Newton method.
Approximate the root within 10^-5
1. f(x) = x^3 - 3 x^2 +x -1
2. f(x) = x^3 -7
3. f(x) = sinx - e^(-x)
.. this is my code but i don't know how to approximate the root within 10^-5
function [ x, ex ] = newton( f, df, x0, tol, nmax )
if nargin == 3
tol = 1e-4;
nmax = 1e1;
elseif nargin == 4
nmax = 1e1;
elseif nargin ~= 5
error('newton: invalid input parameters');
end
f = inline(f);
df = inline(df);
x(1) = x0 - (f(x0)/df(x0));
ex(1) = abs(x(1)-x0);
k = 2;
while (ex(k-1) >= tol) && (k <= nmax)
x(k) = x(k-1) - (f(x(k-1))/df(x(k-1)));
ex(k) = abs(x(k)-x(k-1));
k = k+1;
end
end

Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!