绘制微分方程的解曲线

发布于 2024-11-26 11:16:20 字数 265 浏览 6 评论 0原文

我有一些微分方程,我想为它们绘制解,对于各种起始值 N_0

以下是方程:

dN\dt= bN^2 - aN

dN\dt = bN^2 (1 - N\K) - aN

我将如何解决?

我不太关心使用的语言。在专门的数学方面,我的计算机上有mathematica 和matlab。我可以访问枫树了。我必须做更多这样的事情,并且我想要任何语言的示例,因为它将帮助我找出我想要使用并学习哪一种语言。

I have a few differential equations that I'd like to draw solutions for, for a variety of start values N_0

Here are the equations:

dN\dt= bN^2 - aN

dN\dt = bN^2 (1 - N\K) - aN

How would I go about it?

I don't really care about the language is used. In terms of dedicated math I have mathematica and matlab on my computer. I've got access to maple. I have to do more of this stuff, and I'd like to have examples from any language, as it'll help me figure out which one I want to use and learn it.

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

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

发布评论

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

评论(4

狠疯拽 2024-12-03 11:16:20

我会假装第一个问题无法通过分析来解决,以便展示如何在数学中使用一般的常微分方程。

定义

p1[n0_, a_, b_, uplim_: 10] :=(n /. First@NDSolve[
      {n'[t] == b*n[t]^2 - a*n[t], n[0] == n0},n, {t, 0, uplim}]

返回 ODE 的解,即 a = p1[.1, 2., 3.],然后例如 a[.3] 告诉您n(.3)。然后,可以执行类似的操作

Show[Table[ans = p1[n0, 1, 1];
 Plot[ans[t], {t, 0, 10}, PlotRange \[Rule] Full],
 {n0, 0, 1, .05}], PlotRange \[Rule] {{0, 5}, {0, 1}}]

,绘制一些具有不同初始值的解决方案:

在此处输入图像描述

或者,获得一些见解在解决方案中,我们可以交互地操作 abn0 的值:

Manipulate[
 ans = p1[n0, a, b];
 Plot[ans[t], {t, 0, 10},PlotRange -> {0, 1}],
 {{n0, .1}, 0, 1},
 {{a, 1}, 0, 2},
 {{b, 1}, 0, 2}]

这给出了类似

在此处输入图像描述

控件处于活动状态(即移动它们并且绘图发生变化;实时尝试看看我的意思;注意您可以设置初始条件给出不同解决方案的参数)。

当然,这可以任意变得更复杂。此外,在这种特殊情况下,该 ODE 很容易进行分析积分,但该数值方法可以应用于通用 ODE(以及许多偏微分方程)。

I'll pretend the first one cannot be solved analytically so as to show how one would go about playing with a general ODE in mathematica.

Define

p1[n0_, a_, b_, uplim_: 10] :=(n /. First@NDSolve[
      {n'[t] == b*n[t]^2 - a*n[t], n[0] == n0},n, {t, 0, uplim}]

which returns the solution of the ODE, i.e., a = p1[.1, 2., 3.] and then e.g. a[.3] tells you n(.3). One can then do something like

Show[Table[ans = p1[n0, 1, 1];
 Plot[ans[t], {t, 0, 10}, PlotRange \[Rule] Full],
 {n0, 0, 1, .05}], PlotRange \[Rule] {{0, 5}, {0, 1}}]

which plots a few solutions with different initial values:

enter image description here

or, to gain some insight into the solutions, one can interactively manipulate the values of a, b and n0:

Manipulate[
 ans = p1[n0, a, b];
 Plot[ans[t], {t, 0, 10},PlotRange -> {0, 1}],
 {{n0, .1}, 0, 1},
 {{a, 1}, 0, 2},
 {{b, 1}, 0, 2}]

which gives something like

enter image description here

with the controls active (i.e. you move them and the plot changes; try it live to see what I mean; note that you can set parameters for which the initial conditions gives diverging solutions).

Of course this can be made arbitrarily more complicated. Also in this particular case this ODE is easy enough to integrate analytically, but this numerical approach can be applied to generic ODEs (and many PDEs, too).

不…忘初心 2024-12-03 11:16:20

除了几个好的答案之外,如果您只是想快速了解 ODE 对于许多起始值的解决方案,作为指导,您始终可以执行一行 StreamPlot。假设a==1b==1,以及dy/dx == x^2 - x

StreamPlot[{1, x^2 - x}, {x, -3, 3}, {y, -3, 3}]

在此处输入图像描述

StreamStyle -> “Line”只会给你线条,没有箭头。

Adding to the several good answers, if you just want a quick sketch of an ODE's solutions for many starting values, for guidance, you can always do a one-line StreamPlot. Suppose a==1 and b==1, and dy/dx == x^2 - x.

StreamPlot[{1, x^2 - x}, {x, -3, 3}, {y, -3, 3}]

enter image description here

StreamStyle -> "Line" will give you just lines, no arrows.

乱了心跳 2024-12-03 11:16:20

Mathematica中,你使用 NDSolve (除非它可以解析求解,在这种情况下你使用 DSolve。所以对于你的第一个方程,我尝试过:

b = 1.1; a = 2;
s = NDSolve[{n'[t] == b n[t]^2 - a n[t], n[0] == 1}, n, {t, 0, 10}];
Plot[Evaluate[n[t] /. s], {t, 1, 10}, PlotRange -> All]

我不知道 a、b 或 N0 使用什么,但我得到了这个结果:

Plot of n[t]

In Mathematica you use NDSolve (unless it can be solved analytically, in which case you use DSolve. So for your first equation I tried:

b = 1.1; a = 2;
s = NDSolve[{n'[t] == b n[t]^2 - a n[t], n[0] == 1}, n, {t, 0, 10}];
Plot[Evaluate[n[t] /. s], {t, 1, 10}, PlotRange -> All]

I didn't know what to use for a, b or N0, but I got this result:

Plot of n[t]

情定在深秋 2024-12-03 11:16:20

如果您愿意以数值方式求解方程,MATLAB 有一组可能有用的 ODE 求解器。 此处ode45 函数的文档>。

一般方法是定义一个描述微分方程右侧的“ode 函数”。然后,您可以将此函数以及初始条件和积分范围传递给ode 求解器。

这种方法的一个吸引人的特点是它可以直接扩展到耦合 ODE 的复杂系统。

希望这有帮助。

If you're happy to solve the equations numerically, MATLAB has a set of ODE solvers that might be useful. Check out the documentation for the ode45 function here.

The general approach is to define an "ode function" that describes the right-hand-side of the differential equations. You then pass this function, along with initial conditions and an integration range to the ode solvers.

One attractive feature of this type of approach is that it extends in a straight-forward way to complex systems of coupled ODE's.

Hope this helps.

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