
http://www.ma-xy.com
第一章 偏微分方程 1.5 MATLAB 解偏微分方程
6 %% 旧 方 法
7 clc , c l e a r
8 c = 1; a = 0 ; f = 0; d = 1 ;
9 g = @squareg ;%边 界 区 域
10 b = @squareb3 ;
11 [ p , e , t ] = i nitmesh (g ) ;%初 始 化 网 格
12 x = p( 1 , : ) ’ ;
13 y = p( 2 , : ) ’ ;
14 u0 = atan ( co s ( p i /2*x ) ) ;%初 始 条 件
15 ut0 = 3* s i n ( pi *x ) . * exp ( s i n ( pi /2*y) ) ;%初 始 条 件
16 n = 31 ;
17 t l i s t = l i n s p a c e ( 0 , 5 , n) ;%离 散 时 间
18 uu = hy p e rboli c ( u0 , ut0 , t l i s t , b , p , e , t , c , a , f , d) ;%求 解 双 曲 线 型PDE方 程 , uu 是 一
个 31 列 的 矩 阵 , 每 个 时 间 t 为 一 列
19 del t a = −1: 0 . 1 :1;
20 [ uxy , tn , a2 , a3 ] = t r i 2 g r i d (p , t , uu ( : , 1) , delt a , delta ) ;%将 三 角 形 网 格 转 化 为 矩 形 网
格
21 gp = [ tn ; a2 ; a3 ] ;
22 umax = max(max(uu) ) ;
23 umin = min(min(uu) ) ;
24 newplot
25 M = moviein (n ) ;
26 f o r i = 1 : n
27 pdeplot ( p , e , t , ’ xydata ’ , uu ( : , i ) , ’ zdata ’ , uu ( : , i ) , . . .
28 ’mesh ’ , ’ o f f ’ , ’ xygrid ’ , ’ on ’ , ’ gridparam ’ , gp , . . .
29 ’ colorbar ’ , ’ o f f ’ , ’ z s t y l e ’ , ’ continuous ’ ) ;
30 a x is ([ −1 , 1 , −1, 1 , umin , umax] ) ;
31 c axi s ( [ umin , umax ] ) ;
32 M( : , i ) = getframe ;
33 end
34 movie (M, 10) ;
35 %% 新 方 法
36 %−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
37 %方 程 的 一 般 形 式 为 :m*Utt + d*Ut − div ( c*giadU ) + a*U = f
38 %我 们 要 解 决 的 二 维 波 动 方 程 为 : Utt − div ( c *giadU ) = 0;
39 %−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
40 clc , c l e a r
41 % 1 、 确 定 方 程 ( 模 型 系 数 , 边 界 形 状 )
42 m = 1 ; d = 0 ; c = 1 ; a = 0 ; f = 0 ;
43 g = @squareg ;
44 numberOfPDE = 1;
45 model = createp de (numberOfPDE) ;
46 geometryFromEdges ( model , g ) ;
47 % pdegplot (model , ’ edgeLabels ’ , ’ on ’ ) ;
48 % ylim ([ −1.1 1 . 1 ] ) ;
49 % axi s equal , t i t l e ’Geometry With Edge Labels Displayed ’ , x l a b el x , y la b e l y
50 %2 、 确 定 边 界 条 件
51 s p e c i f y C o e f f i c i e n t s ( model , ’m’ ,m, ’d ’ , d , ’ c ’ , c , ’ a ’ , a , ’ f ’ , f ) ;
52 applyBoundaryCondition ( model , ’Edge ’ , [ 2 , 4 ] , ’u ’ , 0) ;%d i r i c h l e t 边 界 条 件
53 applyBoundaryCondition ( model , ’Edge ’ , [ 1 3] , ’ g ’ , 0) ;%neumann边 界 条 件
54 %3 、 形 成 最 终 模 型
55 generateMesh (model ) ;%形 成 单 元 e
56 u0 = @( l o c a t i o n ) atan ( cos ( pi /2* lo c a t i o n . x ) ) ;%初 始 条 件
http://www.ma-xy.com 49 http://www.ma-xy.com