Solving Matlab Newton's Method Homework

In summary, the conversation discusses a program that implements Newton's method to find the root of an equation. The root is approximately 13.1, but the program returns complex values for initial guesses above 45 and NaN for initial guesses above 1000. The cause of this is attributed to f and df being Inf + a tiny imaginary number and -Inf + a tiny imaginary number, respectively. The appropriate solutions and safeguards are also mentioned.
  • #1
SherlockOhms
310
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
  • #2
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.
 

FAQ: Solving Matlab Newton's Method Homework

How do I set up a Newton's method function in Matlab?

In order to set up a Newton's method function in Matlab, you will need to define the function you want to find the root of, its derivative, and the initial guess. You can then use a loop to iterate through the Newton's method formula until you reach a desired level of accuracy.

2. What is the syntax for implementing Newton's method in Matlab?

The syntax for implementing Newton's method in Matlab is as follows:
[x, err] = newtonMethod(f, df, x0, tol, maxIter)
where f is the function to find the root of, df is its derivative, x0 is the initial guess, tol is the desired level of accuracy, and maxIter is the maximum number of iterations.

3. How do I know if my Newton's method solution is accurate enough?

You can determine the accuracy of your Newton's method solution by comparing the absolute value of the difference between the current and previous approximations to the desired level of accuracy. If the difference is smaller than the tolerance, then you can consider your solution to be accurate enough.

4. Can I use Newton's method to find multiple roots of a function?

Yes, you can use Newton's method to find multiple roots of a function by providing multiple initial guesses. However, it is important to note that this method may not always converge to all roots, and some initial guesses may lead to the same root.

5. Can I use Newton's method to find complex roots of a function in Matlab?

Yes, you can use Newton's method to find complex roots of a function in Matlab. However, you will need to modify the code to handle complex numbers and their derivatives. It is also important to make sure your initial guess is close enough to the desired complex root for the method to converge.

Back
Top