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

AI Thread Summary
The discussion focuses on using fsolve and Newton's method to solve a system of nonlinear equations in Matlab. The user successfully obtains solutions of 1.0000, 0.0000, and 2.0000 using fsolve, indicating that the function values are near zero and the problem is regular. However, Newton's method fails to converge from the given starting point, raising concerns about the accuracy and iteration counting. Participants suggest revising the while loop's conditional statement, noting that the current comparison between a vector and a scalar is incorrect. The user is encouraged to clarify the behavior observed during the non-convergence of Newton's method and to ensure the code aligns with the intended logic.
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.
 
Thread 'Have I solved this structural engineering equation correctly?'
Hi all, I have a structural engineering book from 1979. I am trying to follow it as best as I can. I have come to a formula that calculates the rotations in radians at the rigid joint that requires an iterative procedure. This equation comes in the form of: $$ x_i = \frac {Q_ih_i + Q_{i+1}h_{i+1}}{4K} + \frac {C}{K}x_{i-1} + \frac {C}{K}x_{i+1} $$ Where: ## Q ## is the horizontal storey shear ## h ## is the storey height ## K = (6G_i + C_i + C_{i+1}) ## ## G = \frac {I_g}{h} ## ## C...

Similar threads

Back
Top