myfunn[x_, y_] := Module[{},
If[x < 5, Return[0]];
If[x < 10, Return[2]];
x*y];
I'm unable to find the maximum value of this function over the following range of x: 0<=x<=20 when y=0 using the NMaxValue, NMaximize or FindMaximum commands
NMaximize[{myfunn[x, 0], 0 <= x <= 20}, x]
NMaxValue[{myfunn[x, 0], 0 <= x <= 20}, x]
All of them return 0 while the answer I'm expecting is 2. Is there anyway to get Mathematica to compute the expected answer? Thanks
Clear[myfunn]
followed by the definitionmyfunn[x_?NumericQ, y_?NumericQ] := ...
? Alternatively, usePiecewise
instead ofIf
. $\endgroup$If
statements never get activated. Check:myfunn[x,y]
returnsx y
. $\endgroup$myfunn[x,0]
is immediately simplified to0
before ever putting a numerical value in forx
. TheIf
statements thus never get to see any numerical values forx
. $\endgroup$