-1

A simplified example looks like this. The basic idea is to maximize fHelper by adjusting \[Alpha] for each t, and then use the resulting f in a differential equation. In the real world application, however, the functions and differential equation are much more complicated.

fHelper[t_, u_] := t + u;
f[t_] := MinValue[{fHelper[t, u], u >= 0.15, u <= 1.5}, u];
solution = NDSolve[{g'[t] == f[t], g[0.03] == 0}, g, {t, 0, 0.03}];

The last line produces a series of errors, which seems to be related to unsuccessful evaluation of f.

NMinimize::nnum:
   The function value 0.211738 + t is not a number at {u} = {0.211738}.

NMinimize::nnum:
   The function value 0.211738 + t is not a number at {u} = {0.211738}.

NMinimize::nnum:
   The function value 0.211738 + t is not a number at {u} = {0.211738}.

General::stop: Further output of NMinimize::nnum
     will be suppressed during this calculation.

I have checked f to see if there is something wrong with it, but it behaves exactly like a normal pure function, and I have no idea why Mathematica is complaining.

In[4]:= f[0.01]

Out[4]= 0.16

In[5]:= f[0.02]

Out[5]= 0.17
nalzok
  • 279
  • 2
  • 6
  • 2
    Use f[t_?NumericQ]:=... for function f definition. – Alx Sep 15 '19 at 08:50
  • Possible duplicate: https://mathematica.stackexchange.com/questions/18393/what-are-the-most-common-pitfalls-awaiting-new-users/https://mathematica.stackexchange.com/questions/18393/what-are-the-most-common-pitfalls-awaiting-new-users/26037#26037, esp. this answer – Michael E2 Sep 15 '19 at 20:45

2 Answers2

1

How about this?

fHelper[t_, u_] := t + u;
f[t_] := MinValue[{fHelper[t, u], u >= 0.15, u <= 1.5}, u];
int = {#, f @@ #} & /@ Range[0, 0.03, 0.001] // Interpolation;
solution = g/.Flatten@NDSolve[{g'[t] == int[t], g[0.03] == 0}, g, {t, 0, 0.03}];
Plot[solution[t],{t,0,0.03}]

Mathematica graphics

Xminer
  • 1
  • 7
  • 15
1
fHelper[t_, u_] := t + u;
f[t_?NumericQ] := MinValue[{fHelper[t, u], u >= 0.15, u <= 1.5}, u];
solution = NDSolveValue[{g'[t] == f[t], g[0.03] == 0}, g, {t, 0, 0.03}];
Plot[solution[x], {x, 0, 0.03}]

enter image description here

Alx
  • 3,632
  • 11
  • 15