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

In summary: However, in your code, you're actually solving for d, which doesn't have an inverse. This will result in an error.
  • #1
ver_mathstats
260
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
  • #2
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:
  • #3
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.
 

1. How does Newton's Method work?

Newton's Method is an iterative algorithm used to find the roots of a function. It starts with an initial guess and then uses the tangent line at that point to estimate a better guess. This process is repeated until the desired accuracy is achieved.

2. What is the advantage of using Newton's Method over other root-finding methods?

Compared to other methods, Newton's Method typically converges faster and is more accurate. It also has the advantage of being able to handle complex functions with multiple roots.

3. How do I implement Newton's Method in Matlab?

In Matlab, you can use the newton function to implement Newton's Method. This function takes in the function, its derivative, and an initial guess as inputs and returns the root as an output.

4. What is the difference between using Newton's Method and using the fsolve function in Matlab?

The fsolve function in Matlab uses a combination of different root-finding methods, including Newton's Method, to find the roots of a function. It is more versatile and can handle a wider range of functions, but it may not always be as efficient as using Newton's Method alone.

5. How do I know if my solution using Newton's Method is accurate?

You can check the accuracy of your solution by plugging it back into the original function and seeing if it is close to zero. Additionally, you can also specify a tolerance level in the newton function to control the desired accuracy of the solution.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
884
  • Engineering and Comp Sci Homework Help
Replies
1
Views
946
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
811
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
955
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
861
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
Back
Top