需要帮助求解微分方程并绘制速度作为力项大小的函数

发布于 2024-11-07 17:42:32 字数 761 浏览 0 评论 0原文

我需要一些帮助来在 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 技术交流群。

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

发布评论

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

评论(1

放血 2024-11-14 17:42:32

这是你想要的吗?

b = .1;
w = 1;

sol := {f, 
  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][[1, 1, 2]]}

interpsols = Table[sol, {f, 0, 2, 0.1}];

ListPlot[Table[interpsols, {t, 0, 1000, 2 Pi}]]

解释

首先,让我重点关注 sol。这与您自己的代码很接近(有一些更改),但为了清晰起见进行了重构,而不是隐藏在循环中。

  • sol := 相当于 SetDelayed[sol, ...
  • 这保存了右侧给出的未计算的定义
  • NDSolve因此,在某处使用 sol 之前,不会执行 code> 操作。

我所做的更改是从 NDSolve 的结果中提取这部分:

InterpolatingFunction[{{0.,1000.}},<>][t]

我使用 Part< 执行此操作/代码>: NDSolve[...][[1, 1, 2]]

也可以使用 x[t] / 来完成。首先 @ NDSolve[...]

这个提取的部分与列表中 f 的当前值配对:{f, NDSolve[ ... }以便以后可以绘制它们。

现在:

interpsols = Table[sol, {f, 0, 2, 0.1}];

在全局更改 f 值时构建 sol 变化值表。这是执行 NDSolve 的地方。

结果是 f 的每个值的一系列解决方案,采用以下形式:

{{0.,InterpolatingFunction[{{0.,1000.}},<>][t]},
 {0.1,InterpolatingFunction[{{0.,1000.}},<>][t]},
 {0.2,InterpolatingFunction[{{0.,1000.}},<>][t]},
 {0.3,InterpolatingFunction[{{0.,1000.}},<>][t]},
 {0.4,InterpolatingFunction[{{0.,1000.}},<>][t]}
 ...

最后:

ListPlot[Table[interpsols, {t, 0, 1000, 2 Pi}]]

通过评估上面为全局变化的 t 值创建的整个结果系列来创建一个表,ListPlot 就是它。

还有几件事我想说,但我已经没时间了。我将在几个小时内进行进一步编辑。

Does this do what you want?

b = .1;
w = 1;

sol := {f, 
  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][[1, 1, 2]]}

interpsols = Table[sol, {f, 0, 2, 0.1}];

ListPlot[Table[interpsols, {t, 0, 1000, 2 Pi}]]

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 to SetDelayed[sol, ...
  • This holds the unevaluated definition that it is given on the right-hand-side
  • The NDSolve operation is therefore not performed until sol is used somewhere

The change I made was to extract this portion from the result of NDSolve:

InterpolatingFunction[{{0.,1000.}},<>][t]

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:

interpsols = Table[sol, {f, 0, 2, 0.1}];

builds a table of the changing value of sol as it globally changes the value of f. This is where NDSolve is performed.

The result is a series of solutions for each value of f in this form:

{{0.,InterpolatingFunction[{{0.,1000.}},<>][t]},
 {0.1,InterpolatingFunction[{{0.,1000.}},<>][t]},
 {0.2,InterpolatingFunction[{{0.,1000.}},<>][t]},
 {0.3,InterpolatingFunction[{{0.,1000.}},<>][t]},
 {0.4,InterpolatingFunction[{{0.,1000.}},<>][t]}
 ...

Finally:

ListPlot[Table[interpsols, {t, 0, 1000, 2 Pi}]]

creates a table by evaluating the entire series of results created above for globally changing values of t, and ListPlots 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文