Hi Peter,
I assume the problem you are running into is that Mathematica is unable
to calculate symbolic derivatives of your function, since it is defined
with conditions. There are two standard ways to proceed here, and one
nonstandard way which I prefer.
First, the standard approaches. You will need to use the option
Method->FindMinimum
for these approaches. Then, you can either give two starting points for
your parameters, in which case the method FindMinimum won't calculate
symbolic derivatives, or you can feed NonlinearRegress the symbolic
gradients with the option
Gradient->{ list of gradients }
I should mention that I don't know how to get the second method above to
work.
The problems with the above approaches is that they are restricted to
the method FindMinimum, the two starting point approach is slow, and of
course I can't even get the Gradient method to work.
This leads to the "nonstandard" approach, which is to simply override
Mathematica's derivatives with your own definitions. Before describing
an example of this approach, let me describe it's virtues. It works for
both the default Levenberg-Marquardt method and the FindMinimum method,
and is in general faster than the two starting point approach.
Here is an almost nontrivial example. Suppose your model is given by a
numerical integration routine, such as
f[x_?NumericQ,a_?NumericQ]:=NIntegrate[E^(a q^2),{q,0,x}]
Obviously, Mathematica is unable to calculate derivatives of the above
function. On the other hand, we know that the derivative of the above
function with respect to a is simply
fa[x_?NumericQ,a_?NumericQ]:=NIntegrate[q^2 E^(a q^2),{q,0,x}]
The question is how do we communicate this knowledge to Mathematica? The
answer is to use an upvalue
f /: D[f[x_,a_],a_]:=fa[x,a]
Suppose the data is given simply by
data = Table[{x,f[x,2]},{x,0,2,.1}];
Then with the above definition for the derivative, the following works:
NonlinearRegress[data,f[x,a],x,{a}]
Of course, Mathematica complains, since the data has no error, but that
won't be a problem with real data.
If you try the above with the method FindMinimum, you will also need to
give the definition
Derivative[0,1][f][x_,a_]:=fa[x,a]
I hope the above helps answer your question. If you have problems trying
to apply it, send an example.
Carl Woll
Dept of Physics
U of Washington
Klamser wrote:
> Hello, how can I do a non Linear Regress not on a Function but on a
> Module, because the Function can not be defined without Conditions??
>
> Peter Klamser