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

In summary, the user is trying to run a Newton approximation function in Matlab, but is getting an error. They suspect that the error is caused by an undefined function name, 'ln', and replace it with 'log'. This didn't work, and they are still getting the error.
  • #1
renolovexoxo
25
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
  • #2
Could it be that, in the derivative, "ln" is not the function name for log(e) in Matlab? Try replacing it with "log".
 
  • #3
That didn't work. I am still getting the same error.
 
  • #4
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)
 
  • #5
I think NemoReally hit the problem. The variable 'e' is used in two different contexts that conflict.
 
  • #6
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.
 
  • #7
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.
 
  • #8
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.
 

1. What is Newton's Method and how does it work?

Newton's Method is a numerical technique used to find the roots of a mathematical function. It involves using an initial guess and iteratively improving that guess until it converges to the exact root. This is done by using the function's derivative to find the slope of the tangent line at the current guess and using it to update the guess.

2. How accurate is Newton's Method in finding roots?

Newton's Method can be very accurate in finding roots, especially for well-behaved functions. However, it may not always converge to the exact root or may converge to a different root depending on the initial guess and the function's behavior. It is important to carefully choose the initial guess and to check for convergence before accepting the result.

3. What is the difference between Newton's Method and other root-finding methods?

Unlike other root-finding methods such as the Bisection Method or the Secant Method, Newton's Method requires knowledge of the function's derivative. This can make it more efficient in finding roots, but also adds complexity and the potential for convergence issues. It is also an iterative method, meaning it continuously improves the initial guess until it converges, while other methods may require multiple function evaluations.

4. Can Newton's Method be used for finding complex roots?

Yes, Newton's Method can be extended to find complex roots by using complex arithmetic and derivatives. This involves considering both the real and imaginary parts of the function and its derivative when updating the guess. However, it may be more complex and may require more iterations to converge compared to finding real roots.

5. Is there a built-in function for Newton's Method in Matlab?

Yes, Matlab has a built-in function called "fzero" that uses Newton's Method to find roots of a given function. It takes in the function, an initial guess, and other optional parameters, and returns the approximate root. However, it is important to understand how Newton's Method works in order to use this function effectively and to check for convergence and accuracy of the result.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
943
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • Calculus
Replies
13
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
Back
Top