fsolve is slower when a Jacobian Pattern is provided

1 view (last 30 days)
Hi, I am trying to solve a nonlinear equations system with fsolve. I tried to make it run faster by supplying a Jacobian Pattern, but so far what I am seeing that it makes the code run slower than before. Also, it doesn't seem to make a difference in runtimes if I supply a JacobPattern that is actually full.
You can see in the code below - options0 does not provide a JP, options1 provides the correct JP. I checked the answers themselves against each other and they vary by small numerical errors only (1e-10), so the JP should be correct. Run times, however, are longer for 1 than for 0, and I would expect the converse.
Am I missing something here?
Thanks
K>> options0 = optimoptions(@fsolve,'Algorithm','trust-region-reflective');
K>> options1 = optimoptions(@fsolve,'JacobPattern',obj.jacob_pattern,'Algorithm','trust-region-reflective');
K>> tic,y0=fsolve(@(y)obj.dy(0,y),obj.get_ic('uniform'),options0);toc
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
Elapsed time is 346.054602 seconds.
K>> tic,y1=fsolve(@(y)obj.dy(0,y),obj.get_ic('uniform'),options1);toc
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the default value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
Elapsed time is 543.596238 seconds.
K>> max(abs(y0-y1))
ans =
1.2562e-10
  1 Comment
John D'Errico
John D'Errico on 3 Jul 2016
How sparse is the Jacobian? If it is not that sparse, then you won't see a gain. Too often people think they are using sparse matrices, so things MUST be faster, even for matrices that are only 50% non-zeros. That ain't sparse!

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!