需要帮助求解微分方程并绘制速度作为力项大小的函数
我需要一些帮助来在 mathematica 上绘制混沌振荡器的速度与力项图。
基本上,我必须求解以下微分方程
x''[t] + b x'[t] - x[t] + x[t]^3 - f Cos[w t] == 0, x'[0] == 0,
x[0] == 0
,并以 2*Pi 的增量绘制区间 [0,1000] 内时间解的速度 对于不同的 f 值。
也就是说,对于区间 [0,2] 中的每个 f(以 0.05 为增量),我将有大约 150 个速度点,并且我必须将所有这些点绘制在一张图表上。
我想过使用 do 循环并想出类似的东西
Remove["Global`*"]
b = .1;
w = 1;
Period = 1;
tstep = 2 Pi/Period;
Do[{Do[{data =
Table[Flatten[
Evaluate[{f,
x'[t] /.
NDSolve[{x''[t] + b x'[t] - x[t] + x[t]^3 - f Cos[w t] == 0,
x'[0] == 0, x[0] == 0}, x[t], {t, 0, 1000},
MaxSteps -> 59999]}]], {t, 0, 1000, tstep}]}, {t, 0, 1000,
1}]}, {f, 0, 2, .1}]
,但没有运气。
我该怎么做?
I needed some help with plotting a velocity vs forcing term diagram for a chaotic oscillator on mathematica.
Basically, I have to solve the following differential equation
x''[t] + b x'[t] - x[t] + x[t]^3 - f Cos[w t] == 0, x'[0] == 0,
x[0] == 0
and plot the velocity of my solution for times in the interval [0,1000] in increments of 2*Pi
for different values of f.
That is, for each f in the interval [0,2] (in increments of .05), I will have approximately 150 velocity points, and I must plot all of these points on one graph.
I though about using a do loop and came up with something like
Remove["Global`*"]
b = .1;
w = 1;
Period = 1;
tstep = 2 Pi/Period;
Do[{Do[{data =
Table[Flatten[
Evaluate[{f,
x'[t] /.
NDSolve[{x''[t] + b x'[t] - x[t] + x[t]^3 - f Cos[w t] == 0,
x'[0] == 0, x[0] == 0}, x[t], {t, 0, 1000},
MaxSteps -> 59999]}]], {t, 0, 1000, tstep}]}, {t, 0, 1000,
1}]}, {f, 0, 2, .1}]
but had no luck.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是你想要的吗?
解释
首先,让我重点关注
sol
。这与您自己的代码很接近(有一些更改),但为了清晰起见进行了重构,而不是隐藏在循环中。sol :=
相当于SetDelayed[sol, ...
NDSolve
因此,在某处使用sol
之前,不会执行 code> 操作。我所做的更改是从
NDSolve
的结果中提取这部分:我使用
Part< 执行此操作/代码>:
NDSolve[...][[1, 1, 2]]
也可以使用
x[t] / 来完成。首先 @ NDSolve[...]
这个提取的部分与列表中
f
的当前值配对:{f, NDSolve[ ... }
以便以后可以绘制它们。现在:
在全局更改
f
值时构建sol
变化值表。这是执行 NDSolve 的地方。结果是
f
的每个值的一系列解决方案,采用以下形式:最后:
通过评估上面为全局变化的
t
值创建的整个结果系列来创建一个表,ListPlot 就是它。还有几件事我想说,但我已经没时间了。我将在几个小时内进行进一步编辑。
Does this do what you want?
The Explanation
First, let me focus on
sol
. This is close to your own code (with a change) but refactored for clarity, rather than buried inside the loops.sol :=
is equivalent toSetDelayed[sol, ...
NDSolve
operation is therefore not performed untilsol
is used somewhereThe change I made was to extract this portion from the result of
NDSolve
:I do this with
Part
:NDSolve[...][[1, 1, 2]]
It could also be done with
x[t] /. First @ NDSolve[...]
This extracted portion is paired with the current value of
f
in a list:{f, NDSolve[ ... }
so that later they can be plotted.Now:
builds a table of the changing value of
sol
as it globally changes the value off
. This is where NDSolve is performed.The result is a series of solutions for each value of
f
in this form:Finally:
creates a table by evaluating the entire series of results created above for globally changing values of
t
, andListPlot
s it.There are a few things more I would like to say but I am out of time. I will make a further edit in a few hours.