此为 Mathematica 4 文档,内容基于更早版本的 Wolfram 语言
查看最新文档(版本11.1)

NDSolve

Usage

NDSolve[eqns, y,  x, xmin, xmax ] 用来求解函数y的常微分方程 eqns的数值解,自变量 x 的范围为 xminxmax.
NDSolve[eqns, y,  x, xmin, xmax ,  t, tmin, tmax ] 用来求解偏微分方程eqns的数值解.
NDSolve[eqns,   ,  , ...  ,  x, xmin, xmax ] 用来求解函数  的数值解.


Notes

• NDSolve根据InterpolatingFunction对象得到函数的解.
NDSolve[eqns, y[x],  x, xmin, xmax ] 给出 y[x] 的解而不是函数 y 自身.
• Differential equations 必须通过使用D的导数比如 y'[x]给出, 而不通过使用 Dt的全导数给出.
NDSolve用来求解一大类常微分方程,以及一些偏微分方程.
• 在常微分方程中函数  必须仅依赖与单变量 x. 在偏微分方程中它们可能依赖于多个变量.
• 微分方程必须包括足够的初始条件或边值条件以完全确定  的解.
• 初始条件和边值条件通常以 y[ ] Equal  , y'[ ] Equal  ,等形式给出,但也可能由更多复杂的方程构成.
• 出现在初始条件或边值条件的点 不必在解所考察的范围xminxmax内.
NDSolve中的微分方程可以包含复数.
• 可以给出下列可选项:
"\!\(\*StyleBox[\"\\\"AccuracyGoal\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"Automatic\\\"\", \"MR\"]\) ""解的数字绝对准确度"
"\!\(\*StyleBox[\"\\\"Compiled\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"True\\\"\", \"MR\"]\) "是否编译原来的方程
"\!\(\*StyleBox[\"\\\"InterpolationPrecision\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"Automatic\\\"\", \"MR\"]\) "所返回的插值数据的精度
"\!\(\*StyleBox[\"\\\"MaxSteps\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"Automatic\\\"\", \"MR\"]\) "要采用的最大步数
"\!\(\*StyleBox[\"\\\"MaxStepSize\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"Infinity\\\"\", \"MR\"]\) "每一步的最大步长
"\!\(\*StyleBox[\"\\\"PrecisionGoal\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"Automatic\\\"\", \"MR\"]\) "解的数字精确度
"\!\(\*StyleBox[\"\\\"StartingStepSize\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"Automatic\\\"\", \"MR\"]\) "所使用的初始步长
"\!\(\*StyleBox[\"\\\"WorkingPrecision\\\"\", \"MR\"]\) ""\!\(\*StyleBox[\"\\\"$MachinePrecision\\\"\", \"MR\"]\) "内部计算中所使用的小数数位
MaxSteps的缺省设置是,对常微分方程为1000,对偏微分方程为2000.
• 当达到所指定的AccuracyGoalPrecisionGoal时,NDSolve停止.
AccuracyGoalPrecisionGoalAutomatic自动设置等于WorkingPrecision减去个10个数位.
AccuracyGoal有效地指定解所允许地绝对误差,而PrecisionGoal指定相对误差.
• 如果在解的值接近0时要准确地得到该解,AccuracyGoal应被设置得较大,或是Infinity.
InterpolationPrecision设置指定在由NDSolve生成的InterpolatingFunction对象的内部所使用的数字精确度.
InterpolationPrecisionAutomatic设置使用WorkingPrecision的当前设置.
• 参见 Mathematica 全书: 3.9.1节 和 3.9.7节.
• 实现注释: 参见 A.9.4节.
• 同时参见: DSolve, NIntegrate.
Further Examples

Ordinary differential equations: Basic usage

This command finds a numerical approximation to a function that is equal to its first derivative at each point x between  and  , and that has the value  when x is  . NDSolve returns a rule to replace y by an InterpolatingFunction object.

In[1]:=  

Out[1]=

Mathematica expresses the closed-form solution of this interesting differential equation as a pure function.

In[2]:=  

Out[2]=

