MATLAB has two methods to solve a nonlinear equation:
Therefore, one can use the following methods to solve a system of n
nonlinear independent equations:
- Use a loop to solve the equations separately using
fzero
- Use a loop to solve the equations separately using
fsolve
- Use
fsolve
to solve them together
My intuition would be that:
- A loop method is faster than a single system for large
n
as complexity (gradient calculation) is 0(n^2) - A loop may be slower for small
n
as a loop has a high overhead in MATLAB and there may be some constant startup time fzero
is faster thanfsolve
as it is specifically made for a single nonlinear equation.
Question: What is the fastest method to solve this problem? Which options should be used to speed up the process?
Related threads
The best approach to evaluate the performance of some method is to write a benchmark. Four cases are considered:
fzero
fsolve
Results
All the figures show the computation time for the complete system in function of
n
, the number of equations.The first two figures plot
n
up to 1000, with an interval of 100. The last two figures plotn
up to 100 with an interval of 1. For each, the second plot is the same as the first one but without loop fzero as it is much slower than the others.Conclusion
n
(fastest method forn < ~20
)n
(fastest method for~20 < n < ~50
, but difference with 2 and 3 is relative small).n
(fastest method for~50 < n
)In general, you should use independent fsolve, only for small
n
loop fzero may be used instead, i.e. usefsolve
with the following options:Lazy people may just use the default fsolve as it has a reasonable performance for a moderate number of equations (
n < ~200
)Remarks
fzero
andfsolve
may behave differently in some boundary cases, f.efzero
will not found a solution forx^2
as it searches the location of a sign change.