Differential Equations
This example shows how to use MATLAB® to formulate and solve several different types of differential equations. MATLAB offers several numerical algorithms to solve a wide variety of differential equations:
Initial value problems
Boundary value problems
延迟微分方程
Partial differential equations
初始值问题
Vanderpoldemo
is a function that defines the van der Pol equation
类型Vanderpoldemo
function dydt = vanderpoldemo(t,y,Mu) %VANDERPOLDEMO Defines the van der Pol equation for ODEDEMO. % Copyright 1984-2014 The MathWorks, Inc. dydt = [y(2); Mu*(1-y(1)^2)*y(2)-y(1)];
该方程式写为两个一阶普通微分方程(ODE)的系统。评估这些方程的参数值 。为了faster integration, you should choose an appropriate solver based on the value of 。
为了
,任何MATLAB ODE求解器都可以有效地求解Van der pol方程。这ode45
solver is one such example. The equation is solved in the domain
有初始条件
and
。
tspan = [0 20]; y0 = [2; 0]; Mu = 1; ode = @(t,y) vanderpoldemo(t,y,Mu); [t,y] = ode45(ode, tspan, y0);%情节解决方案plot(t,y(:,1)) xlabel('t')ylabel('solution y')title('van der pol方程,\ mu = 1')
为了larger magnitudes of
,,,,the problem becomes僵硬的。该标签是针对抵制使用普通技术进行评估的问题的问题。相反,需要特殊的数值方法进行快速集成。这ODE15S
,,,,ODE23S
,,,,ode23t
,,,,andode23tb
功能可以有效地解决严重的问题。
此解决方案的解决方案
你sesODE15S
具有相同的初始条件。您需要大幅度地延长时间到
能够看到解决方案的周期性运动。
tspan = [0, 3000]; y0 = [2; 0]; Mu = 1000; ode = @(t,y) vanderpoldemo(t,y,Mu); [t,y] = ode15s(ode, tspan, y0); plot(t,y(:,1)) title('van der pol方程,\ mu = 1000')轴([0 3000 -3 3])Xlabel('t')ylabel('solution y')
边界价值问题
bvp4c
andbvp5c
solve boundary value problems for ordinary differential equations.
示例函数twoode
具有一个微分方程为两个一阶ODE的系统。微分方程是
类型twoode
function dydx = twoode(x,y) %TWOODE Evaluate the differential equations for TWOBVP. % % See also TWOBC, TWOBVP. % Lawrence F. Shampine and Jacek Kierzenka % Copyright 1984-2014 The MathWorks, Inc. dydx = [ y(2); -abs(y(1)) ];
这functiontwobc
有问题的边界条件:
and
。
类型twobc
函数res = twOBC(YA,YB)%TWOBC评估在TWOBVP边界条件下的残差。%%参见Twoode,TwoBvp。%Lawrence F. Shampine和Jacek Kierzenka%版权1984-2014 The Mathworks,Inc。res = [YA(1);YB(1) + 2];
在打电话之前bvp4c
,,,,you have to provide a guess for the solution you want represented at a mesh. The solver then adapts the mesh as it refines the solution.
这bvpinit
function assembles the initial guess in a form you can pass to the solverbvp4c
。为了a mesh of[[01234这是给予的
和不断的猜测
and
,打电话bvpinit
是:
solinit = bvpinit([0 1 2 3 4],[1; 0]);
With this initial guess, you can solve the problem withbvp4c
。评估返回的解决方案bvp4c
在某些时候使用deval
,然后绘制结果值。
sol = bvp4c(@twoode, @twobc, solinit); xint = linspace(0, 4, 50); yint = deval(sol, xint); plot(xint, yint(1,:)); xlabel('X')ylabel('solution y')holdon
This particular boundary value problem has exactly two solutions. You can obtain the other solution by changing the initial guesses to and 。
solinit = bvpinit([0 1 2 3 4],[-1; 0]); sol = bvp4c(@twoode,@twobc,solinit); xint = linspace(0,4,50); yint = deval(sol,xint); plot(xint,yint(1,:)); legend('Solution 1',,,,'Solution 2')holdoff
Delay Differential Equations
dde23
,,,,ddesd
,,,,andddensd
solve delay differential equations with various delays. The examplesDDEX1
,,,,DDEX2
,,,,DDEX3
,,,,DDEX4
,,,,andDDEX5
form a mini tutorial on using these solvers.
这DDEX1
示例显示了如何求解微分方程的系统
您可以用匿名函数表示这些方程
ddex1fun = @(t,y,z)[z(1,1);z(1,1)+z(2,2);y(2)];
这history of the problem (for )is constant:
您可以表示历史记录为矢量。
ddex1hist =一个(3,1);
A two-element vector represents the delays in the system of equations.
lags = [1 0.2];
Pass the function, delays, solution history, and interval of integration to the solver as inputs. The solver produces a continuous solution over the whole interval of integration that is suitable for plotting.
sol = dde23(ddex1fun,lags,ddex1hist,[0 5]);情节(sol.x,sol.y);标题({'An example of Wille and Baker',,,,'DDE持续延迟'});Xlabel('时间t');ylabel('solution y');legend('y_1',,,,'y_2',,,,'y_3',,,,'Location',,,,'西北');
部分微分方程
pdepe
在一个空间变量和时间中求解部分微分方程。示例pdex1
,,,,pdex2
,,,,pdex3
,,,,pdex4
,,,,andpdex5
form a mini tutorial on usingpdepe
。
This example problem uses the functionspdex1pde
,,,,pdex1ic
,,,,andPDEX1BC
。
pdex1pde
定义微分方程
类型pdex1pde
function [c,f,s] = pdex1pde(x,t,u,DuDx) %PDEX1PDE Evaluate the differential equations components for the PDEX1 problem. % % See also PDEPE, PDEX1. % Lawrence F. Shampine and Jacek Kierzenka % Copyright 1984-2014 The MathWorks, Inc. c = pi^2; f = DuDx; s = 0;
pdex1ic
sets up the initial condition
类型pdex1ic
function u0 = pdex1ic(x) %PDEX1IC Evaluate the initial conditions for the problem coded in PDEX1. % % See also PDEPE, PDEX1. % Lawrence F. Shampine and Jacek Kierzenka % Copyright 1984-2014 The MathWorks, Inc. u0 = sin(pi*x);
PDEX1BC
设置边界条件
类型PDEX1BC
函数[PL,QL,PR,QR] = PDEX1BC(XL,UL,XR,UR,T)%PDEX1BC评估PDEX1中编码的问题的边界条件。%%另请参见PDEPE,PDEX1。%Lawrence F. Shampine和Jacek Kierzenka%版权1984-2014 The Mathworks,Inc。pl = ul;ql = 0;pr = pi * exp(-t);QR = 1;
pdepe
requires the spatial discretizationX
and a vector of timest
(在其中您想要该解决方案的快照)。使用20个节点的网格解决问题,并在五个值中要求解决方案t
。Extract and plot the first component of the solution.
X=linspace(0,1,20); t = [0 0.5 1 1.5 2]; sol = pdepe(0,@pdex1pde,@pdex1ic,@pdex1bc,x,t); u1 = sol(:,:,1); surf(x,t,u1); xlabel('X');ylabel('t');Zlabel('U');