首页 文章

使用Mathematica将解决方案绘制到方程式中

提问于
浏览
0

我有一个函数 f(x,t) ,我想使用Mathematica绘制 f(x(t),t)=0 解决方案的功能 x(t) . 我该怎么做?

Mathematica通常与我可以使用的其他编程语言完全不同 . 通常,我会尝试看起来像:

Create arrays X, T

For t in T do
   solve (numerically) f(x,t)=0, append the solution to X

Plot X

但是,我不太清楚如何在Mathematica中使用循环,对于数组也是如此,所以我在这方面遇到了严重的问题 .

Mathematica是否有一些快速,直接的方法来解决这个问题?如果没有,有人可以帮我解决这个问题吗?

此外,是否有人有更好的问题 Headers ?


Edit: 根据@LutzL的建议,我会尝试以下内容:

Table[FindRoot[f[x,t]==0,{x,x_0}],{t,start,stop,step}]

这会正常吗?

我还有一个问题,因为我的函数 f(x,t) 是高度非线性的,因此我想为每个 t 输入一个好的起点 . 具体来说,我知道 t=0 的解决方案,我想用于时间步 t_{n+1} t_n 的解决方案 . 有没有办法做到这一点?


Edit 2: 我通过以下方式解决了问题:

tmax = 10; nsteps = 100*tmax;
thrust = {v/2 - g}; angle = {Pi/2};
For[i = 1, i <= nsteps, i++, 
  sol = {thr, \[Theta]} /. 
    FindRoot[{eq1[i*tmax/nsteps], 
      eq2[i*tmax/nsteps]}, {{thr, Last[thrust]}, {\[Theta], 
       Last[angle]}}]; AppendTo[thrust, sol[[1]]]; 
  AppendTo[angle, sol[[2]]]];
ListPlot[Table[{i*tmax/nsteps, thrust[[i + 1]]}, {i, 0, nsteps}]]
ListPlot[Table[{i*tmax/nsteps, angle[[i + 1]]/Pi}, {i, 0, nsteps}]]

其中 eq1eq2 是我的方程式, thrustangle 是解决方案

1 回答

  • 1

    一种方法是创建一个列表,然后绘制它 .

    你有 x(0) ,你想要 x(t) for t>0 . 您可以使用Szabolcs提供的表达式:

    root(t_NumericQ, x0_):= Module[{z}, z = z /. FindRoot[f[z, t] == 0, {z, x0}]]
    

    然后你计算并绘制一个列表 .

    list[tin_, tend_, tstep_, x0_] := Module[{z = x0, t = tin}, lis = {}; 
    While[t < tend, z = root[t, z]; lis = Append[lis, {t, z}]; t = t + tstep; ];
    ListPlot[lis]]
    

    或者您可以更改 x=Interpolation[lis] 的最后一行,而 x[t] 将是解决方案的插值函数 x(t)

    此外,您可以测试 x(t) 的其他解决方案是否可以替换 root[t,z]RandomReal[{x_1,x_2}] ,其中 x_1x_2 在您要探索的 x 空间的范围内 .

相关问题