0

For example, Here is a NDSolve function with a parameter of w

s[w_] := NDSolve[{x''[t] == -T1 x'[t] + C1*Ex[x[t], y[t], t, w], 
    y''[t] == -T1 y'[t] + C1*Ey[x[t], y[t], t, w] - g, x[0] == 0, 
    y[0] == 0, x'[0] == 0, y'[0] == 0}, {x, y}, {t, 0, 
    timelimit}];
MMy[t_] = y[t] /. s[7];
ymax = Max[Table[MMy[i], {i, timelimit - 2, timelimit, .00001}]];

In this way, I can know the max value when w=7.

Now, how can I set a functin of ymax related to w.

Like ymax[w_] :=

I mean if I want to get the max value of y[t], I only need to change the w in the yman[] function rather than the value in this place MMy[t_] = y[t] /. s[w]

If I can set a function ymax[w], I can easily get a series of ymax with different value of w.

I have tried this:

MMy[w_, t_] = y[t] /. s[w];
ymax[w_] = Max[Table[MMy[w, i], {i, timelimit - 2, timelimit, .00001}]];

It seems not work.

Yue Yu
  • 55
  • 7
  • Max[y@“ValuesOnGrid” /. s[7]]? – Michael E2 Oct 14 '21 at 17:29
  • The output shows InterpolatingFunction[{{0., 5.}}, <>][CurlyDoubleQuote[ValuesOnGrid]] – Yue Yu Oct 14 '21 at 17:32
  • How did you enter "CurlyDoubleQuote"??? That's an input error. Use an ASCII double quote. [Ah, I guess it was my dumb smartphone.] – Michael E2 Oct 14 '21 at 17:33
  • I just copied this Max[y@"ValuesOnGrid" /. s[7]] – Yue Yu Oct 14 '21 at 17:36
  • Related: https://mathematica.stackexchange.com/a/58256/4999 – Michael E2 Oct 14 '21 at 17:41
  • Ok, I will try, thank you. I also want to limit the range of t in this {timelimit-2, timelimit} to find the max value. So, based on the find max value method in the question, do you have some suggestions? – Yue Yu Oct 14 '21 at 17:41
  • Yes, thank you for the link. I learned a lot from the previous question to get the optimal method to find max value. However, I think, currently, my question is more like a function construction question. Maybe I did not make it clear. – Yue Yu Oct 14 '21 at 17:45
  • If you don't need the full solution, use s[w_] := NDSolve[..., {t, timelimit - 2, timelimit}] and then the Max[..] code would do what you want. Otherwise something like Max@Select[Transpose[{y@"Grid", y@"ValuesOnGrid"} /. s[7]}], #[[1, 1]] > timelimit - 2 &] -- I don't have a computer at hand, so the code might need tweaking. – Michael E2 Oct 14 '21 at 17:51
  • You probably should define s[w_] with ClearAll[s]; s[w_?NumericQ] :=... and MMy with := instead of =. – Michael E2 Oct 14 '21 at 17:53
  • Thank fot the suggestion. I tried := too, which is very slow and I don't know if I can get the same results. I think the use of = stores the value of y[t] and will make the calculation of max[Table] very fast. – Yue Yu Oct 14 '21 at 18:28
  • If slow, maybe instead of NDSolve and s[w_], you should use s = ParametricNDSolveValue[...] with w as a parameter. It saves (recent) NDSolve results for you internally, so that multiple calls to s[7] reuse the NDSolve result – Michael E2 Oct 14 '21 at 18:38
  • Good point. But I don't know how to get reuslt with two variables using ParametricNDSolveValue I find examples in the help of ParametricNDSolveValue only have single variable. – Yue Yu Oct 14 '21 at 19:00

0 Answers0