2
$\begingroup$

I have a routine which repeatedly calls FindMaximum to find the local maxima of f[m,a,b,c] as a function of m only (i.e. considers a,b,c as fixed and then finds the values of m for which the function is a local max).

I know that the FindMaximum algorithm uses Derivative[1,0,0,0][f]. Does this mean that Mathematica will recalculate the derivative every time FindMaximum is called? If so,

  1. Is there any way to avoid this (e.g. define explicitly a function equal to Derivative[1,0,0,0][f][m,a,b,c] and get Mathematica to use this function rather than calculating the derivative every time)?
  2. If (1.) is feasible, given that I expect a run of the routine to call FindMaximum several thousand times, would it represent a significant time saving?

If it helps, the function is

f[m_,a_,b_,c_]:=(Exp[a(m-b)]+1)^(-1) - c(2+m)/3 + c ((1-m^2)/3)^(1/2)

and the routine calls FindMaximum twice for each value of the parameters a,b,c (starting at m=0.9 and m=-0.9 respectively).

$\endgroup$
4
$\begingroup$

FindMaximum knows the Gradient-Option.

So you can just plug your gradient in like

FindMaximum[...,{...,...},Gradient->...]

I used your function and you'll get a speedup at about 23%. (For arbitrary choosen parameters and on my machine of course)

func=f[m,10,0,0];
grad=D[f[m,10,0,0],m];
Do[FindMaximum[func,{m,-0.9}];,10000]//Timing
Do[FindMaximum[func,{m,-0.9},Gradient->{grad}];,10000]//Timing

{3.34375,Null}

{2.71875,Null}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.