Solving Matlab Newton's Method Homework

Click For Summary
SUMMARY

The discussion focuses on a MATLAB implementation of Newton's Method for finding roots of equations, specifically addressing issues with complex roots and NaN results when initial guesses exceed certain thresholds. The user reports that while the method works correctly for initial guesses up to 50, values above this lead to complex outputs and eventually NaN due to overflow in function evaluations. The conversation highlights the need for safeguards against invalid initial guesses and suggests that the algorithm's limitations may be inherent to Newton's Method.

PREREQUISITES
  • Understanding of Newton's Method for root-finding
  • Familiarity with MATLAB programming
  • Knowledge of complex numbers and their representation in programming
  • Basic concepts of numerical stability and overflow in computations
NEXT STEPS
  • Implement input validation to check initial guess validity before executing Newton's Method
  • Explore MATLAB functions for handling complex numbers and NaN values
  • Research numerical methods for root-finding that are more stable than Newton's Method
  • Learn about MATLAB's error handling and warning mechanisms to manage iteration limits
USEFUL FOR

Students and professionals working with numerical methods in MATLAB, particularly those implementing root-finding algorithms and seeking to improve the robustness of their code against invalid inputs.

SherlockOhms
Messages
309
Reaction score
0

Homework Statement


So, I've written a program that carries out Newton's method. The root of the equation which I am trying to find is approximately 13.1. This is fine and my program returns the correct value when my initial guess is around this value (up to about x = 50 as my initial guess), however when I start using values such as 100+ it returns a complex root. The real part will be approximately 13.1 and the imaginary part will be VERY close to 0. Why is this and is there any way that I can fix/safeguard against this? Thanks.


Homework Equations





The Attempt at a Solution

Code:
function xnew = Newton (f, df, xi, tol)
xold = xi; %x(old) is assigned the value of the initial guess.
xnew = xold - f(xold)/df(xold); %Implement Newtons method to find x(new).
k = 0; %Assigns k(the counter) an initial value.
fprintf('\nTable of Iteration No.(k) and Depth(h)\n')
fprintf('\nIteration No.\tDepth\n')
fprintf('%5u\t\t%2.6e\n',k,xi)

while ((abs(xnew - xold))/(abs(xnew)) > tol) %Running condition.
    if (k <= 100) %Max number of iterations.
        xold = xnew; %x(old) get's x(new)'s calculated value as per Newton Method's.
        xnew = xold - f(xold)/df(xold); 
        k = k + 1; %Increment k.
    else
        warning('Maximum number of iterations has been reached') 
        break;
    end
    fprintf('%5u\t\t%2.6e\n',k,xnew)
end
 
Physics news on Phys.org
Yeah, so f and df are both printing out complex values once the initial guess is about 45 and above(remember the actual value of the root is about 13.1) and once the initial guess is about 1000, NaN is returned as the root. This is due to f being Inf + some tiny imaginary number and df being -Inf + some tiny imaginary number. Thus, f/fd will return NaN. First of all, is this due to a problem with my algorithm or is this just a shortcoming of Newton's Method? And second, how can I safeguard against getting imaginary values and values that are too large(i.e. Inf) for f and df? Like, is there a way to check before Newton's Method is carried out to see if the initial guess is valid? Thanks.
 

Similar threads

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