If you approximate your function with a straight line, where would that line cross zero? From the starting point, compute the slope of your function at that location. You can think of fsolve as a variation of Newton's method. However, it cannot be made to work as well in more than one dimension. Once it does, then it follows a scheme like that above.Īs you can see, such a scheme will work very nicely in one dimension. If fzero is given only one starting point, then it tries to find a pair of points that bracket a root. Do this operation repeatedly, and you will converge to a solution with certainty, as long as your function is continuous. This allows the routine to cut the interval in half, since now it MUST have a new interval that contains a root of f(x). If you give a bisection algorithm two end points of an interval that brackets a root of f(x), then it can pick a mid point of that interval. You can think of fzero as a sophisticated version of bisection. So what you need to do is to understand the algorithms! This is true of any comparison between different tools in matlab, both of which should yield a solution. Yes, there is a difference between the two functions, since they have different underlying algorithms. While I like the answer given by yoda, I'll just add a few points. For that, I'll refer you to the online documentation on the functions (see the links above). I'd probably use fzero in that case (assuming it crosses the x-axis).Īpart from this major difference, there are differences in implementations and the algorithms used. Sure, you have finer control but for finding a zero of a single var equation, I consider it a pain. As a result, there are a host of options and tolerances that need to be set manually to massage out the exact root. Often you can't find an exact solution to those equations and will have to set a tolerance level, within which you're willing to accept the solution as the answer. It is designed to solve a system of nonlinear equations. If it crosses the x-axis, there is a zero and it will find it (real only). It's because their objectives are different.įzero has a clear goal: find the zero! Simple. The difference in answers is not because of inefficient algorithms. However, fzero's answer is correct to within machine precision!. Even using the options I defined above only gets me to 0.0017 with fsolve. fsolve(f,0.1)įsolve doesn't return exactly 0 in this case either. (Function value at -1.37296e+154 is Inf.)Ĭheck function or try again with a different starting value.Ĭonsider another example with the function that crosses the x-axis and has a root at x=0. The problem appears regular as measured by the gradient.Įxiting fzero: aborting search for an interval containing a sign changeīecause NaN or Inf function value encountered during search. Using fsolve options=optimset('MaxIter',1e3,'TolFun',1e-10) įsolve completed because the vector of function values is near zeroĪs measured by the selected value of the function tolerance, and We'll define an anonymous function as and try to find the root using both the methods. The function is non-negative for all real values of x. Here's a simple example: Consider the function f=x^2. However, fzero will find the zero if and only if the function crosses the x-axis. I'll just mention the most straightforward difference between the two:įsolve can be used to solve for the zero of a single variable equation.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |