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

• ver_mathstats
However, in your code, you're actually solving for d, which doesn't have an inverse. This will result in an error.

#### ver_mathstats

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:
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.