在mathematica中,如何将初始条件作为ndsolve中的变量?

发布于 2024-10-06 12:01:35 字数 723 浏览 2 评论 0原文

我想要这样的东西,

w[w1_] := 
 NDSolve[{y''[x] + y[x] == 2, y[0] == w1, y'[0] == 0}, y, {x, 0, 30}]

这看起来效果更好,但我想我错过了 smtn,

w := NDSolve[{y''[x] + y[x] == 2, y[0] == w1, y'[0] == 0}, 
  y, {x, 0, 30}]
w2 = Table[y[x] /. w, {w1, 0.0, 1.0, 0.5}]

因为当我尝试制作表格时,它不起作用:

Table[Evaluate[y[x] /. w2], {x, 10, 30, 10}]

我收到错误:

ReplaceAll::reps: {<<1>>[x]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>

ps:有更好的地方吗问这样的问题? mathematica 没有支持的论坛,只有 mathGroup 电子邮件列表。如果 stackoverflow 能有更具体的 mathematica 标签,比如简化、ndsolve、绘图操作,那就太好了

i'd like to have something like this

w[w1_] := 
 NDSolve[{y''[x] + y[x] == 2, y[0] == w1, y'[0] == 0}, y, {x, 0, 30}]

this seems like it works better but i think i'm missing smtn

w := NDSolve[{y''[x] + y[x] == 2, y[0] == w1, y'[0] == 0}, 
  y, {x, 0, 30}]
w2 = Table[y[x] /. w, {w1, 0.0, 1.0, 0.5}]

because when i try to make a table, it doesn't work:

Table[Evaluate[y[x] /. w2], {x, 10, 30, 10}]

i get an error:

ReplaceAll::reps: {<<1>>[x]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>

ps: is there a better place to ask questions like that? mathematica doesn't have supported forums and only has mathGroup e-mail list. it would be nice if stackoverflow would have more specific mathematica tags like simplify, ndsolve, plot manipulation

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

贱人配狗天长地久 2024-10-13 12:01:35

有很多方法可以做到这一点。一是:

w[w1_] :=  NDSolve[{y''[x] + y[x] == 2, 
                     y'[0] == 0},      y[0] == w1,
                      y[x], {x, 0, 30}];

Table[Table[{w1,x,y[x] /. w[w1]}, {w1, 0., 1.0, 0.5}]/. x -> u, {u, 10, 30, 10}] 

输出:

{{{0., 10, {3.67814}}, {0.5, 10, {3.25861}}, {1.,10, {2.83907}}}, 
 {{0., 20, {1.18384}}, {0.5, 20, {1.38788}}, {1.,20, {1.59192}}}, 
 {{0., 30, {1.6915}},  {0.5, 30, {1.76862}}, {1.,30, {1.84575}}}}

There are a lot of ways to do that. One is:

w[w1_] :=  NDSolve[{y''[x] + y[x] == 2, 
                     y'[0] == 0},      y[0] == w1,
                      y[x], {x, 0, 30}];

Table[Table[{w1,x,y[x] /. w[w1]}, {w1, 0., 1.0, 0.5}]/. x -> u, {u, 10, 30, 10}] 

Output:

{{{0., 10, {3.67814}}, {0.5, 10, {3.25861}}, {1.,10, {2.83907}}}, 
 {{0., 20, {1.18384}}, {0.5, 20, {1.38788}}, {1.,20, {1.59192}}}, 
 {{0., 30, {1.6915}},  {0.5, 30, {1.76862}}, {1.,30, {1.84575}}}}
夜深人未静 2024-10-13 12:01:35

我看到你已经选择了一个答案,但我想把这个线性方程组的解决方案扔掉。具体来说,这是对 Lotka-Volterra 的轻微变化进行建模。

(*Put everything in a module to scope x and y correctly.*)
Module[{x, y},

 (*Build a function to wrap NDSolve, and pass it
              the initial conditions and range.*)
 soln[iCond_, tRange_, scenario_] :=
  NDSolve[{
    x'[t] == -scenario[[1]] x[t] + scenario[[2]] x[t]*y[t],
    y'[t] == (scenario[[3]] - scenario[[4]]*y[t]) - 
      scenario[[5]] x[t]*y[t],
    x[0] == iCond[[1]],
    y[0] == iCond[[2]]
    },
   {x[t], y[t]},
   {t, tRange[[1]], tRange[[2]]}
   ];

 (*Build a plot generator*)
 GeneratePlot[{iCond_, tRange_, scen_, 
    window_}] :=
  (*Find a way to catch errors and perturb iCond*)     
  ParametricPlot[
   Evaluate[{x[t], y[t]} /. soln[iCond, tRange, scen]],
   {t, tRange[[1]], tRange[[2]]},
   PlotRange -> window,
   PlotStyle -> Thin, LabelStyle -> Medium
   ];

 (*Call the plot generator with different starting conditions*)
 graph[scenario_, tRange_, window_, points_] :=
  {plots = {};
   istep = (window[[1, 2]] - window[[1, 1]])/(points[[1]]+1);
   jstep = (window[[2, 2]] - window[[2, 1]])/(points[[2]]+1);
   Do[Do[
     AppendTo[plots, {{i, j}, tRange, scenario, window}]
     , {j, window[[2, 1]] + jstep, window[[2, 2]] - jstep, jstep}
     ], {i, window[[1, 1]] + istep, window[[1, 2]] - istep, istep}];
   Map[GeneratePlot, plots]
   }
 ]
]

然后我们可以使用 Animate(或 table,但 animate 非常棒)

tRange = {0, 4};
window = {{0, 8}, {0, 6}};
points = {5, 5}
Animate[Show[graph[{3, 1, 8, 2, 0.5},
      {0, t}, window, points]], {t, 0.01, 5},
      AnimationRunning -> False]

I see you already chose an answer, but I want to toss this solution for families of linear equations up. Specifically, this is to model a slight variation on Lotka-Volterra.

(*Put everything in a module to scope x and y correctly.*)
Module[{x, y},

 (*Build a function to wrap NDSolve, and pass it
              the initial conditions and range.*)
 soln[iCond_, tRange_, scenario_] :=
  NDSolve[{
    x'[t] == -scenario[[1]] x[t] + scenario[[2]] x[t]*y[t],
    y'[t] == (scenario[[3]] - scenario[[4]]*y[t]) - 
      scenario[[5]] x[t]*y[t],
    x[0] == iCond[[1]],
    y[0] == iCond[[2]]
    },
   {x[t], y[t]},
   {t, tRange[[1]], tRange[[2]]}
   ];

 (*Build a plot generator*)
 GeneratePlot[{iCond_, tRange_, scen_, 
    window_}] :=
  (*Find a way to catch errors and perturb iCond*)     
  ParametricPlot[
   Evaluate[{x[t], y[t]} /. soln[iCond, tRange, scen]],
   {t, tRange[[1]], tRange[[2]]},
   PlotRange -> window,
   PlotStyle -> Thin, LabelStyle -> Medium
   ];

 (*Call the plot generator with different starting conditions*)
 graph[scenario_, tRange_, window_, points_] :=
  {plots = {};
   istep = (window[[1, 2]] - window[[1, 1]])/(points[[1]]+1);
   jstep = (window[[2, 2]] - window[[2, 1]])/(points[[2]]+1);
   Do[Do[
     AppendTo[plots, {{i, j}, tRange, scenario, window}]
     , {j, window[[2, 1]] + jstep, window[[2, 2]] - jstep, jstep}
     ], {i, window[[1, 1]] + istep, window[[1, 2]] - istep, istep}];
   Map[GeneratePlot, plots]
   }
 ]
]

We can then use Animate (or table, but animate is awesome)

tRange = {0, 4};
window = {{0, 8}, {0, 6}};
points = {5, 5}
Animate[Show[graph[{3, 1, 8, 2, 0.5},
      {0, t}, window, points]], {t, 0.01, 5},
      AnimationRunning -> False]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文