用Mathematica绘制一个方程的解
我有一个函数f(x,t)
,我想用Mathematica绘制f(x(t),t)=0
的解x(t)
的f(x(t),t)=0
。 我该怎么做?
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解决这个问题有一些快速,直接的方法吗? 如果没有,请问有人能帮我解决这个问题吗?
另外,有没有人对这个问题有更好的称号?
编辑:遵循@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
。 有没有办法做到这一点?
编辑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}]]
其中eq1
和eq2
是我的等式, thrust
和angle
是解决方案
一种方法是创建一个列表然后绘制它。
你有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)
其他解是否可以替代RandomReal[{x_1,x_2}]
root[t,z]
,其中x_1
和x_2
在您想要探索的x
空间范围内。