I need some advice on using the function FindMaximum as follows.
I find the function y(x) from a differential equation as below, and then for each c and each xMax I have different solutions y(x) and I evaluate Abs(y(x=xMax)).
test[a_, xMax_] := Module[{temp},
temp = NDSolve[{y'[x] == c* y[x] Cos[x + y[x]], y[0] == 1},
y, {x, 0, xMax}];
(y /. temp)[[1]][xMax] // Abs (*Abs(y(x=xMax))*)
]
I fix xMax = 30 and narrow c from 0.2 to 0.5 and I want to find c that gives me the largest Abs(y(x=xMax)).
FindMaximum[{test[c, 30], 0.2 < c < 0.5}, {c, 0.3}]
But the code doesn't work. Errors appear like
How can I resolve this.
test[c_, xMax_] := Module[...
withClear[test];test[c_?NumericQ, xMax_] := Module[...
and see if your problem goes away. ThatClear[test]
removes any cached old definition oftest
and the?NumericQ
forces evaluation to be done on numeric constants given toc
instead of any possible symbolic evaluation ofc
without an assigned numeric constant value. $\endgroup$