MATLAB's fsolve function

23 views (last 30 days)
John Smith
John Smith on 14 Feb 2017
Commented: Star Strider on 14 Feb 2017
I'm currently trying to understand MATLAB's fsovle function. I have two simultaneous equations and if I take an initial guess quite close to the solution, the result from fsovle would be correct. If I, however, take an initial guess quite far from the actual solution, MATLAB's fsolve result would yield an incorrect answer.
I'm not entirely sure why MATLAB gives this incorrect answer, as I thought that it would just take MATLAB longer to solve, going through more iterations until it finds a result close to the solution but it doesn't appear to do this. Can anyone explain to me what is going on here or at least some of the possible reasons if it isn't possible to say exactly why?
  1 Comment
Walter Roberson
Walter Roberson on 14 Feb 2017
I have not seen fsolve give incorrect solutions, except to the extent that sometimes the true root is between two representable numbers so back substituted does not always give 0.
Oh wait, yes, I have seen fsolve give a solution that is not a true root in the case where the function does not cross 0 but comes within round off error of doing so.
More common is that fsolve returns a different (actual) root than expected. The basic newton's algorithm used can end up over projecting to the point where it picks up a root fairly different than the starting point. Oddly enough that happens more when the guess is close to the correct solution but on the steep part of the hump.

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 14 Feb 2017
Your equations have more than one root or set of roots. The fsolve function finds those closest to your initial parameter estimates.
  2 Comments
John Smith
John Smith on 14 Feb 2017
Oh that makes sense! Can't believe I didn't think of that. It would also explain how the solution it finds is zero for one equation and not the other.
Usually if this happens what would be the approach to find the correct corresponding roots? I need to keep changing a parameter in the simultaneous equations and then find the roots, and I created a loop to do this. The only problem is, we don't know what should be the best initial guess as it changes when I change the parameter. I realise that this might depend on the actual equations and if it does that's also fine, but I thought I would just mention it here on the off chance that there is an efficient way of doing this.
Star Strider
Star Strider on 14 Feb 2017
I don’t know what your system consists of. If you have one equation defined on the (x,y) plane and want to find the (x,y) values where the equations evaluate to zero, use the contour function. You can tell it to output the (x,y) coordinates of a specific value. This is the way I always use to find the roots (zeros) of a bivariate funciton.
See the contour documentation for Display Single Contour Line and note the documentation allow you to recover those values:
  • [C,h] = contour(...) returns the contour matrix C containing the data that defines the contour lines, and the contour object h.

Sign in to comment.

Categories

Find more on Contour Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!