How to find a root and know if fsolve or fzero is better?
91 views (last 30 days)
Show older comments
Dear expert,
Can you please help how to find the root of the following function and pass parameters such as Z, X_mem, phi, C_surf so that I can use the value of the solver by calling the function to another function?
And if fsolve or fzero is better?
Z=[-1 1 -1]
X_mem = -0.5
phi= [0.7 0.8 0.9]
C_surf=[0.001 0.002 0.003]
f= @(k) Z(1)*k + X_mem + Z(2)*phi(2)*C_surf(2)*(k/(phi(1)*C_surf(1)))^(Z(2)/Z(1))...
+ Z(3)*phi(3)*C_surf(3)*(k/(phi(1)*C_surf(1)))^(Z(3)/Z(1));
Thank you in advance for your help.
0 Comments
Accepted Answer
John D'Errico
on 19 Feb 2023
Edited: John D'Errico
on 22 Feb 2023
IF you have a function of more than one variable, you ALWAYS need to use fsolve. fzero would not apply.
IF you have a function of only one variable, thus in your example f(k), then fzero will almost always be preferred. Why? fzero does some things that attempt to make it more robust against problems. It is designed to be a very good rootfinding code in ONE dimension. On the other hand, fsolve is designed to solve problems in multiple dimensions. It has none of the protections that fzero has built into it.
Next, fzero can accept a bracket. If you have a pair of points that bracket a solution, then fzero will ALWAYS converge to some solution, and it will do so in a robust, stable way.
If you do not have a bracket around the solution, so only one point, then fzero will attempt to find a bracket starting from that point that does contain a solution, but it is always happier if you give it a bracket.
The only flaw with fzero in this respect is if you do supply a bracket that does not have different signs at each end point, then it will fail, in a circumstance where fsolve MAY have succeeded. But had you given only one start point to fzero on the same problem, it too would often have worked, if a solution could be found.
Conversely, there may be some start points for fsolve where it may diverge, when fzero may be successful.
In general, IF you can use fzero, then I would always recommend fzero. Both solvers will be relatively fast, no matter what. But fzero has much going for it. If you can though, I would also always recommend providing a bracket for fzero.
For example:
f = @(x) exp(x) - 2;
We know the solution is in fact log(2)
format long g
log(2)
Now try both optimizers.
opts = optimset('Display','iter');
fzero(f,5,opts)
As you can see, fzero spent a fair amount of time wasted, searching for a bracket that contains the solution. In fact, you can see fzero operated in tow modes, first searching for a bracket, and then, once if had a bracket, it zoomed into the oslution nicely. Had I just given fzero a bracket initially though. fzero converges quite rapidly.
fzero(f,[0,10],opts)
By way of comparison, fsolve took more time to converge, but it did not need a bracket.
fsolve(f,5,opts)
Finally, you might notice that fsolve did not yield quite as good of a solution, whereas fzero gave virtually full double precision. Fsolve stopped at a point where it was happy.
log(2)
Again, if you can use fzero, then do so. Best, give it a bracket around a root, even if the bracket is quite wide.
More Answers (0)
See Also
Categories
Find more on Optimization 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!