We rewrite it as an ordinary algebraic expression. The solution has a singularity at  .

In[3]:=  

Out[3]=

Now we solve the same equation approximately, using NDSolve instead of DSolve. As we can see from the closed-form solution, the warning is appropriate.

In[4]:=  

Out[4]=

We can use an InterpolatingFunction object like any other function that evaluates to a number. Here are three ways to check the boundary condition.

In[5]:=  

Out[5]=

In[6]:=  

Out[6]=

In[7]:=  

Out[7]=

The solution was not found all the way to  , but the InterpolatingFunction object still allows you to evaluate with arguments outside its range. You can see that the approximate value is quite large, though smaller than the correct value of infinity!

In[8]:=  

Out[8]=

It is also easy to make plots of solutions. Here is how to see a graph of an approximation to the function which is the reciprocal of its derivative. The Evaluate command saves time by substituting the InterpolatingFunction once, instead of for each number used to generate the plot. Not too surprisingly, this plot looks very much like the square root function. What do you think would happen if you tried to solve in the other direction?

Evaluate the cell to see the graphic.

In[9]:=  

Mathematica can handle higher order equations. Here is a plot of the interesting solution of a third-order equation. Note that you have to specify enough initial conditions to uniquely determine the solution.

Evaluate the cell to see the graphic.

In[10]:=  

Solving systems of equations works similarly. For systems of two equations a so-called phase plot is often a good way to visualize the solution. Here is a phase plot that describes the motion of a weakly damped pendulum.

Evaluate the cell to see the graphic.

In[11]:=  

Ordinary differential equations: Options

This way of solving the differential equation for the cosine function doesn't work over such a long interval.

In[12]:=  

Out[12]=

You need to increase the setting of the MaxSteps option. As expected, at the end of the interval the solution is close to  .

In[13]:=  

Out[13]=

The Method option allows you to specify which method NDSolve uses to approximate the solution. The default method, Automatic, automatically switches between Gear and Adams, depending on stiffness. Another possibility for equations which are not stiff is RungeKutta. For some problems, the Runge-Kutta method can find the solution using fewer steps.

For the Rössler equations, the Runge-Kutta method needs about half as many steps as the default method.

Evaluate the cell to see the graphic.

In[14]:=  

One way to get a very precise solution of an ODE is to give a sufficiently high value for the WorkingPrecision option. Note that AccuracyGoal and PrecisionGoal default to 10 less than the value of WorkingPrecision when it is greater than $MachinePrecision.

You need to be careful not to use too high a value for WorkingPrecision, because as working precision increases, not only does the time taken for each arithmetic operation and function evaluation increase, but the number of steps typically increases exponentially also.

These commands compare a known exact solution with solutions computed with different values of WorkingPrecision.

In[15]:=  

In[16]:=  

In[17]:=  

The values of x at which the steps are taken is kept in the third part of the InterpolatingFunction object. This is why the Length command above gives the number of steps.

In[18]:=  

Out[18]//TableForm=

Ordinary differential equations: Boundary value problems

Simple linear boundary value problems can be solved by constructing the boundary value equations appropriately. If the order of the equation is  you need to know the values of some combination of the function or its derivatives at  points.

This normalized equation describes the effect of a wave incident on the right edge of an optical medium (where x is  ).

Evaluate the cell to see the graphic.

In[19]:=  

Here is a third-order equation where the function values and combinations of the derivatives are known at three points.

Evaluate the cell to see the graphic.

In[20]:=  

Not all linear equations with boundary values can be solved by the method that is implemented. Nonlinear equations cannot be solved, either.

Evaluating this generates a lot of messages and no result.

In[21]:=  

Partial differential equations: Basic usage

For the first three examples, consider the one-dimensional heat equation. With fixed boundary conditions this is a model for diffusion of heat in an insulated rod with the temperatures at the endpoints held fixed.

This command solves the heat equation with the left end ( ) held at fixed temperature  , the right end ( ) held at fixed temperature  , and an initial heat profile given by the quadratic in x.

In[22]:=  

Out[22]=

