What is the error in running Newton's Method in Matlab for a specific function?

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
7 replies · 9K views
renolovexoxo
Messages
23
Reaction score
0
I've been using this for a Newton Approximation in Matlab

function x = Newton(f, fp, x, nmax, e)

% f is an inline function which we apply Newton's method on
% fp is an inline function that is the derivative of function f
% x is the initial guess of the root
% nmax is the total number of iterations done
% e is the error used to control convergence

fprintf('x(0) = %10g \n', x)
for n = 1:nmax
d = f(x)/fp(x);
x = x - d;
fprintf('x(%i) = %10g \n', n, x)
if abs(d) < e
fprintf('Converged! \n')
return
end
end

with this to run it:
%declare our function f
f = inline('e^x+2^(-1*x)+2*cos(x)-6');

% declare the derivative of function f
fp = inline('e^x-ln(2)*2^(-1*x)-2*sin(x)');

% declare total number of iterations to be undertaken
nmax = 100;

% declare value of initial starting point
x = 1.0;

% declare amount of error allowed
e = 10.0e-5;

% carry out iteration using function above
x = Newton2(f,fp,x,nmax,e);


This isn't working or running for this function, but has run fine for every other function. I'm not sure what isn't working about it. I keep getting the error:

Error using inline/subsref (line 13)
Not enough inputs to inline function.

Error in Newton2 (line 11)
d = f(x)/fp(x);

But I am very new to Matlab, and have no idea what this means.
 
Physics news on Phys.org
Could it be that, in the derivative, "ln" is not the function name for log(e) in Matlab? Try replacing it with "log".
 
That didn't work. I am still getting the same error.
 
renolovexoxo said:
That didn't work. I am still getting the same error.
OK, is 'e' a pre-defined constant in Matlab? (I can't see it in the function list, wheareas pi, i and j are listed and there's no reference to it under the definition of exp)
 
Error using inlineeval (line 15)
Error in inline expression ==> 2.71828^x-ln(2)*2^(-1*x)-2*sin(x)
Undefined function 'ln' for input arguments of type 'double'.

Error in inline/subsref (line 24)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);

Error in Newton2 (line 11)
d = f(x)/fp(x);

I got this error when I replaced e with 2.71828, which I thought would have gotten rid of the problem? I'm sorry, I really have no idea what I'm doing MATLAB wise and my teacher isn't any help.
 
renolovexoxo said:
Error using inlineeval (line 15)
Error in inline expression ==> 2.71828^x-ln(2)*2^(-1*x)-2*sin(x)
Undefined function 'ln' for input arguments of type 'double'.

Error in inline/subsref (line 24)
INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);

Error in Newton2 (line 11)
d = f(x)/fp(x);

I got this error when I replaced e with 2.71828, which I thought would have gotten rid of the problem? I'm sorry, I really have no idea what I'm doing MATLAB wise and my teacher isn't any help.
I don't think 'ln' is a valid Matlab function name - try replacing it with log again.
 
renolovexoxo said:
...
I got this error when I replaced e with 2.71828, which I thought would have gotten rid of the problem? I'm sorry, I really have no idea what I'm doing MATLAB wise and my teacher isn't any help.

Oh, and replace 'e' with 'exp(1)' for greater precision.