这一节我们主要用matlab自带的偏微分方程的工具箱函数求解
该命令用以求解以下的 P D E PDE PDE方程式:
c ( x , t , u , ∂ u ∂ x ) ∂ u ∂ t = x − m ∂ ( x m f ( x , t , u , ∂ u ∂ x ) ) ∂ x + s ( x , t , u , ∂ u ∂ x ) c(x,t,u,\frac{\partial u }{\partial x})\frac{\partial u}{\partial t } = x^{-m}\frac{\partial (x^mf(x,t,u,\frac{\partial u}{\partial x}))}{\partial x }+s(x,t,u,\frac{\partial u }{\partial x}) c(x,t,u,∂x∂u)∂t∂u=x−m∂x∂(xmf(x,t,u,∂x∂u))+s(x,t,u,∂x∂u)
其中: t ∈ [ t 0 , t f ] , x ∈ [ a , b ] t \in [t_0,t_f],x \in [a,b] t∈[t0,tf],x∈[a,b]。偏微分方程的初解:
u ( x , t 0 ) = v 0 ( x ) u(x,t_0) = v_0(x) u(x,t0)=v0(x)
边界条件为:
p ( x , t , u ) + q ( x , t ) f ( x , t , u , ∂ u ∂ x ) = 0 p(x,t,u) +q(x,t)f(x,t,u,\frac{\partial u}{\partial x}) = 0 p(x,t,u)+q(x,t)f(x,t,u,∂x∂u)=0
下面介绍求解此类方程的函数用法:
s o l = p d e p e ( m , p d e p e , i c f u n , b c f u n , x m e s h , t s p a n , o p t i o n s ) ; sol = pdepe(m,pdepe,icfun,bcfun,xmesh,tspan,options); sol=pdepe(m,pdepe,icfun,bcfun,xmesh,tspan,options);
m : m: m:对称参数。
x m e s h : xmesh: xmesh:位置向量, x m e s h = [ x 0 , x 1 , . . . x N ] , x 0 = a , x N = b xmesh = [x_0,x_1,...x_N],x_0 = a,x_N = b xmesh=[x0,x1,...xN],x0=a,xN=b。
t s p a n : tspan: tspan:时间变量 t t t的向量, t s p a n = [ t 0 , t 1 , . . . t M ] , t 0 = t 0 , t M = t f tspan = [t_0,t_1,...t_M],t_0 = t_0,t_M = t_f tspan=[t0,t1,...tM],t0=t0,tM=tf。
p d e f u n : pdefun: pdefun:用户提供的 p d e pde pde函数文件。函数格式如下:
[ c , f , s ] = p d e f u n ( x , t , u , d u d x ) ; [c,f,s] = pdefun(x,t,u,dudx); [c,f,s]=pdefun(x,t,u,dudx);
也就是说我们要自己设置相应的输出 c , f , s c,f,s c,f,s,且它们都是行向量。
i c f u n : icfun: icfun:求解 u u u的起始值,格式为 u = i c f u n ( x ) u = icfun(x) u=icfun(x)。且 u u u是行向量。
b c f u n : bcfun: bcfun:提供边界条件函数,格式:
[ p l , q l , p r , q r ] = b c f u n ( x l , u l , x r , u r , t ) ; [pl,ql,pr,qr] = bcfun(xl,ul,xr,ur,t); [pl,ql,pr,qr]=bcfun(xl,ul,xr,ur,t);
p l , q l : pl,ql: pl,ql:左边界 p p p和 q q q的行向量。 p r , q r : pr,qr: pr,qr:右边界 p p p和 q q q的行向量。
o p t i o n s : options: options:求解器相关解法参数,见 o d e s e t odeset odeset。
s o l : sol: sol:多维向量输出, s o l ( : , : , i ) sol(:,:,i) sol(:,:,i)为 u i u_i ui的输出,而 u i ( j , k ) = s o l ( j , k , i ) u_i(j,k) = sol(j,k,i) ui(j,k)=sol(j,k,i)表示在 t = t s p a n ( j ) , x = x m e s h ( k ) t = tspan(j),x = xmesh(k) t=tspan(j),x=xmesh(k)时候 的 u i u_i ui的值。
要获得特定位置和时间的解用以下命令:
[ u o u t , d u o u t d x ] = p d e v a l ( m , x m e s h , u i , x o u t ) ; [uout,duoutdx] = pdeval(m,xmesh,ui,xout); [uout,duoutdx]=pdeval(m,xmesh,ui,xout);
x m e s h : [ x 0 , x 1 , . . . x N ] xmesh:[x_0,x_1,...x_N] xmesh:[x0,x1,...xN]
u i : s o l ( j , : , i ) , ui:sol(j,:,i), ui:sol(j,:,i),第 i i i个输出 u i u_i ui在时间 t j t_j tj处的解。
u o u t : uout: uout:在指定 t f t_f tf下对应指定位置 x o u t xout xout的值。
d u o u t d x : duoutdx: duoutdx:相对应的 d u d x \frac{du}{dx} dxdu。
1.求解以下偏微分方程(解析解为 u ( x , t ) = e − t s i n ( π x ) u(x,t) = e^{-t}sin(\pi x) u(x,t)=e−tsin(πx)):
π 2 ∂ u ∂ t = ∂ 2 u ∂ x 2 \pi^2\frac{\partial u}{\partial t} = \frac{\partial^2u}{\partial x^2} π2∂t∂u=∂x2∂2u
其中 x ∈ [ 0 , 1 ] x \in[0,1] x∈[0,1],满足以下条件:
u ( x , 0 ) = s i n ( π x ) u ( 0 , t ) = 0 π e − t + ∂ u ( 1 , t ) ∂ x = 0 u(x,0) = sin(\pi x)\\ u(0,t) = 0 \\ \pi e^{-t} + \frac{\partial u(1,t)}{\partial x} = 0 u(x,0)=sin(πx)u(0,t)=0πe−t+∂x∂u(1,t)=0
s o l v e : solve: solve:
改写以上偏微分方程到标准形式:
π 2 ∂ u ∂ t = x 0 ∂ ∂ x ( x 0 ∂ u ∂ x ) + 0 \pi^2 \frac{\partial u}{\partial t} = x^0\frac{\partial}{\partial x}(x^0\frac{\partial u}{\partial x}) +0 π2∂t∂u=x0∂x∂(x0∂x∂u)+0
具体的实现代码如下:
function first
%计算从t:0~3的值
x = linspace(0,1,20);
t = linspace(0,3,60);
subplot(121);
sol = pdepe(0,@firstPdefun,@firstIcfun,@firstBcfun,x,t);
u = surf(x,t,sol(:,:,1));
title('微分方程数值解');
xlabel('x');
ylabel('t');
zlabel('u')
subplot(122);
[X,T] = meshgrid(x,t);
U = exp(-T).*sin(pi*X);
surf(X,T,U);
title('微分方程解析解');
end
%方程段
function [c,f,s] = firstPdefun(x,t,u,dudx)
c = pi^2;
f = dudx;
s = 0;
end
%起始值条件段
function u = firstIcfun(x)
u = sin(pi*x);
end
%边界条件段
function [pl,ql,pr,qr] = firstBcfun(xl,ul,xr,ur,t);
pl = ul;
ql = 0;
pr = pi*exp(-t);
qr = 1;
end
对比一下发现几乎和解析解一摸一样!
2.求解以下偏微分方程的数值解:
∂ u 1 ∂ t = 0.024 ∂ 2 u 1 ∂ x 2 − F ( u 1 − u 2 ) ∂ u 2 ∂ t = 0.170 ∂ 2 u 2 ∂ x 2 + F ( u 1 − u 2 ) F ( u 1 − u 2 ) = e 5.73 ( u 1 − u 2 ) − e − 11.46 ( u 1 − u 2 ) \frac{\partial u_1}{\partial t} = 0.024\frac{\partial^2u_1}{\partial x^2} - F(u_1-u_2)\\ \frac{\partial u_2}{\partial t} = 0.170\frac{\partial^2u_2}{\partial x^2} + F(u_1-u_2)\\ F(u_1-u_2) = e^{5.73(u_1-u_2)} - e^{-11.46(u_1-u_2)} ∂t∂u1=0.024∂x2∂2u1−F(u1−u2)∂t∂u2=0.170∂x2∂2u2+F(u1−u2)F(u1−u2)=e5.73(u1−u2)−e−11.46(u1−u2)
初值条件:
u 1 ( x , 0 ) = 1 u 2 ( x , 0 ) = 0 u_1(x,0) = 1\\ u_2(x,0) = 0 u1(x,0)=1u2(x,0)=0
边值条件:
∂ u 1 ( 0 , t ) ∂ x = 0 u 2 ( 0 , t ) = 0 u 1 ( 1 , t ) = 1 ∂ u 2 ( 1 , t ) ∂ x = 0 \frac{\partial u_1(0,t)}{\partial x} = 0\\ u_2(0,t) = 0\\ u_1(1,t) = 1\\ \frac{\partial u_2(1,t)}{\partial x} = 0\\ ∂x∂u1(0,t)=0u2(0,t)=0u1(1,t)=1∂x∂u2(1,t)=0
S o l v e : Solve: Solve:
化简上面的偏微分方程为标准形式:
( 1 1 ) . ∗ ∂ ∂ t ( u 1 u 2 ) = ∂ ∂ x ( 0.024 ∂ u 1 ∂ x 0.170 ∂ u 2 ∂ x ) + ( − F ( u 1 − u 2 ) F ( u 1 − u 2 ) ) \begin{pmatrix} 1\\1 \end{pmatrix}_.*\frac{\partial}{\partial t}\begin{pmatrix} u_1\\u_2 \end{pmatrix} = \frac{\partial}{\partial x}\begin{pmatrix} 0.024\frac{\partial u_1}{\partial x}\\0.170\frac{\partial u_2}{\partial x} \end{pmatrix} +\begin{pmatrix} -F(u_1-u_2)\\F(u_1-u_2) \end{pmatrix} (11).∗∂t∂(u1u2)=∂x∂(0.024∂x∂u10.170∂x∂u2)+(−F(u1−u2)F(u1−u2))
化简左边界条件也有:
( 0 u 2 ) + ( 1 0 ) . ∗ ( 0.024 ∂ u 1 ∂ x 0.170 ∂ u 2 ∂ x ) = ( 0 0 ) \begin{pmatrix} 0\\u_2 \end{pmatrix}+\begin{pmatrix} 1\\0 \end{pmatrix}_.* \begin{pmatrix} 0.024\frac{\partial u_1}{\partial x}\\0.170\frac{\partial u_2}{\partial x} \end{pmatrix}= \begin{pmatrix} 0\\0 \end{pmatrix} (0u2)+(10).∗(0.024∂x∂u10.170∂x∂u2)=(00)
化简右边界条件有:
( u 1 − 1 0 ) + ( 0 1 ) . ∗ ( 0.024 ∂ u 1 ∂ x 0.170 ∂ u 2 ∂ x ) = ( 0 0 ) \begin{pmatrix} u_1-1\\0 \end{pmatrix}+\begin{pmatrix} 0\\1 \end{pmatrix}_.* \begin{pmatrix} 0.024\frac{\partial u_1}{\partial x}\\0.170\frac{\partial u_2}{\partial x} \end{pmatrix}= \begin{pmatrix} 0\\0 \end{pmatrix} (u1−10)+(01).∗(0.024∂x∂u10.170∂x∂u2)=(00)
最后附上整个代码:
function second
xmesh = linspace(0,1,20);
tspan = linspace(0,3,60);
sol = pdepe(0,@secondPdefun,@secondPdein,@secondPdebc,xmesh,tspan);
subplot(121);
surf(xmesh,tspan,sol(:,:,1));
xlabel('x');
ylabel('t');
zlabel('u(1)');
title('u(1)-x-t图像');
subplot(122);
surf(xmesh,tspan,sol(:,:,2));
xlabel('x');
ylabel('t');
zlabel('u(2)');
title('u(2)-x-t图像');
end
function [c,f,s] = secondPdefun(x,t,u,dudx)
c = [1 1]';
f = [0.024*dudx(1) 0.170*dudx(2)]';
y = u(1) - u(2);
stemp = exp(5.73*y) - exp(-11.46*y);
s = [-stemp stemp]';
end
function up = secondPdein(x,t,u,dudx)
up = [1 0]';
end
function [pl ql pr qr] = secondPdebc(xl,ul,xr,ur,t)
pl = [0 ul(2)]';
ql = [1 0]';
pr = [ur(1)-1 0]';
qr = [0 1]';
end
最后的结果是: