Using Newton's Method to Solve and Using Fsolve in Matlab

Click For Summary
SUMMARY

This discussion focuses on the implementation of Newton's Method and the use of the fsolve function in MATLAB to solve a system of nonlinear equations. The user successfully obtained solutions of 1.0000, 0.0000, and 2.0000 using fsolve, which indicates that the function values are near zero. However, Newton's Method did not converge, prompting a need to analyze the iteration conditions and accuracy requirements, specifically aiming for an accuracy of 1e-6. Key issues identified include the incorrect conditional statement in the while loop and the need for proper handling of vector comparisons.

PREREQUISITES
  • Understanding of MATLAB programming, specifically function handles and vector operations.
  • Familiarity with numerical methods, particularly Newton's Method for solving nonlinear equations.
  • Knowledge of the fsolve function in MATLAB for solving systems of equations.
  • Basic concepts of Jacobian matrices and their role in iterative methods.
NEXT STEPS
  • Review the MATLAB documentation on the fsolve function to understand its parameters and usage.
  • Learn about the convergence criteria for Newton's Method and how to implement them effectively.
  • Investigate how to properly handle vector comparisons in MATLAB, especially in conditional statements.
  • Explore techniques for counting iterations in iterative algorithms to assess performance and convergence.
USEFUL FOR

Mathematics students, engineers, and researchers who are working with numerical methods for solving nonlinear equations in MATLAB, particularly those interested in optimizing their implementation of Newton's Method and fsolve.

ver_mathstats
Messages
258
Reaction score
21
Homework Statement
We are required to solve a system of nonlinear equations in Matlab using both fsolve and Newton's Method. For Newton's Method we must achieve an accuracy of 1e-6 and write down the number of iterations to achieve the accuracy.
Relevant Equations
Newton's Method and Fsolve
Here is my code so far and solution.

When using fsolve, we obtain the result that the equation does get solved. We find the values to be 1.0000, 0.0000, and 2.0000. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient as stated by Matlab. Using Newton’s method from the starting point we get that the system of nonlinear equations does not converge, we're analyzing them so it does not mean there is a solution for this practice problem. I am trying to figure out how to count the amount of iterations and reach an accuracy of 1e-6 so my count may be completely incorrect and I am unsure of how to proceed from here because using fsolve results in me having solutions but using Newton's method, it does not converge, any help would be appreciated.

Matlab:
f = @(x) [x(1)^2+x(2)^2+x(3)^2-5 ; x(1)+x(2)-1 ; x(1)+x(3)-3];
fp = @(x) [2*x(1), 2*x(2), 2*x(3); 1, 1, 0; 1, 0, 1];
format long g

x = [(1+sqrt(3))/2; (1-sqrt(3))/2; sqrt(3)]; %starting point
acc =  1e-6;
count = 0;

while abs(x)>acc
  b = f(x); % evaluate f
  A = fp(x); % evaluate Jacobian
  d = -A\b;  % solve A*d = -b
  x = x + d
  count = count + 1;
end

disp(count);
 
Last edited by a moderator:
Physics news on Phys.org
I have not checked your Newton’s method code, but you need to rethink the conditional statement for the while loop. Under what conditions will the loop terminate?

Also, when you say it does not converge, what behavior are you seeing?
 
Last edited:
jasonRF said:
I have not checked your Newton’s method code, but you need to rethink the conditional statement for the while loop. Under what conditions will the loop terminate?
I think that what @jasonRF is saying is that the condition in your while loop is comparing apples and oranges. x is a vector, so abs(x) will also be a vector, while acc is a scalar.

I don't see fsolve anywhere in your code, and if you're using Newton's method, I don't see that anywhere, either.

In addition, there's something wrong with this line: either the code is wrong or the comment is wrong.
Matlab:
d = -A\b;  % solve A*d = -b

From the comment, if Ad = -b, then ##d = -A^{-1}b##, provided that A has an inverse.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 9 ·
Replies
9
Views
2K
Replies
2
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K