Difference between fzero and fsolve for one variable

Is there a difference between using fzero and fsolve for one variable equation?

+6
source share
2 answers

Yes there is. I just mentioned the simplest difference between the two:

fsolve can be used to solve a single equation of a variable for zero. However, fzero will find zero if and only if the function intersects the x axis.

Here is a simple example: Consider the function f=x^2 . The function is non-negative for all real values โ€‹โ€‹of x . It has a root at x=0 . We will define the anonymous function as f=@ (x)x.^2; and try to find the root using both methods.

Using fsolve

 options=optimset('MaxIter',1e3,'TolFun',1e-10); fsolve(f,0.1,options) Equation solved. fsolve completed because the vector of function values is near zero as measured by the selected value of the function tolerance, and the problem appears regular as measured by the gradient. <stopping criteria details> ans = 1.9532e-04 

Not zero, but close.

Using fzero

 fzero(f,0.1) Exiting fzero: aborting search for an interval containing a sign change because NaN or Inf function value encountered during search. (Function value at -1.37296e+154 is Inf.) Check function or try again with a different starting value. ans = NaN 

He cannot find zero.

Consider another example with the function f=@ (x)x.^3; that intersects the x axis and has a root at x=0 .

 fsolve(f,0.1) ans = 0.0444 fzero(f,0.1) ans = -1.2612e-16 

fsolve in this case does not return exactly 0 . Even when using the options that I defined above, only 0.0017 with fsolve gets me. However, fzero answer is correct, accurate to the machine !. The difference in answers is not in inefficient algorithms. This is because their goals are different .

fzero has a clear goal: find zero! Simply. There are no ambiguities. If it crosses the x axis, then there is zero, and it will find it (only for real). If he does not cross, he whines.

However, the fsolve area is wider. It is designed to solve a system of nonlinear equations. Often you cannot find the exact solution to these equations and must set the tolerance level at which you are ready to make a decision as an answer. As a result, there are many options and tolerances that need to be adjusted manually in order to massage the exact root. Of course, you have finer control, but to find the zero of one var equation I find this a pain. I would fzero use fzero in this case (assuming it crosses the x axis).

Besides this significant difference, there are differences in implementations and algorithms used. To do this, I will give you an online documentation of the features (see links above).

+19
source

While I like the answer given by iodine, I just add a few points. Yes, there is a difference between the two functions, because they have different basic algorithms. Then there must be a difference. So you need to understand the algorithms! This is true for any comparison between different tools in Matlab, both of which should provide a solution.

You can think of fzero as a complex version of halving. If you give the halving algorithm two end points of the interval that copies the root of f (x), then it can select the midpoint of this interval. This allows the subroutine to cut the interval in half, since now it MUST have a new interval containing the root f (x). Repeat this operation and you will be sure to come to a decision if your function is continuous.

If fzero is given only one starting point, then it tries to find a pair of points that copy the root. Once this happens, he follows a pattern similar to the one above.

As you can see, such a scheme will work very well in one dimension. However, it cannot be made to work in more than one dimension. Therefore, fsolve cannot use a similar scheme. You can think of fsolve as a variation of Newton's method. From the starting point, calculate the slope of your function at that location. If you approximate your function with a straight line, where does this line cross zero?

Thus, in essence, fsolve uses a scheme in which you approximate your function locally, and then use this approximation to extrapolate to a new place where there is hope that the solution is near this point. Then repeat this pattern until you achieve convergence. This scheme will be easier to extend to more complex tasks than fzero.

You must have seen the difference. But you also need to understand when one circuit is successful and another is unsuccessful. In fact, the algorithms used are more complex than what I described, but these descriptions encapsulate the main ideas.

+7
source

Source: https://habr.com/ru/post/887040/


All Articles