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

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting an implementation of Newton's Method in Matlab for a specific function. Participants explore potential errors in the code related to function definitions and variable usage.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their implementation of Newton's Method and the error encountered when running it.
  • Another participant suggests that the issue may stem from using "ln" instead of "log" for the natural logarithm in Matlab.
  • A participant reports that changing "ln" to "log" did not resolve the error.
  • Concerns are raised about the variable 'e' potentially conflicting with Matlab's predefined constants.
  • Further errors are reported when replacing 'e' with a numerical approximation, indicating ongoing confusion about the function definitions.
  • Another participant reiterates that 'ln' is not a valid function in Matlab and suggests replacing it with "log" again.
  • One participant recommends using 'exp(1)' instead of '2.71828' for better precision when defining 'e'.

Areas of Agreement / Disagreement

Participants express differing views on the source of the error, with no consensus reached on a definitive solution. Multiple competing hypotheses about the cause of the problem remain unresolved.

Contextual Notes

Participants note potential limitations in the code related to function definitions and variable naming conventions in Matlab, but do not resolve these issues.

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)
 
I think NemoReally hit the problem. The variable 'e' is used in two different contexts that conflict.
 
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.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
Replies
1
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K