I want to have the argument 'a' for myf2 in form of optional argument, but at the same time I need to evaluate the function only if 'a' is Numeric, see also my previous question.
Options[myf2] = {"a" -> 1};
myf2[b_?NumericQ, opts : OptionsPattern[]] := Module[{y, x, a},
a = OptionValue["a"];
If[NumericQ[a], First[y /.NDSolve[{a y'[x] == b y[x] Cos[x + y[x]], y[0] == 1},
y, {x, 0, 30}]][10]]
];
datamyf2 = {#, myf2[1, #] + 0.01 RandomReal[]}&/@Range[0.5, 1.5,0.1]
model = NonlinearModelFit[datamyf2, myf2[b, "a" -> a], b, a]
When I tried to add the NumericQ test for 'a' in myf2, the problem is that it returns Null upon evaluating with symbol 'y' {myf2[1, "a" -> y]}, instead of leaving it unevaluated at all (as for the case when 'a' was specified as regular argument with a_?NumericQ)
How can I than specify that it should have numeric value for 'a' in the option?
Thanks!
a
to be an optional argument? At least in your example it doesn't look optional to me. $\endgroup$