Just as for ODE's, the solution is returned as a rule to replace the dependent variable with an InterpolatingFunction object. The only noticeable difference is that the InterpolatingFunction object is two-dimensional. That is to say, it takes two arguments to evaluate, with the arguments in the same order as the variables in the NDSolve command.

The boundary conditions can be a linear combination of Dirichlet and Neumann type conditions. For example, this command solves a model for the diffusion of heat in a one-dimensional insulated rod with the left end held at a constant temperature and the right end radiating into free space. The Neumann boundary condition was entered using the Derivative operator. An equivalent way to give the condition is ((u[x,t] + D[u[x,t],x]) /. x->1) Equal 0.

Evaluate the cell to see the graphic.

In[23]:=  

An even more interesting case is when the boundary conditions are time-dependent. For example, entering these command produces a plot of a solution with the temperature at the left edge varying sinusoidally.

Evaluate the cell to see the graphic.

In[24]:=  

It is also possible to compute the solution of some systems of PDE's. These commands solve a system of mixed parabolic-hyperbolic type and produce separate contour plots for each of the dependent variables.

Evaluate the cell to see the graphics.

In[25]:=  

In[26]:=  

In[27]:=  

Periodic boundary conditions are frequently convenient for numerical solutions. You can tell NDSolve to solve with periodic boundary conditions by specifying that the values of the solution are to be equal at the left and right edges of the domain in one independent variable for all values of the other independent variable.

For example, an interesting solution with periodic solutions is the nonlinear Schrödinger equation. These commands set up a periodic initial condition (it happens to be a soliton), and computes the solution, and then produces  plots showing the modulus and real and imaginary parts of the solution.

Evaluate the cell to see the graphics.

In[28]:=  

Partial differential equations: Limitations

NDSolve uses a method for solving PDE's that is called the numerical method of lines. It discretizes in one variable to make a system of ODE's. This system is then solved using the ODE methods built into NDSolve.

For the method to work, an initial function must be specified for one variable and boundary values may be specified for the other variable. The initial function is used to find the initial conditions for the system of the ODE's. Boundary and initial values may be specified on at most three sides of a rectangle.

The method has the advantage that it can solve a reasonably large class of equations. However, there are types of equations which it cannot solve.

Because elliptic problems are ill-posed unless boundary values are specified on all sides of a region, this method cannot find solutions of elliptic problems. A classic example is Laplace's equation. Entering this command indicates what will happen when you try to do something of this sort. Typically ill-posedness will appear as numerical instability, and looking at the scale on the plot produced indicates that you should heed the message seriously!

Evaluate the cell to see the graphic.

In[29]:=  

Another class of problems that the method cannot currently handle are those that form singularities in the solution. The discretization is done with finite difference methods, so fronts may be incorrectly resolved or completely lost. For example, Burgers' equation is a model for some of the features of gas dynamics, including shock formation. Entering these commands will produce a plot that show the oscillations typical of the interaction between finite difference methods and fronts.

Evaluate the cell to see the graphic.

In[30]:=  

Partial differential equations: Options

Many of the options that control the ODE solver also apply to the PDE solver, though often in a different way. The solutions for PDE's are computed in two stages. First the equation is discretized, and then the resulting system of ODE's is solved using NDSolve's built-in method. If you want different option values for the two stages, you can specify the option value as a list. (The order of the independent variables in the command determines to what variable the options apply.)

The following options to NDSolve can be used in such a list: AccuracyGoal, PrecisionGoal, DifferenceOrder, MaxSteps, MaxStepSize, StartingStepSize.

In the discretization stage, the default used is fourth-order finite differences. In some cases fourth-order is not optimal; you can control this with the DifferenceOrder option. When the equation has high spatial differential order (eg. Airy's equation), it is better to increase the order.

Entering these commands computes a solution to Airy's equation with periodic boundary conditions and produces  plots that you can view as an animation. Since the independent variable  is the one with an initial function, it is the one for which we specify the difference order of  . (The variable x appears after t in the list of arguments of NDSolve.)

Evaluate the cell to see the graphic.

In[31]:=  

In[32]:=  

We clean up by clearing the definitions that were made.

In[33]:=