NDSolveValue[eqns,expr,{x,xmin,xmax}]
根据含有独立变量 x 的常微分方程 eqns 的数值解,给出函数表达式 expr 的值,其范围为 xmin 到 xmax.
NDSolveValue[eqns,expr,{x,xmin,xmax},{y,ymin,ymax}]
在矩形区域内求解偏微分方程 eqns.
NDSolveValue[eqns,expr,{x,y}∈Ω]
在区域 Ω 内求解偏微分方程 eqns.
NDSolveValue[eqns,u,{t,tmin,tmax},{x,y}∈Ω]
在区域 Ω 内求解时变偏微分方程 eqns.
NDSolveValue
NDSolveValue[eqns,expr,{x,xmin,xmax}]
根据含有独立变量 x 的常微分方程 eqns 的数值解,给出函数表达式 expr 的值,其范围为 xmin 到 xmax.
NDSolveValue[eqns,expr,{x,xmin,xmax},{y,ymin,ymax}]
在矩形区域内求解偏微分方程 eqns.
NDSolveValue[eqns,expr,{x,y}∈Ω]
在区域 Ω 内求解偏微分方程 eqns.
NDSolveValue[eqns,u,{t,tmin,tmax},{x,y}∈Ω]
在区域 Ω 内求解时变偏微分方程 eqns.
更多信息和选项
- NDSolveValue[eqns,y[x],{x,xmin,xmax}] 给出 y[x] 的解,而不是函数 y 本身.
- 微分方程必须以导数例如 y'[x] 的形式说明,用 D 获得,而不是用 Dt 得到的总导数.
- 可以用微分运算符 Grad (∇)、Div (∇.)、Laplacian (∇2) 和 Curl (∇) 来指定偏微分方程. 一般情况下,这些运算符像在 Inactive[op] 中一样,作为算子形式存在而不被计算.
- NDSolveValue 求解广范围的常微分方程以及许多偏微分方程.
- NDSolveValue 也可以求解许多时滞微分方程.
- 在常微分方程中,函数 yi 必须只依赖于单个变量 x. 在偏微分方程中,它们可能依赖多个变量.
- 微分方程必须包括足够的初始或边界条件,才能完全决定 yi 的解.
- 初始和边界条件一般以形式 y[x0]==c0、y'[x0]==dc0 等给出,但可能由更复杂的方程组成.
- c0、dc0 等可以是列表,指定 y[x] 是具有向量或通用列表值的函数.
- 周期边界条件可用 y[x0]==y[x1] 指定.
- 初始或边界条件中的 x0 不必位于求解范围 xmin 到 xmax.
- 在时滞微分方程中,初始历史函数以 y[x/;x<x0]==c0 形式给出,其中 c0 一般是 x 的函数.
- WhenEvent[event,action] 可能包含在方程 eqns 中,用来指定当 event 变成 True 时发生的 action.
- 可以用 DirichletCondition 和 NeumannValue 来指定边界值.
- NDSolveValue 中的微分方程可包含复数.
- NDSolveValue 可求解许多微分代数方程组,某些 eqns 是纯代数的或某些变量是隐式代数.
- yi 可以是因变量的函数,不需要包括所有这样的变量.
- 可以给出以下选项:
-
AccuracyGoal Automatic 寻求的绝对精确数字 Compiled Automatic 表达式是否应该自动编译 DependentVariables Automatic 所有相关变量的列表 EvaluationMonitor None 当计算函数时计算的的表达式 InitialSeeding {} 用于某些算法的播种方程 InterpolationOrder Automatic 最终输出的连续度 MaxStepFraction 1/10 每个步骤中最大的范围比例 MaxSteps Automatic 采用的最大步骤数 MaxStepSize Automatic 每一步的最大尺寸 Method Automatic 使用的方法 NormFunction Automatic 误差估计使用的范数 PrecisionGoal Automatic 寻求的精确数字 StartingStepSize Automatic 使用的初始步骤大小 StepMonitor None 每采取步骤时计算的表达式 WorkingPrecision MachinePrecision 内部计算使用的精度 - NDSolveValue 会调整它的步骤大小,因此解中的估计误差恰好在由 PrecisionGoal 和 AccuracyGoal 指定的公差之内.
- 选项 NormFunction->f 指定每个 yi 的估计误差应使用 f[{e1,e2,…}] 组合.
- AccuracyGoal 有效的指定在求解中每一步允许的绝对局部误差,其中 PrecisionGoal 指定相对局部误差.
- 如果当解的值接近零时,必须准确求解的话,AccuracyGoal 应设为大一些,或为 Infinity.
- AccuracyGoal 和 PrecisionGoal 的 Automatic 的默认设置等同于 WorkingPrecision/2.
- MaxSteps 的默认设置 Automatic 根据开始和终止时间,以及每个步骤的大小,估计 NDSolve 所需的最大步骤数. 如果结果不可行,则采用固定步骤数.
- MaxStepFraction 的设置指定由 NDSolve 采取的最大步骤,作为每个独立变量值的范围比例.
- 当 DependentVariables->Automatic 时,NDSolve 通过分析给定的方程,试图决定相关变量.
- NDSolveValue 一般通过依赖于方程类型的多个不同阶段求解微分方程. 当 Method->{s1->m1,s2->m2,…},阶段 si 由方法 mi 处理. 实际使用的阶段和它们的顺序由基于要求解的问题的 NDSolve 决定.
- 可能的求解阶段为:
-
"TimeIntegration" 微分方程组的时间积分 "BoundaryValues" 常微分方程边界值解 "DiscontinuityProcessing" 处理不连续微分方程的符号式处理 "EquationSimplification" 数值计算的方程形式化简 "IndexReduction" 微分代数方程的符号索引化简 "DAEInitialization" 微分代数方程的一致初始化 "PDEDiscretization" 偏微分方程的离散化 - 当 Method->m1 或 Method->{m1,s2->m2,…},方法 m1 假设为时间积分,因此 Method->m1 等同于 Method->{"TimeIntegration"->m1}.
- Method 选项的可能显式时间积分设置包括:
-
"Adams" 预测-校正亚当斯法,阶数从 1 至 12 "BDF" 隐式向后差分公式,阶数为 1 至 5 "ExplicitRungeKutta" 2(1) 至 9(8) 自适应嵌入对龙格库塔法 "ImplicitRungeKutta" 任意阶数隐式龙格库塔法系列 "SymplecticPartitionedRungeKutta" 可分哈密顿系统的交错龙格库塔法 - 当 Method->{"controller",Method->"submethod"} 或Method->{"controller",Method->{m1,m2,…}},可能的控制器方法包括:
-
"Composition" 构成子方法的列表 "DoubleStep" 双步法自适应步骤尺寸 "EventLocator" 响应指定的事件 "Extrapolation" 使用多项式外推自适应阶数和步骤大小 "FixedStep" 使用常量步骤大小 "OrthogonalProjection" 满足正交约束的投影解 "Projection" 满足一般约束的投影解 "Splitting" 分开方程组和是哦那个不同的子方法 "StiffnessSwitching" 如果检测到刚度从显式切换到隐式 - 主要用作子方法的方法包括:
-
"ExplicitEuler" 向前欧拉方法 "ExplicitMidpoint" 中点规则方法 "ExplicitModifiedMidpoint" 具有 Gragg 平滑的中点规则法 "LinearlyImplicitEuler" 线性隐式欧拉方法 "LinearlyImplicitMidpoint" 线性隐式中点规则法 "LinearlyImplicitModifiedMidpoint" 线性隐式 Bader 平滑中点规则法 "LocallyExact" 数值近似到本地精确符号式解 - 设置 InterpolationOrder->All 指定 NDSolve 应该产生使用与基本使用方法具有同样阶数的插值的解. »
范例
打开所有单元 关闭所有单元基本范例 (7)
ysol = NDSolveValue[{y'[x] == y[x]Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}]Plot[ysol[x], {x, 0, 30}, PlotRange -> All]ParametricPlot[{ysol[x], ysol'[x]}, {x, 0, 20}]{ysol[10.5], ysol'[12.5]}NDSolveValue 也可以直接给出上述数值:
NDSolveValue[{y'[x] == y[x]Cos[x + y[x]], y[0] == 1}, {y[10.5], y'[12.5]}, {x, 0, 30}]ysol = NDSolveValue[{y''[x] + Sin[y[x]]y[x] == 0, y[0] == 1, y'[0] == 0}, y, {x, 0, 30}]Plot[{ysol[x], ysol'[x], ysol''[x]}, {x, 0, 30}]或者将要绘制的函数直接代入 NDSolveValue:
Plot[Evaluate[NDSolveValue[{y''[x] + Sin[y[x]]y[x] == 0, y[0] == 1, y'[0] == 0}, {y[x], y'[x], y''[x]}, {x, 0, 30}]], {x, 0, 30}]{xsol, ysol} = NDSolveValue[{x'[t] == -y[t] - x[t] ^ 2, y'[t] == 2x[t] - y[t] ^ 3, x[0] == y[0] == 1}, {x, y}, {t, 20}]ParametricPlot[{xsol[t], ysol[t]}, {t, 0, 20}]usol = NDSolveValue[{D[u[t, x], t] == D[u[t, x], x, x], u[0, x] == 0, u[t, 0] == Sin[t], u[t, 5] == 0}, u, {t, 0, 10}, {x, 0, 5}]Plot3D[usol[t, x], {t, 0, 10}, {x, 0, 5}, PlotRange -> All]NDSolveValue[{Subscript[∂, t]u[t, x] == Subscript[∂, x, x]u[t, x], u[0, x] == 0, u[t, 0] == Sin[t], u[t, 5] == 0}, u, {t, 0, 10}, {x, 0, 5}]求解 Disk 上的泊松方程:
NDSolveValue[{-Laplacian[u[x, y], {x, y}] == 1, DirichletCondition[u[x, y] == 0, True]}, u[x, y], {x, y}∈Disk[]]Plot3D[%, {x, y}∈Disk[]]求带有正弦边界条件 Disk 上的最小曲面:
NDSolveValue[{-Inactive[Div][(1/Sqrt[1 + Grad[u[x, y], {x, y}] . Grad[u[x, y], {x, y}]])*
Inactive[Grad][u[x, y], {x, y}], {x, y}] == 0, DirichletCondition[u[x, y] == Sin[2π * (x + y)], True]}, u[x, y], {x, y}∈Disk[]]Plot3D[%, {x, y}∈Disk[]]Ω = Polygon[2{{-Pi, -E}, {0, -1}, {0, 0}, {Pi, 0}, {Pi, E}, {-Pi, E}}];
NDSolveValue[{Laplacian[u[x, y], {x, y}] + λ Cos[v[x, y]]Sin[u[x, y]] == 0, Laplacian[v[x, y], {x, y}] + λ Sin[u[x, y]]Sin[v[x, y]] == 0, DirichletCondition[{u[x, y] == 1 / 10, v[x, y] == 1 / 20}, True]} /. λ -> 5, {u[x, y], v[x, y]}, {x, y}∈Ω]Plot3D[%, {x, y}∈Ω]范围 (25)
常微分方程组 (7)
xsol = NDSolveValue[{x''[t] + 1 / 10x'[t] + Sin[x[t]] == 1 / 2Cos[t], x[0] == x'[0] == 0}, x, {t, 0, 100}]ParametricPlot[{xsol[t], xsol'[t]}, {t, 0, 100}, ColorFunction -> Hue]{xsol, ysol, zsol} = NDSolveValue[{Derivative[1][x][t] == -3(x[t] - y[t]), Derivative[1][y][t] == -x[t]z[t] + 26.5x[t] - y[t], Derivative[1][z][t] == x[t]y[t] - z[t], x[0] == z[0] == 0, y[0] == 1}, {x, y, z}, {t, 0, 5}]Plot[{xsol[t], ysol[t], zsol[t]}, {t, 0, 5}]ysol = NDSolveValue[{Derivative[2][y][x] + (| | | | |
| :- | :- | :- | :- |
| 1 | 1 | 1 | 1 |
| 1 | 2 | 1 | 2 |
| 1 | 1 | 3 | 1 |
| 1 | 2 | 1 | 4 |).y[x] == 0, y[0] == Derivative[1][y][0] == {1, 1, 1, 1}}, y, {x, 0, 8}]ysol[5]Plot[ysol[x], {x, 0, 8}]xsol = NDSolveValue[{x''[t] + x[t] == 0, x[0] == 1, x'[0] == 0}, x, {t, 10}]Plot[Evaluate[{xsol[t], xsol'[t]}], {t, 0, 10}]{xsol, ysol} = NDSolveValue[{x'[t] == y[t], y'[t] == -x[t], x[0] == 1, y[0] == 0}, {x, y}, {t, 0, 10}]Plot[Evaluate[{xsol[t], ysol[t]}], {t, 0, 10}]zsol = NDSolveValue[{z'[t] == {{0, 1}, {-1, 0}}.z[t], z[0] == {1, 0}}, z, {t, 0, 10}]Plot[zsol[t], {t, 0, 10}]A = RandomReal[{0, 1}, {5, 5}];fs = NDSolveValue[{x'[t] == A.x[t], x[0] == IdentityMatrix[5]}, x, {t, 0, 1}]Plot[Norm[Flatten[fs[t] - MatrixExp[A t]]], {t, 0, 1}]vdp = {x'[t] == y[t], y'[t] == -x[t] + 1000(1 - x[t] ^ 2)y[t], x[0] == 2, y[0] == 0};{xsol, ysol} = NDSolveValue[vdp, {x, y}, {t, 2000}]Plot[Evaluate[xsol[t]], {t, 0, 2000}, PlotRange -> All]NDSolveValue[{Derivative[1][x][t] == y[t], Derivative[1][y][t] == -x[t] + 1000(1 - x[t]^2)y[t], x[0] == 2, y[0] == 0}, {x, y}, {t, 2000}, Method -> "ExplicitRungeKutta"]解 ysol[x] 是连续的,因为它对分段函数只进行一次积分:
ysol = NDSolveValue[{y'[x] + Cos[y[x]] == Floor[x], y[0] == 1}, y, {x, 0, 3}]Plot[ysol[x], {x, 0, 3}]解 ysol[x] 是可微的,然而 ysol'[x] 只是连续的:
ysol = NDSolveValue[{y''[x] + y[x] == Floor[x], y[0] == y'[0] == 1}, y, {x, 0, 5}]Plot[{ysol[x], ysol'[x]}, {x, 0, 5}]偏微分方程组 (5)
usol = NDSolveValue[{D[u[t, x], t] == 0.5D[u[t, x], x, x] + u[t, x]D[u[t, x], x], u[t, -Pi] == u[t, Pi] == 0, u[0, x] == Sin[x]}, u, {t, 0, 2}, {x, -Pi, Pi}]Plot3D[usol[t, x], {t, 0, 2}, {x, -Pi, Pi}, PlotRange -> All]pde = {Subscript[∂, t]u[t, x] == Subscript[∂, x]((v[t, x] - 1)Subscript[∂, x]u[t, x]) + (16x t - 2t - 16(v[t, x] - 1))(u[t, x] - 1) + 10x E^-4x, Subscript[∂, t]v[t, x] == Subscript[∂, {x, 2}]v[t, x] + Subscript[∂, x]u[t, x] + 4u[t, x] - 4 + x^2 - 2t - 10t E^-4x};bc = {u[0, x] == 1, v[0, x] == 1, u[t, 0] == 1, v[t, 0] == 1, 3u[t, 1] + u^(0, 1)[t, 1] == 3, 5v^(0, 1)[t, 1] == E^4(u[t, 1] - 1)};{usol, vsol} = NDSolveValue[{pde, bc}, {u, v}, {x, 0, 1}, {t, 0, 2}]Plot3D[{usol[t, x], vsol[t, x]}, {x, 0, 1}, {t, 0, 2}, PlotStyle -> {Directive[Opacity[0.7], Red], Directive[Opacity[0.7], Blue]}]具有周期性边界条件的两个空间维度的非线性正弦-Gordon 方程:
L = 4;
usol = NDSolveValue[{D[u[t, x, y], t, t] == D[u[t, x, y], x, x] + D[u[t, x, y], y, y] + Sin[u[t, x, y]], u[t, -L, y] == u[t, L, y], u[t, x, -L] == u[t, x, L], u[0, x, y] == Exp[-(x ^ 2 + y ^ 2)], Derivative[1, 0, 0][u][0, x, y] == 0}, u, {t, 0, L / 2}, {x, -L, L}, {y, -L, L}]Plot3D[usol[L / 2, x, y], {x, -L, L}, {y, -L, L}]Plot3D[usol[t, x, 0], {t, 0, L / 2}, {x, 0, L}]Ω = RegionDifference[RegionDifference[Rectangle[{0, 0}, {2, 1}], Rectangle[{9 / 10, 0}, {11 / 10, 4 / 10}]], Rectangle[{9 / 10, 6 / 10}, {11 / 10, 1}]];
sol = NDSolveValue[{D[u[t, x, y], {t, 2}] - Laplacian[u[t, x, y], {x, y}] == 0, DirichletCondition[u[t, x, y] == 0, True], u[0, x, y] == 2 * Exp[-125((x - 0.25) ^ 2 + (y - 0.5) ^ 2)], Derivative[1, 0, 0][u][0, x, y] == 0}, u, {t, 0, 2}, {x, y}∈Ω]ListAnimate[Table[Rasterize[Plot3D[sol[t, x, y], {x, y}∈Ω, PlotRange -> {-0.75, 2}, AspectRatio -> Automatic]], {t, 0, 2, 1 / 25}], SaveDefinitions -> True]Ω = RegionDifference[RegionUnion[Disk[], Rectangle[{0, -1}, {2, 1}]], Disk[{2, 0}]];ufun = NDSolveValue[{-Subsuperscript[∇, {x, y}, 2]u[x, y] == 1., PeriodicBoundaryCondition[u[x, y], (x - 2) ^ 2 + y ^ 2 == 1, Function[x, x - {2, 0}]], DirichletCondition[u[x, y] == 0, (0 < x < (2 - 10 ^ -6) && (y ≤ -1 || y ≥ 1))]}, u, {x, y}∈Ω];ContourPlot[ufun[x, y], {x, y}∈Ω, ColorFunction -> "TemperatureMap", AspectRatio -> Automatic]边界值问题 (5)
{xsol, ysol} = NDSolveValue[{x''[t] == y[t]x[t], y'[t] == 2 - x[t], x[0] == x[4] == 1, y[1] == 1}, {x, y}, t]Plot[{xsol[t], ysol[t]}, {t, 0, 4}, Epilog -> {Point[{0, 1}], Point[{4, 1}], Red, Point[{1, 1}]}]求解非线性扩散方程
,其中狄利克雷和诺伊曼条件开始于
的初始种子:
sol = NDSolveValue[{Inactive[Div][(-u[x]^2)*Inactive[Grad][u[x], {x}], {x}] == 4 + NeumannValue[2., x == 1], DirichletCondition[u[x] == 1., x == 0]}, u, {x}∈Line[{{0}, {1}}], InitialSeeding -> {u[x] == 1}]Plot[sol[x], {x, 0, 1}]求解非线性方程
,其中狄利克雷和诺伊曼条件开始于
的初始种子:
sol = NDSolveValue[{Subsuperscript[∇, {x}, 2]u[x] + 4u[x]D[u[x], x] == 2, DirichletCondition[u[x] == 1., True]}, u, {x}∈Line[{{0}, {1}}], InitialSeeding -> {u[x] == 1}]Plot[sol[x], {x, 0, 1}]sol = NDSolveValue[{-Subsuperscript[∇, {x}, 2]u[x] + I u[x] ^ 2 == 10, DirichletCondition[u[x] == -6I, x == 0], DirichletCondition[u[x] == -Sqrt[2]I, x == 1]}, u, {x}∈Line[{{0}, {1}}]]ReImPlot[sol[x], {x, 0, 1}]sol = NDSolveValue[{Subsuperscript[∇, {x}, 2]u[x] == Sin[u[x]], DirichletCondition[u[x] == 5., x == 0], DirichletCondition[u[x] == 5.1, x == π / 2]}, u, {x}∈Line[{{0}, {π / 2}}]]Plot[sol[x], {x, 0, π / 2}]时滞微分方程 (2)
xsol = NDSolveValue[{x'[t] == x[t](x[t - Pi] - x'[t - 1]), x[t /; t ≤ 0] == Cos[t]}, x, {t, 0, 8}]Plot[Evaluate[{xsol[t], xsol'[t]}], {t, 0, 8}, PlotRange -> All]Manipulate[
Module[{sol, y, t},
sol = NDSolveValue[{y'[t] == λ y[t] + μ y[t - 1], y[t /; t ≤ 0] == 1 - t}, y,
{t, 0, 10}];
If[pp, ParametricPlot[{sol[t], sol[t - 1]}, {t, 1, 10}, PlotRange -> {{-3, 3}, {-3, 3}}],
Plot[sol[t], {t, 0, 10}, PlotRange -> {{0, 10}, {-3, 3}}]]], {{pp, False, "Plot in phase plane"}, {True, False}}, {{λ, -1}, -5, 5}, {{μ, 1}, -5, 5}, FrameLabel -> ToString[y'[t] == λ y[t] + μ y[t - 1], TraditionalForm]]混合及不连续系统 (5)
ifun = NDSolveValue[{y'[t] == -Sign[y[t]], y[0] == 1}, y[t], {t, 0, 2}];Plot[ifun, {t, 0, 2}, PlotRange -> All]ifun = NDSolveValue[{y'[t] == Piecewise[{{.5, 0 ≤ y[t] ≤ 1}, {1, 1 < y[t] ≤ 2}}, 0], y[0] == 0.5}, y[t], {t, 0, 3}];Plot[ifun, {t, 0, 3}]ifun = NDSolveValue[{y'[t] == a[t], y[0] == 0, a[0] == 1, WhenEvent[Mod[t, 1] == 0, a[t] -> -a[t]]}, y[t], {t, 0, 5}, DiscreteVariables -> a[t]];Plot[ifun, {t, 0, 5}]de = {x'[t] == y[t], y'[t] == -x[t] + .2y[t] + 1};
ic = {x[0] == .5, y[0] == .5};ifun = NDSolveValue[{de, ic, WhenEvent[And[y[t] == 0, x[t] < 0], x[t] -> -x[t]]}, {x[t], y[t]}, {t, 0, 1000}];ParametricPlot[ifun, {t, 0, 200}, PlotPoints -> 200]Manipulate[Module[{x0, y0, xsol, ysol},
{x0, y0} = p;{xsol, ysol} = NDSolveValue[{x'[t] == y[t], y'[t] == If[y[t] - 1 > 0, -1, .1y[t] - x[t]], x[0] == x0, y[0] == y0}, {x, y}, {t, 0, 20}];
ParametricPlot[{xsol[t], ysol[t]}, {t, 0, 20}, PlotRange -> {{-1.5, 1.5}, {-1.5, 1.5}}]],
{{p, {-1.5, 1.5}}, Locator}]推广和延伸 (1)
eqns = {Table[Derivative[1][y[i]][x] == y[i - 1][x] - y[i][x], {i, 2, 4}], {Derivative[1][y[1]][x] == -y[1][x], Derivative[1][y[5]][x] == y[4][x], y[1][0] == 1}, Table[y[i][0] == 0, {i, 2, 5}]}sol = NDSolveValue[eqns, Table[y[i], {i, 5}], {x, 10}]Plot[Evaluate[Table[sol[[i]][x], {i, 5}]], {x, 0, 10}]选项 (29)
AccuracyGoal 与 PrecisionGoal (1)
eqn = z''[t] == -z[t] / (z[t]^2 + ((1 + Sin[2π t] / 2) / 2)^2)^3 / 2;Plot[Evaluate[NDSolveValue[{eqn, z[0] == 1, z'[0] == 0}, z[t], {t, 0, 40}]], {t, 0, 40}]Plot[Evaluate[NDSolveValue[{eqn, z[0] == 1, z'[0] == 0}, z[t], {t, 0, 40}, AccuracyGoal -> 10, PrecisionGoal -> 10]], {t, 0, 40}]Plot[Evaluate[NDSolveValue[{eqn, z[0] == 1, z'[0] == 0}, z[t], {t, 0, 40}, AccuracyGoal -> 20, PrecisionGoal -> 20, WorkingPrecision -> 35, MaxSteps -> Infinity]], {t, 0, 40}]DependentVariables (1)
n = 1000;
vars = Table[Subscript[x, i][t], {i, n}];
eqns = Table[j = Mod[i, n] + 1;{Subscript[x, i]'[t] == 1 / (Subscript[x, i][t] + Subscript[x, j][t])^2, Subscript[x, i][0] == 1 / i}, {i, n}];Short[eqns, 3]x1sol = NDSolveValue[eqns, Subscript[x, 1], {t, 0, 100}, DependentVariables -> vars]Plot[x1sol[t], {t, 0, 100}, PlotRange -> All]EvaluationMonitor (2)
Module[{c = 0}, NDSolveValue[{y'[x] == y[x]Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}, EvaluationMonitor :> c++];c]ListLinePlot[Differences[Reap[NDSolveValue[{y'[x] == y[x]Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}, EvaluationMonitor :> Sow[x]]][[2, 1]]]]InitialSeedings (2)
NDSolveValue[{-2u[x]Derivative[1][u][x] - Derivative[2][u][x] == 1, DirichletCondition[u[x] == 0, x == 0], DirichletCondition[u[x] == 0, x == 1]}, u, {x}∈Line[{{0}, {1}}], InitialSeeding -> {u[x] == 0}]NDSolveValue[{-2u[x]Derivative[1][u][x] - Derivative[2][u][x] == 0, DirichletCondition[u[x] == 0, x == 0], DirichletCondition[u[x] == 1, x == 1]}, u, {x}∈Line[{{0}, {1}}], InitialSeeding -> {u[x] == x}]InterpolationOrder (1)
使用 InterpolationOrder->All 获取与方法同样阶数的插值:
Timing[{xa, ya} = NDSolveValue[{x'[t] == y[t], y'[t] == -x[t], x[0] == 1, y[0] == 0}, {x, y}, {t, 0, 1000}, InterpolationOrder -> All]]Timing[{xd, yd} = NDSolveValue[{x'[t] == y[t], y'[t] == -x[t], x[0] == 1, y[0] == 0}, {x, y}, {t, 0, 1000}]]Plot[Evaluate[{xa[t], xd[t]} - Cos[t]], {t, 0, 10}, PlotRange -> All, PlotStyle -> {Directive[Thick, Blue], Red}]MaxStepFraction (1)
Table[Plot[Evaluate[NDSolveValue[{y''[x] + y[x] == Exp[-(1000(x / L - 1 / 3)) ^ 2], y[0] == y'[0] == 0}, y[x], {x, L}]], {x, L / 4, L / 2}, PlotRange -> All], {L, {1, 10, 100, 1000}}]使用 MaxStepFraction 确保不错过特征,和区间大小无关:
Table[Plot[Evaluate[NDSolveValue[{y''[x] + y[x] == Exp[-(1000(x / L - 1 / 3)) ^ 2], y[0] == y'[0] == 0}, y[x], {x, L}, MaxStepFraction -> 0.001]], {x, L / 4, L / 2}, PlotRange -> All], {L, {1, 10, 100, 1000}}]MaxSteps (1)
NDSolveValue[{y''[x] + x y[x] == 0, y[0] == 1, y'[0] == 0}, y, {x, 0, 200}]ysol = NDSolveValue[{y''[x] + x y[x] == 0, y[0] == 1, y'[0] == 0}, y, {x, 0, 200}, MaxSteps -> 10 ^ 6]ParametricPlot[Evaluate[{ysol[x], ysol'[x]}], {x, 0, 200}, ColorFunction -> Function[Hue[#3]], AspectRatio -> 1]MaxStepSize (2)
NDSolveValue[{y''[x] + (1 + Sech[1000(x - Pi)])y[x] == 0, y[0] == 1, y'[0] == 0}, y, {x, 0, 10}][x]Plot[Evaluate[NDSolveValue[{y''[x] + (1 + Sech[1000(x - Pi)])y[x] == 0, y[0] == 1, y'[0] == 0}, y, {x, 0, 10}][x] - Cos[x]], {x, 0, 10}]更小的 MaxStepSize 设置保证 NDSolveValue 获取特征:
Plot[Evaluate[NDSolveValue[{y''[x] + (1 + Sech[1000(x - Pi)])y[x] == 0, y[0] == 1, y'[0] == 0}, y, {x, 0, 10}, MaxStepSize -> 0.01][x] - Cos[x]], {x, 0, 10}]Block[{n = 0}, NDSolveValue[{y'[t] == y[t], y[-1] == E^-1, WhenEvent[Sin[π y[t]] == 0, n++]}, y, {t, 5}];n]设置足够小的 MaxStepSize 保证不会错过任何事件:
Block[{n = 0}, NDSolveValue[{y'[t] == y[t], y[-1] == E^-1, WhenEvent[Sin[π y[t]] == 0, n++]}, y, {t, 5}, MaxStepSize -> 0.001];n]Method (12)
TimeIntegration (5)
ListLinePlot[Differences[Reap[NDSolveValue[{y'[x] == y[x]Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}, StepMonitor :> Sow[x]]][[2, 1]]]]ListLinePlot[Differences[Reap[NDSolveValue[{y'[x] == y[x]Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}, StepMonitor :> Sow[x], Method -> "ExplicitRungeKutta"]][[2, 1]]]]ListLinePlot[Differences[Reap[NDSolveValue[{y'[x] == y[x]Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}, StepMonitor :> Sow[x], Method -> {"ExplicitRungeKutta", "DifferenceOrder" -> 8}]][[2, 1]]]]ListLinePlot[Differences[Reap[NDSolveValue[{y'[x] == y[x]Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}, StepMonitor :> Sow[x], Method -> {"ExplicitRungeKutta", "DifferenceOrder" -> 3}]][[2, 1]]]]ListLinePlot[Differences[Reap[NDSolveValue[{y'[x] == y[x]Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}, StepMonitor :> Sow[x], Method -> "Extrapolation"]][[2, 1]]]]BoundaryValues (1)
xsol0 = NDSolveValue[{x''[t] + Sin[x[t]] == 0, x[0] == x[10] == 0}, x, t]Table[xsol0[t], {t, 0, 10}]为 "Shooting" 解法指定不同的起始条件来求出不同的解:
xsols = Map[NDSolveValue[{x''[t] + Sin[x[t]] == 0, x[0] == x[10] == 0}, x, t, Method -> {"Shooting", "StartingInitialConditions" -> {x[0] == 0, x'[0] == #}}]&,
{1.5, 1.75, 2}];
Plot[Evaluate[Through[xsols[t]]], {t, 0, 10}, PlotStyle -> {StandardGray, Blue, Green}]DiscontinuityProcessing (1)
NDSolveValue 会自动处理像 Sign 这样的不连续函数:
xsol1 = NDSolveValue[{x'[t] == Sign[1 - x[t]], x[0] == 0}, x, {t, 0, 2}]如果相应的选项被设置为无效的话,碰到断点,NDSolveValue 可能会无法继续进行计算:
NDSolveValue[{x'[t] == Sign[1 - x[t]], x[0] == 0}, x, {t, 0, 2}, Method -> {"DiscontinuityProcessing" -> False}]xsol2 = NDSolveValue[{x'[t] == Sign[1 - x[t]], x[0] == 0}, x, {t, 0, 2}, Method -> {"DiscontinuityProcessing" -> False, "TimeIntegration" -> "Extrapolation"}]Plot[{xsol1[t], xsol2[t]}, {t, 0, 2}]另一个办法是利用 "DiscontinuitySignature" 求解:
xsol3 = NDSolveValue[{Derivative[1][x][t] == v[t], WhenEvent[1 == x[t], v[t] -> "DiscontinuitySignature"], x[0] == 0, v[0] == 1}, x, {t, 0, 2}, DiscreteVariables -> {Element[v, {-1, 0, 1}]}]Table[(xsol1[t]) - (xsol3[t]), {t, 0, 2, .25}]EquationSimplification (1)
NDSolveValue[{Derivative[1][x][t] ^ 2 + x[t] ^ 2 == 1, x[0] == 1 / 2}, x, {t, 0, 10Pi}]xsol1 = NDSolveValue[{Derivative[1][x][t] ^ 2 + x[t] ^ 2 == 1, x[0] == 1 / 2}, x, {t, 0, 10Pi}, Method -> {"EquationSimplification" -> "Residual"}]xsol2 = NDSolveValue[{Derivative[1][x][t] ^ 2 + x[t] ^ 2 == 1, x[0] == 1 / 2, x'[0] == -Sqrt[3 / 4]}, x, {t, 0, 10Pi}, Method -> {"EquationSimplification" -> "Residual"}]Plot[{xsol1[t], xsol2[t]}, {t, 0, 10Pi}]IndexReduction (1)
eqns = {x''[t] == T[t]x[t], y''[t] == T[t]y[t] - 1, x[t] ^ 2 + y[t] ^ 2 == 1};
init = {x[0] == 1, y[0] == 0, y'[0] == -14 / 10};NDSolveValue[{eqns, init}, {T, x, y}, {t, 0, 10}]s1 = NDSolveValue[{x''[t] == T[t]x[t], y''[t] == T[t]y[t] - 1, x[t] ^ 2 + y[t] ^ 2 == 1, x[0] == 1, y[0] == 0, y'[0] == -14 / 10}, {T, x, y}, {t, 0, 10}, Method -> {"IndexReduction" -> True}]s2 = NDSolveValue[{x''[t] == T[t]x[t], y''[t] == T[t]y[t] - 1, x[t] ^ 2 + y[t] ^ 2 == 1, x[0] == 1, y[0] == 0, y'[0] == -14 / 10}, {T, x, y}, {t, 0, 10}, Method -> {"IndexReduction" -> {True, "ConstraintMethod" -> "Projection"}}]energy[sol_] := Module[{T, x, y, times, es},
{T, x, y} = sol;times = Part[x, 3, 1];es[t_] = 1 + y[t] + (1/2)(-y[t]Derivative[1][x][t] + x[t]Derivative[1][y][t])^2;Transpose[{times, es[times] - es[0]}]];ListPlot[{energy[s1], energy[s2]}]DAEInitialization (1)
用前向配置法来初始化以避免在 0 点处有 Abs 项的问题:
NDSolveValue[{x'[t] == Abs[u[t]], u[t] == Sin[t], y[t] == x[t], y[0] == 10}, {x, y, u}, {t, 0, 10}, Method -> {"DAEInitialization" -> {"Collocation", "CollocationDirection" -> "Forward"}}]PDEDiscretization (2)
usol1 = NDSolveValue[{Subscript[∂, t]u[t, x] == (1/1000)Subscript[∂, x, x]u[t, x] - u[t, x]Subscript[∂, x]u[t, x], u[0, x] == Sin[2π x], u[t, 0] == u[t, 1]}, u, {t, 0, 2}, {x, 0, 1}]
end = Part[usol1, 1, 1, -1];
Plot[usol1[end, x], {x, 0, 1}]usol2 = NDSolveValue[{Subscript[∂, t]u[t, x] == (1/1000)Subscript[∂, x, x]u[t, x] - u[t, x]Subscript[∂, x]u[t, x], u[0, x] == Sin[2π x], u[t, 0] == u[t, 1]}, u, {t, 0, 2}, {x, 0, 1}, Method -> {"PDEDiscretization" -> {"MethodOfLines", "SpatialDiscretization" -> {"TensorProductGrid", "MinPoints" -> 1000}}}]end = Part[usol2, 1, 1, -1];
Plot[usol2[end, x], {x, 0, 1}]X = Part[usol2, 3, 2];
Plot[Max[usol2[t, X]], {t, 0, 2}]solution = NDSolveValue[{Inactive[Div][{{-(1/2)}} . Inactive[Grad][u[t, x], {x}], {x}] == I u^(1, 0)[t, x], u[0, x] == E^(-3I x - (1/20) x^2), DirichletCondition[u[t, x] == 0, True]}, u, {t, 0, 5}, {x, -10, 10}, Method -> {"PDEDiscretization" -> {"MethodOfLines", "SpatialDiscretization" -> {"FiniteElement", "MeshOptions" -> {"MaxCellMeasure" -> 0.1}}}}]Plot3D[Evaluate[Abs[solution[t, x]]], {x, -10, 10}, {t, 0, 5}, PlotPoints -> 50, AxesLabel -> {x, t}]NormFunction (1)
L = 5;Table[usol[p] = NDSolveValue[{D[u[t, x], t, t] == D[u[t, x], x, x], u[0, x] == Exp[-x ^ 2], Derivative[1, 0][u][0, x] == -Exp[-x ^ 2], u[t, -L] == u[t, L]}, u, {t, 0, 4L}, {x, -L, L}, NormFunction -> (Norm[#, p]&)];
Plot[Evaluate[(usol[p][4L, x]) - (Exp[-x ^ 2] - 2Sqrt[π])], {x, -L, L}, PlotRange -> All], {p, {1, 2, 3, ∞}}]Plot3D[usol[1][t, x], {t, 0, 4L}, {x, -10, 10}, Mesh -> False, PlotPoints -> 20]StartingStepSize (1)
feature[t_ ? NumberQ] := If[t < 1, t Sin[10Pi t], 0];xsol = NDSolveValue[{x''[t] + 10 ^ -4x[t] == feature[t], x[0] == x'[0] == 0}, x, {t, 0, 10000}]Max[Abs[xsol[RandomReal[{0, 10000}, 10000]]]]xsol = NDSolveValue[{x''[t] + 10 ^ -4x[t] == feature[t], x[0] == x'[0] == 0}, x, {t, 0, 10000}, StartingStepSize -> 0.1]{Plot[xsol[t], {t, 0, 2}], Plot[xsol[t], {t, 0, 10000}]}StepMonitor (3)
ListLinePlot[Reap[NDSolveValue[{y'[x] == y[x]Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}, StepMonitor :> Sow[{x, y[x]}]]][[2, 1]]]Module[{c = 0}, NDSolveValue[{y'[x] == y[x]Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}, StepMonitor :> c++];c]ListLinePlot[Differences[Reap[NDSolveValue[{y'[x] == y[x]Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}, StepMonitor :> Sow[x]]][[2, 1]]]]WorkingPrecision (1)
T = 200Pi;
Timing[NDSolveValue[{y''[x] + y[x] == 0, y[0] == 0, y'[0] == 1}, y, {x, 0, T}, MaxSteps -> Infinity][T]]Timing[NDSolveValue[{y''[x] + y[x] == 0, y[0] == 0, y'[0] == 1}, y, {x, 0, T}, WorkingPrecision -> 32, MaxSteps -> 10 ^ 6][T]]具有大的工作精度,有时,"Extrapolation" 方法相当有效:
Timing[NDSolveValue[{y''[x] + y[x] == 0, y[0] == 0, y'[0] == 1}, y, {x, 0, T}, WorkingPrecision -> 32, Method -> "Extrapolation"][T]]应用 (22)
Duffing 方程 (2)
xsol = NDSolveValue[{x''[t] + 0.15x'[t] - x[t] + x[t] ^ 3 == 0.3Cos[t], x[0] == -1, x'[0] == 1}, x, {t, 0, 50}]Plot[xsol[t], {t, 0, 50}]xsol = NDSolveValue[{x''[t] + 0.15x'[t] - x[t] + x[t] ^ 3 == 0.3Cos[t],
x[0] == -1, x'[0] == 1.001}, x, {t, 0, 50}]Plot[xsol[t], {t, 0, 50}]Lotka–Volterra 方程组 (1)
Lotka–Volterra 猎物捕食方程 [更多信息]:
{xsol, ysol} = NDSolveValue[{y'[t] == y[t](x[t] - 1), x'[t] == x[t](2 - y[t]), x[0] == 1, y[0] == 2.7}, {x, y}, {t, 0, 10}]ParametricPlot[{xsol[t], ysol[t]}, {t, 0, 10}]Lorenz 方程组 (1)
Lorenz 方程组 [更多信息]:
{xsol, ysol, zsol} = NDSolveValue[{Derivative[1][x][t] == -3(x[t] - y[t]), Derivative[1][y][t] == -x[t]z[t] + 26.5x[t] - y[t], Derivative[1][z][t] == x[t]y[t] - z[t], x[0] == z[0] == 0, y[0] == 1}, {x, y, z}, {t, 0, 200}, MaxSteps -> ∞]ParametricPlot3D[{xsol[t], ysol[t], zsol[t]}, {t, 0, 200}, PlotPoints -> 1000, ColorFunction -> (Hue[#4]&)]Gavrilov–Shilnikov 模型 (1)
eqns = {
x'[t] == x[t](2 + μ - 10(x[t] ^ 2 + y[t] ^ 2)) + z[t] ^ 2 + y[t] ^ 2 + 2y[t],
y'[t] == -z[t] ^ 3 - (1 + y[t])(z[t] ^ 2 + y[t] ^ 2 + 2y[t]) - 4x[t] + μ y[t],
z'[t] == (1 + y[t])z[t] ^ 2 + x[t] ^ 2 - ϵ};Table[Block[{ϵ = 0.0357, T = 1000},
{xsol, ysol, zsol} = NDSolveValue[{eqns, x[0] == y[0] == z[0] == 1}, {x, y, z}, {t, 0, T}, MaxSteps -> ∞];
ParametricPlot3D[{xsol[t], ysol[t], zsol[t]}, {t, T / 4, T}, BoxRatios -> 1, PlotPoints -> 1000, PlotRange -> All]], {μ, .455, .456, .001}]热方程 (2)
usol = NDSolveValue[{D[u[t, x], t] == D[u[t, x], x, x], u[0, x] == 0, u[t, 0] == Sin[t], u[t, 5] == 0}, u, {t, 0, 10}, {x, 0, 5}]Plot3D[Evaluate[usol[t, x]], {t, 0, 10}, {x, 0, 5}, PlotRange -> All]vars = {Θ[x], {x}};
pars = <|"ThermalConductivity" -> 0.026, "HeatSource" -> 1|>;
Ω = Line[{{0}, {1}}];
eqn = HeatTransferPDEComponent[vars, pars] == 0Tfun = NDSolveValue[{eqn, HeatTemperatureCondition[x == 0, vars, pars, <|"SurfaceTemperature" -> 0|>]}, Θ, x∈Ω];Plot[Tfun[x], {x}∈Ω]有关热方程和适用的边界条件的更多信息,请参见 Heat Transfer 教程和 Heat Transfer PDE models 指南页面.
波方程 (1)
usol = NDSolveValue[{Subscript[∂, t, t]u[t, x] == Subscript[∂, x, x]u[t, x], u[0, x] == E^-x^2, u^(1, 0)[0, x] == 0, u[t, -10] == u[t, 10]}, u, {t, 0, 40}, {x, -10, 10}]DensityPlot[usol[t, x], {t, 0, 40}, {x, -10, 10}, PlotPoints -> 30]Wolfram 非线性波方程 (3)
Wolfram 非线性波方程 [更多信息]:
usol = NDSolveValue[{Subscript[∂, t, t]u[t, x] == Subscript[∂, x, x]u[t, x] + (1 - u[t, x]^2)(1 + 2u[t, x]), u[0, x] == E^-x^2, u^(1, 0)[0, x] == 0, u[t, -10] == u[t, 10]}, u, {t, 0, 10}, {x, -10, 10}]Plot3D[usol[t, x], {t, 0, 10}, {x, -10, 10}]DensityPlot[usol[-t, x], {x, -10, 10}, {t, 0, -10}]使用 Wolfram 的非线性波动方程进行建模 [更多信息],并采用狄里克雷条件.
ics = {u[0, x] == Exp[-x ^ 2], Derivative[1, 0][u][0, x] == 0};bcs = DirichletCondition[u[t, x] == 0, True];sol = NDSolveValue[{(1 + 2 u[t, x]) (-1 + u[t, x]^2) + Inactive[Div][-Inactive[Grad][u[t, x], {x}], {x}] + u^(2, 0)[t, x] == 0, ics, bcs}, u, {t, 0, 10}, {x, -10, 10}]Plot3D[sol[t, x], {x, -10, 10}, {t, 0, 10}, ...]usol = NDSolveValue[{D[u[t, x, y], t, t] == D[u[t, x, y], x, x] + D[u[t, x, y], y, y] / 2 + (1 - u[t, x, y] ^ 2)(1 + 2u[t, x, y]), u[0, x, y] == E^-(x^2 + y^2), u[t, -5, y] == u[t, 5, y], u[t, x, -5] == u[t, x, 5], u^(1, 0, 0)[0, x, y] == 0}, u, {t, 0, 4}, {x, -5, 5}, {y, -5, 5}]Table[Plot3D[usol[t, x, y], {x, -5, 5}, {y, -5, 5}, PlotRange -> All], {t, 1, 4}]n-体问题 (1)
简化的 3-体问题 [更多信息]:
eqn = z''[t] == -(z[t]/(z[t]^2 + ((1/2)(1 + 0.1Sin[2Pi t]))^2)^3 / 2);zsol = NDSolveValue[{eqn, z[0] == 1, z'[0] == 0}, z, {t, 0, 30}]Plot[zsol[t], {t, 0, 30}]eqn = D[z[t, s], t, t] == -(z[t, s]/(z[t, s]^2 + ((1/2)(1 + 0.1Sin[2Pi t]))^2)^3 / 2);zsol = NDSolveValue[{eqn, z[0, s] == s, Derivative[1, 0][z][0, s] == 0}, z, {t, 0, 10}, {s, 1, 2}]Plot3D[zsol[tp, z0], {tp, 0, 10}, {z0, 1, 2}]非线性薛定谔方程 (2)
L = 50;
usol = NDSolveValue[{I D[u[t, x], t] + D[u[t, x], x, x] + 2Abs[u[t, x]] ^ 2u[t, x] == 0.1(1 - Cos[Pi x / L]), u[0, x] == Sech[x]Exp[I x], u[t, -L] == u[t, L]}, u, {t, 0, 10}, {x, -L, L}]Plot3D[Abs[usol[t, x]], {t, 0, 10}, {x, -L, L}, PlotPoints -> 60, MaxRecursion -> 3, Mesh -> None]L = 20;
usol = NDSolveValue[{I D[u[t, x], t] + D[u[t, x], x, x] + 2 Abs[u[t, x]] ^ 2 u[t, x] + 0.1 (1 - Cos[Pi x / L]) u[t, x] == 0, u[0, x] == Sech[x]Exp[I x], u[t, -L] == u[t, L]}, u, {t, 0, 15}, {x, -L, L}]Plot3D[Abs[usol[t, x]], {t, 0, 15}, {x, -L, L}, PlotPoints -> 60, PlotRange -> All, MaxRecursion -> 3]Korteweg-de Vries (KdV) 方程 (2)
KdV = {D[u[x, t], t] + D[u[x, t], {x, 3}] + 6u[x, t]D[u[x, t], x] == 0}sol = NDSolveValue[{KdV, u[x, 0] == 2 - 2 Tanh[(1/4) + x]^2, u[-50, t] == u[50, t]}, u, {x, -50, 50}, {t, 0, 10}]Plot[Evaluate[Table[sol[x, t], {t, 1, 12, 3}]], {x, -5, 50}, PlotRange -> All, Filling -> Axis, Epilog -> {Arrow[{{5, 1.6}, {9, 1.6}}], Arrow[{{17, 1.6}, {21, 1.6}}], Arrow[{{29, 1.6}, {33, 1.6}}], Arrow[{{41, 1.6}, {45, 1.6}}]}]KdV = {D[u[x, t], t] + D[u[x, t], {x, 3}] - 6u[x, t]D[u[x, t], x] == 0}sol = NDSolveValue[{KdV, u[x, 0] == -6 Sech[x]^2, u[-50, t] == u[50, t]}, u, {x, -50, 50}, {t, 0, 10}, Method -> {"MethodOfLines", "SpatialDiscretization" -> {"TensorProductGrid", "MaxPoints" -> 5000, "MinPoints" -> 5000}, Method -> "StiffnessSwitching"}]ListAnimate[Plot[#, {x, -50, 50}, PlotRange -> All]& /@ Table[sol[x, t], {t, 0, 10, 0.1}], SaveDefinitions -> True]Mackey–Glass 方程 (1)
Manipulate[
Module[{xsol, x, t},
xsol = NDSolveValue[{x'[t] == a x[t - τ] / (1 + x[t - τ] ^ 10) - b x[t], x[t /; t ≤ 0] == 1 / 2}, x, {t, 0, 500}];
ParametricPlot[{xsol[t], xsol[t - τ]}, {t, 300, 500}, PlotRange -> {{0, 2}, {0, 2}}]], {{a, .2}, 0, 1}, {{b, .1}, 0, 1},
{{τ, 17}, 1, 20}]声学方程 (1)
vars = {p[t, x], t, {x}};
pars = <|"SoundSpeed" -> 343, "MassDensity" -> 1.2|>;p0 = D[0.125 Erf[(x - 0.5) / 0.15], x];
ics = {p[0, x] == p0, Derivative[1, 0][p][0, x] == -343 * D[p0, x]};eqn = AcousticPDEComponent[vars, pars] == AcousticSoundHardValue[x == 1, vars, pars]pfun = NDSolveValue[{eqn, ics}, p, {t, 0, 0.003}, x∈Line[{{0}, {1}}]];Manipulate[Plot[pfun[t, x], {x, 0, 1}, ...], {{t, 0}, 0, 0.003, 10 ^ -4}, Rule[...]]有关波方程和声学边界条件的更多信息,请参见 Acoustics in the Time Domain 教程和 Acoustics PDE models 指南页面.
物质输运 (1)
给定化学物种在一种材料内的反应速率,模拟一维化学物种穿过不同材料的过程. 右侧和左侧分别受到质量浓度和流入条件的影响:
vars = {c[x], {x}};Ω = Line[{{0}, {1}}];pars = <|"DiffusionCoefficient" -> 0.01, "MassReactionRate" -> If[x > 1 / 2, 1, 0]|>;Subscript[Γ, flux] = MassFluxValue[x == 0, vars, pars, <|"MassFlux" -> 1|>]Subscript[Γ, C] = MassConcentrationCondition[x == 1, vars, pars, <|"MassConcentration" -> 0|>]eqn = MassTransportPDEComponent[vars, pars] == Subscript[Γ, flux]cfun = NDSolveValue[{eqn, Subscript[Γ, C]}, c, x∈Ω];Show[Plot[cfun[x], x∈Ω, Rule[...]], Graphics[...]]有关物质输运方程和边界条件的更多信息,请参见 Mass Transport 教程和 MassTransport PDE models 指南页面.
瞬态 Gray–Scott (1) 方程
eqn = {u[t, x, y] (f + v[t, x, y]^2) + Inactive[Div][(-c1)*Inactive[Grad][u[t, x, y], {x, y}], {x, y}] + u^(1, 0, 0)[t, x, y] == f, v[t, x, y] (f + k - u[t, x, y] v[t, x, y]) + Inactive[Div][(-c2)*Inactive[Grad][v[t, x, y], {x, y}], {x, y}] + v^(1, 0, 0)[t, x, y] == 0} //. {c1 -> 2 * 10 ^ -5, c2 -> c1 / 4, f -> 1 / 25, k -> 3 / 50};ics = {u[0, x, y] == 1 / 2, v[0, x, y] == If[x ^ 2 + y ^ 2 <= 1 / 40, 1, 0]};bcs = DirichletCondition[{u[t, x, y] == 0, v[t, x, y] == 0}, True];{ufun, vfun} = NDSolveValue[{eqn, bcs, ics}, {u, v}, {x, y}∈Disk[], {t, 0, 3000}, Method -> {"PDEDiscretization" -> {"MethodOfLines", "SpatialDiscretization" -> {"FiniteElement", "MeshOptions" -> {"MaxCellMeasure" -> 0.003}}}}];frames = Table[ContourPlot[vfun[t, x, y], {x, -1, 1}, {y, -1, 1}, ...], {t, 100, 2000, 25}];
ListAnimate[frames, Rule[...]]瞬态朗道-金兹堡方程 (1)
eqn = {u^(1, 0)[t, x] + Inactive[Div][-Inactive[Grad][u[t, x], {x}], {x}] + Inactive[Div][-Inactive[Grad][v[t, x], {x}], {x}] - u[t, x] + (u[t, x] - 2 v[t, x]) (u[t, x]^2 + v[t, x]^2) == 0, v^(1, 0)[t, x] + Inactive[Div][Inactive[Grad][u[t, x], {x}], {x}] + Inactive[Div][-Inactive[Grad][v[t, x], {x}], {x}] - v[t, x] + (2 u[t, x] + v[t, x]) (u[t, x]^2 + v[t, x]^2) == 0};exp[x_] := If[x < Log[$MinMachineNumber], 0., Exp[x]]
ics = {u[0, x] == 1 - exp[-x ^ 2], v[0, x] == exp[-(10 - x) ^ 2]};bcs = {u[t, -50] == 1, u[t, 50] == 1, v[t, -50] == 0, v[t, 50] == 0};{ufun, vfun} = NDSolveValue[{eqn, ics, bcs}, {u, v}, {t, 0, 40}, {x, -50, 50}, Method -> {"MethodOfLines", "SpatialDiscretization" -> {"FiniteElement", "MeshOptions" -> {"MaxCellMeasure" -> 0.4}}}];DensityPlot[Evaluate[Sqrt[ufun[t, x] ^ 2 + vfun[t, x] ^ 2]], {x, -50, 50}, {t, 0, 40}, ...]布莱克-舒尔斯方程 (1)
目标是使用布莱克-舒尔斯模型确定欧式普通看涨期权的价格,已知标的资产价格
与执行价格
均为
,无风险利率
为
,资产的波动率
为
,并且该期权的到期期限
为一年.
pars = {tEnd -> 1, k -> 100, σ -> 0.2, r -> 0.05}{lower, upper} = {0, 3k} /. parsBSPDE = -r V[t, s] + r s V^(0, 1)[t, s] + (1/2) s^2 σ^2 V^(0, 2)[t, s] + V^(1, 0)[t, s] == 0 /. parsterminalBC = V[tEnd, s] == Max[0, s - k] /. pars边界条件的选择方式如下:在下界处,当资产价格非常低时,期权价值为零. 上界处的条件来自于看涨-看跌期权平价关系.
bcs = {DirichletCondition[V[t, s] == upper - k E^-r(tEnd - t), s == upper], DirichletCondition[V[t, s] == 0, s == lower]} /. parssol = NDSolveValue[{BSPDE, terminalBC, bcs}, V, {t, 0, tEnd} /. pars, {s, lower, upper}, Method -> {"MethodOfLines", "SpatialDiscretization" -> {"FiniteElement", "MeshOptions" -> {"MaxCellMeasure" -> 0.1}}}]sol[0, 100]Plot3D[sol[t, s], {s, 50, 150}, {t, 0, 1}, ...]定义一个函数,使用 FinancialDerivative 函数来获取期权的价格:
optionPrice[s0_] := FinancialDerivative@@ReplaceAll[{{"European", "Call"}, {"StrikePrice" -> k, "Expiration" -> tEnd}, {"InterestRate" -> r, "Volatility" -> σ, "CurrentPrice" -> s0}}, pars]检查由 FinancialDerivative 给出的通常为真的值:
optionPrice[100]trueVal = optionPrice[100];
numericalVal = sol[0, 100];
PercentForm[(Abs[trueVal - numericalVal]/trueVal)]Plot[optionPrice[s0] - sol[0, s0], {s0, k - 100, k + 100} /. pars, ...]属性和关系 (9)
NDSolveValue 给出解的函数,而 NDSolve 返回的结果是一个规则:
ysol = NDSolveValue[{y'[x] == y[x]Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}]sol = NDSolve[{y'[x] == y[x]Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}]ysol === First[y /. sol]{Plot[ysol[x], {x, 0, 30}], Plot[y[x] /. sol, {x, 0, 30}]}NDSolveValue 可直接返回在指定点的解的函数:
NDSolveValue[{y'[x] == y[x]Cos[x + y[x]], y[0] == 1}, y[1.], {x, 0, 30}]来看 NDSolve:
y[1.] /. First@NDSolve[{y'[x] == y[x]Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}]NDSolveValue 可直接返回解在几个点的值:
NDSolveValue[{y'[x] == y[x]Cos[x + y[x]], y[0] == 1}, Table[y[i], {i, 10, 30, 10}], {x, 0, 30}]来看 NDSolve:
Table[y[i], {i, 10, 30, 10}] /. First@NDSolve[{y'[x] == y[x]Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}]NDSolveValue 可直接返回涉及解的任意表达式的结果:
f[x_] = NDSolveValue[{y'[x] == y[x]Cos[x + y[x]], y[0] == 1}, y''[x]Cos[x + y[x]], {x, 0, 30}]Plot[f[x], {x, 0, 30}]NDSolveValue[{y'[x] == y[x]Cos[x + y[x]], y[0] == 1}, y''[25]Cos[25 + y[25]], {x, 0, 30}]来看 NDSolve:
sol = First@NDSolve[{y'[x] == y[x]Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}];y''[x]Cos[x + y[x]] /. soly''[25]Cos[25 + y[25]] /. sol如果一个方程有多个解,NDSolveValue 会选取其中一个:
ysol = NDSolveValue[{y'[x] ^ 2 == (1 - y[x] ^ 2)(1 - y[x] ^ 2 / 2), y[0] == 0}, y, {x, 0, 1.5}]Plot[ysol[x], {x, 0, 1.5}]NDSolve 则会同时求出两个解:
sol = NDSolve[{y'[x] ^ 2 == (1 - y[x] ^ 2)(1 - y[x] ^ 2 / 2), y[0] == 0}, y[x], {x, 0, 1.5}];
Plot[Evaluate[y[x] /. sol], {x, 0, 1.5}]iv = Table[{x, NIntegrate[Sin[Exp[s]], {s, 0, x}]}, {x, 0, 3, .25}]具有独立变量的函数,NDSolve 有效地给出不定积分:
ysol = NDSolveValue[{y'[x] == Sin[Exp[x]], y[0] == 0}, y, {x, 0, 3}]Plot[ysol[x], {x, 0, 3}, Epilog -> Map[Point, iv]]ev[t_ ? NumberQ] := NDSolveValue[{y''[x] + y[x] == 0, y[0] == 1, y'[0] == 0}, y, {x, t}][t]Plot[ev[t], {t, 0, 3}]FindRoot[ev[t], {t, 1}]NDSolveValue[{y''[x] + y[x] == 0, y[0] == 1, y'[0] == 0, WhenEvent[y[x] == 0, "StopIntegration"]}, y, {x, ∞}]Subscript[y, 10][yp0_ ? NumberQ] := NDSolveValue[{y''[x] + Sin[y[x]] == 0, y[0] == 1, y'[0] == yp0}, y, {x, 10}][10]Plot[Subscript[y, 10][Subsuperscript[y, 0, ′]], {Subsuperscript[y, 0, ′], -2, 2}]FindRoot[Subscript[y, 10][Subsuperscript[y, 0, ′]] == 0, {Subsuperscript[y, 0, ′], -1.5}]ysol = NDSolveValue[{y''[x] + Sin[y[x]] == 0, y[0] == 1, y[10] == 0}, y, x];Plot[{ysol[x], ysol'[x]}, {x, 0, 10}]SystemModel["DocumentationExamples.Basic.DiffEq"]使用 NDSolve 作为 SystemModel 的一个求解器:
sim = SystemModelSimulate[[image], Method -> "NDSolve"]SystemModelPlot[sim, {"y", "z"}]SystemModel["DocumentationExamples.Simulation.Tanks.TankSystem"]使用 SystemModel 模拟更大型的分层模型:
sim = SystemModelSimulate[[image]]SystemModelPlot[sim, {"tank1.h", "tank2.h", "tank3.h"}, PlotRange -> All]可能存在的问题 (12)
针对许多 NDSolveValue 消息,有特定的消息参考页面. 请到理解出错消息工作流程中了解怎样访问这些页面.
有多个解 (1)
对于有多个解的方程,NDSolveValue 只会返回一个解:
NDSolveValue[{Derivative[1][y][x]^2 - y[x]^2 == 0, y[0]^2 == 4}, y[x], {x, 1}]用 NDSolve 求得所有的解:
NDSolve[{Derivative[1][y][x]^2 - y[x]^2 == 0, y[0]^2 == 4}, y[x], {x, 1}]Plot[Evaluate[y[x] /. %], {x, 0, 1}]数值误差 (3)
ysol = NDSolveValue[{y''[x] + y[x] == 0, y[0] == 1, y'[0] == 0}, y, {x, 0, 100}]Plot[ysol[x] - Cos[x], {x, 0, 100}]ysol = NDSolveValue[{y''[x] == y[x](y[x] ^ 2 - 3 / 2), y[0] == 0, y'[0] == 1}, y, {x, 0, 10}]Plot[ysol[x] - JacobiSN[x, 1 / 2], {x, 0, 10}]ysol = NDSolveValue[{y''[x] == y[x](y[x] ^ 2 - 3 / 2), y[0] == 0, y'[0] == 1}, y, {x, 0, 10}, Method -> {"ExplicitRungeKutta", "DifferenceOrder" -> 8}]Plot[(ysol''[x] - ysol[x](ysol[x] ^ 2 - 3 / 2)), {x, 0, 10}, PlotRange -> All]ysol2 = NDSolveValue[{y''[x] == y[x](y[x] ^ 2 - 3 / 2), y[0] == 0, y'[0] == 1}, y, {x, 0, 10}, Method -> {"ExplicitRungeKutta", "DifferenceOrder" -> 8}, InterpolationOrder -> All]Plot[(ysol2''[x] - ysol2[x](ysol2[x] ^ 2 - 3 / 2)), {x, 0, 10}, PlotRange -> All]微分代数方程 (2)
DAE = {Derivative[1][Subscript[x, 1]][t] == Subscript[x, 3][t], Subscript[x, 2][t](1 - Subscript[x, 2][t]) == 0, Subscript[x, 1][t]Subscript[x, 2][t] + Subscript[x, 3][t](1 - Subscript[x, 2][t]) == t};NDSolveValue[{DAE, Subscript[x, 1]'[0] == 1}, {Subscript[x, 1], Subscript[x, 2], Subscript[x, 3]}, {t, 0, 1}]设
,如果还不能求出方程的解,NDSolveValue 可能会改变给定的初始条件:
NDSolveValue[{DAE, Subscript[x, 1]'[0] == 0}, {Subscript[x, 1], Subscript[x, 2], Subscript[x, 3]}, {t, 0, 1}]NDSolveValue 只能求解指数1的方程,但是
时方程指数为2:
NDSolveValue[{DAE, Subscript[x, 1][0] == 0, Subscript[x, 2][0] == 1, Subscript[x, 3][0] == 0}, {Subscript[x, 1], Subscript[x, 2], Subscript[x, 3]}, {t, 0, 1}]s = 100;
dae = {x'[t] == -(xs[t] - ys[t]), y'[t] == (xs[t] - ys[t]), xs[t] == s x[t], ys[t] == s y[t], x[0] == 1, y[0] == 0, xs[0] == s, ys[0] == 0};NDSolveValue[dae, {x, y}, {t, 0, 1}]把 AccuracyGoal 和 PrecisionGoal 设为较小的值以后,求出了一个解:
NDSolveValue[dae, {x, y}, {t, 0, 1}, AccuracyGoal -> 6, PrecisionGoal -> 6]"StateSpace" 时间积分法可以在设置为缺省容差的情况下求出方程的解:
NDSolveValue[dae, {x, y}, {t, 0, 1}, Method -> {"TimeIntegration" -> "StateSpace"}]偏微分方程 (4)
在 PDE models overview 中可以找到各个领域的大量 PDE 模型以及详细的说明.
L = 25;
wpde = {Subscript[∂, t, t]u[t, x] == Subscript[∂, x, x]u[t, x] + (1 - u[t, x]^2)(1 + 2u[t, x]), u[0, x] == E^-x^2, u^(1, 0)[0, x] == 0, u[t, -L] == u[t, L]};usol1 = NDSolveValue[wpde, u, {t, 0, L}, {x, -L, L}]
usol2 = NDSolveValue[wpde, u, {t, 0, L}, {x, -L, L}, Method -> {"MethodOfLines", "SpatialDiscretization" -> {"TensorProductGrid", "MinPoints" -> 1000}}]DensityPlot[usol2[-t, x], {x, -L, L}, {t, 0, -L}, PlotPoints -> 50]hpde = {D[u[t, x], t] == D[u[t, x], x, x], u[0, x] == UnitStep[1 / 2 - x], u[t, 0] == 1, u[t, 1] == 0};Plot3D[Evaluate[NDSolveValue[hpde, u[t, x], {t, 0, .01}, {x, 0, 1}]], {x, 0, 1}, {t, 0, .01}]Plot3D[Evaluate[NDSolveValue[hpde, u[t, x], {t, 0, .01}, {x, 0, 1}]], {x, 0, 1}, {t, 0, .01}, Method -> {"MethodOfLines", "SpatialDiscretization" -> {"TensorProductGrid", "MaxPoints" -> 100}}]lpde = {D[u[x, y], x, x] + D[u[x, y], y, y] == 0, u[x, 0] == Sin[Pi x], Derivative[0, 1][u][x, 0] == 0, u[0, y] == u[1, y] == 0};usol = NDSolveValue[lpde, u, {x, 0, 1}, {y, 0, 1}]
Plot3D[Evaluate[usol[x, y]], {x, 0, 1}, {y, 0, 1}, PlotRange -> All]边界值问题 (1)
ysol = NDSolveValue[{y''[x] + Sin[y[x]] == 0, y[0] == 0, y[10] == 0}, y, x];Plot[{ysol[x], ysol'[x]}, {x, 0, 10}]Table[
ysol = NDSolveValue[{y''[x] + Sin[y[x]] == 0, y[0] == 0, y[10] == 0}, y, x, Method -> {"Shooting", "StartingInitialConditions" -> {y[0] == 0, y'[0] == i}}];
Plot[{ysol[x], ysol'[x]}, {x, 0, 10}], {i, 1, 2}]巧妙范例 (1)
mesh = ImageMesh[[image]];
if = NDSolveValue[{Laplacian[u[x, y], {x, y}] == 0, DirichletCondition[u[x, y] == 1, x <= 6], DirichletCondition[u[x, y] == 0, x >= 595]}, u, {x, y}∈mesh];
ContourPlot[Evaluate[Sqrt[Total[Grad[if[x, y], {x, y}] ^ 2]]], {x, y}∈mesh, Contours -> 2, PlotPoints -> 30]相关指南
-
▪
- 区域上的求解器 ▪
- 偏微分方程 ▪
- 微分方程 ▪
- 微分算子 ▪
- 符号向量、矩阵和数组
文本
Wolfram Research (2012),NDSolveValue,Wolfram 语言函数,https://reference.wolfram.com/language/ref/NDSolveValue.html (更新于 2019 年).
CMS
Wolfram 语言. 2012. "NDSolveValue." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2019. https://reference.wolfram.com/language/ref/NDSolveValue.html.
APA
Wolfram 语言. (2012). NDSolveValue. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/NDSolveValue.html 年
BibTeX
@misc{reference.wolfram_2026_ndsolvevalue, author="Wolfram Research", title="{NDSolveValue}", year="2019", howpublished="\url{https://reference.wolfram.com/language/ref/NDSolveValue.html}", note=[Accessed: 04-July-2026]}
BibLaTeX
@online{reference.wolfram_2026_ndsolvevalue, organization={Wolfram Research}, title={NDSolveValue}, year={2019}, url={https://reference.wolfram.com/language/ref/NDSolveValue.html}, note=[Accessed: 04-July-2026]}