ADI(交替隐式求求解抛物型偏微分方程)程序.

合集下载

二维波动方程的高精度交替方向隐式方法

二维波动方程的高精度交替方向隐式方法

二维波动方程的高精度交替方向隐式方法马月珍;李小纲;葛永斌【摘要】基于二阶微商的四阶紧致差商逼近公式及加权平均思想,提出了数值求解二维波动方程的2种精度分别为O(τ~2+h~4)和O(τ~4+h~4)的交替方向隐式(ADI)格式,以及与其相匹配的第一个时间层的同阶离散格式,并且通过Fourier方法分析了格式的稳定性.该方法在沿每个空间方向上只涉及3个网格基架点,因此可以重复采用TDMA算法,从而大大节省计算时间.数值实验验证了所用方法的精确性和可靠性.【期刊名称】《四川师范大学学报(自然科学版)》【年(卷),期】2010(033)002【总页数】5页(P179-183)【关键词】波动方程;高阶紧致格式;交替方向隐式方法;稳定性【作者】马月珍;李小纲;葛永斌【作者单位】宁夏大学,应用数学与力学研究所,宁夏,银川,750021;宁夏大学,应用数学与力学研究所,宁夏,银川,750021;宁夏大学,应用数学与力学研究所,宁夏,银川,750021【正文语种】中文【中图分类】O241.82有限差分法[1-3]是数值求解偏微分方程的常用方法之一.在航空、气象、海洋、水利等许多流体力学的问题中,常常遇到双曲型偏微分方程,针对传统的差分离散格式,普遍有着精度低且受到很强的稳定性条件限制的缺陷,因此发展其高精度且稳定性好的差分离散方法具有十分重要的意义[4-7].交替方向隐式(AD I)方法是数值求解该类问题的一种非常有效的方法,它将高维问题转化为若干一维问题进行求解,而一维问题又可采用高效的 TDMA算法,从而可以大大提高计算效率,节省存储空间.传统的AD I方法如 Peaceman-Rachford(P-R)格式和Beam-War ming[8]格式都是二阶精度的.另一方面,文献[9]提出了求解二维非定常对流扩散方程的高精度AD I格式,文献[10]提出了求解高维热传导方程的高精度AD I格式.本文依然根据AD I方法的基本思想,提出两种求解二维波动方程的高精度紧致AD I差分格式,为此考虑初边值问题:其中Ω ={(x,y):0≤x,y≤1},Γ为Ω的边界,u(x, y,t)为待求未知量,f(x,y,t)为源项,φ(x,y),ψ(x,y)和 g(x,y,t)均为已知函数且具有充分的光滑性.1 高阶紧致AD I格式用τ表示时间步长,空间取等间距网格,步长用h表示.网格点为(xi,yj,tn),xi=ih,yj=jh,tn = nτ,i,j=0,1,…,N,h=1/N,n≥0.1.1 AD I(2,4)格式考虑(1)式在 n时刻值,对时间导数项采用中心差分,空间导数项采用Kreiss[11]提出的四阶紧致差分公式:则有其中在空间方向以和的算术平均值代替可得对(5)式进行整理且略去高阶项可得为了构造AD I差分格式,采用与文献[9-10]类似的技巧,在(6)式左端加上可得显然,(7)式与(6)式的截断误差同阶,利用可将(7)式写为如下形式引入一个过渡变量则可将 (8)式写为对于过渡变量的边界条件,可以由下式给出(9)式即为求解二维波动方程的高精度紧致 AD I格式,其精度为O(τ2+h4),记为AD I(2,4).1.2 AD I(4,4)格式对时间和空间导数项均采用四阶紧致差分公式,考虑(1)式在 n时刻值,可得对上式进行整理且略去高阶项可得可将(10)式写为如下形式为了构造AD I差分格式,采用与文献[9-10]类似的技巧,在 (11)式左端加上并进行因式分解,可得显然,(11)式与(12)式的截断误差同阶,引入一个过渡变量则 (12)式可写为对于过渡变量的边界条件,可以由下式给出(13)式即为求解二维波动方程的高精度紧致 AD I格式,其精度为O(τ4+h4),记为AD I(4,4).1.3 初始条件的离散因为格式是 3层的.即每一次时间推进都需要知道前两个时间步的值,初始时刻有(2)式精确给出,第一个时间步的值由 (3)式给出,因此,须对 (3)式进行离散,下面推出与(9)和 (13)式相匹配的第一个时间步的离散格式.利用 Taylor展开式将在处展开可得利用(1)~(3)式,且略去高阶项,即可得与(9)式相匹配的第一个时间步的离散格式与(13)式相匹配的第一个时间步的离散格式2 稳定性分析下面,采用 Fourier分析方法对格式进行稳定性分析.引理[12] 实系数二次方程λ2-bλ-c=0的根按模不大于 1的充要条件为|b|≤1-c≤2.2.1 AD I(2,4)格式的稳定性定理 1 格式(9)是无条件稳定的.证明显然,格式(9)可写成用表示采用上述格式进行计算产生的误差,设源项 f无误差存在,则格式的误差项满足格式相应的齐次方程,即记其特征项表示为其中为虚数单位,ηn为第 n个时间层上的波幅,σ1、σ2为波数,令λ=τ/h,则可得误差的传播矩阵为其中矩阵的特征方程为所以有由于Px≥0、Py≥0、Qx≥0、Qy≥0,故|b|≤2,由引理可得格式(9)是无条件稳定的.2.2 AD I(4,4)格式的稳定性定理 2 格式(13)是条件稳定的,其稳定性条件为证明由相同的分析过程可得格式(13)误差的传播矩阵为矩阵的特征方程为由引理得即上式右端显然成立,考察左端可得解之可得|a|λ≤1/3,即格式(13)是条件稳定的,其稳定性条件为|a|λ≤1/3.3 数值验证对于问题(1)~(4),令问题的精确解为计算是用 Fortran77语言进行编程且在 Pentium IV/2.4G PC机上双精度制下进行的.由于2种格式所得线性方程组均为三对角线型,所以对每一步可以采用TDMA 算法.表 1给出了不同网格步长下,问题在 t=0.125时刻,当τ=h时二阶AD I格式[8]、FULL(4,4)格式[7]和τ=h2时AD I(2,4)格式的数值计算结果的最大误差 E和收敛阶 rate=ln(E1/E2)/ln2(E1和E2分别为粗网格及相邻的细网格上的最大误差).结果表明,3种格式均达到了各自的精度,并且AD I(2,4)格式的计算结果要比其它两种格式精确得多.表 2和表 3给出了不同网格步长下,问题在 t =0.25时刻,当τ =h/2时FULL(4,4)格式[7]、AD I(4,4)格式和τ=h2时 HWAL(2,4)[7]格式的数值计算结果的最大误差 E、收敛阶 rate和 CPU时间.结果表明,3种格式均达到了各自的精度,并且AD I(4,4)格式的计算结果要比其它两种格式精确得多,并且计算时间最少.表 4给出了 h=1/64时,问题在 t=3.2时刻,对不同网格比λ(此时τ=λh不同),3种AD I 格式的收敛性的比较.结果表明,当时,AD I(4,4)格式是发散的,即条件稳定的,其它格式仍然是收敛的,即无条件稳定的.这与本文的理论分析结果一致.本文的方法可推广到三维波动方程的数值求解中去,将另文报道.表 1 t=0.125时刻不同空间步长 h的最大误差和收敛阶Table 1 Max imum error and convergencerate att=0.125 for differenthh 二阶ADI格式[8]FULL(4,4)格式[7]ADI(2,4)格式E rate E rate E rate 1/8 2.81e-2 1.29e-34.81e-4 1/16 7.57e-3 1.89 8.20e-5 3.98 3.01e-5 3.97 1/32 1.93e-3 1.975.15e-6 3.99 1.88e-6 4.00 1/64 4.83e-4 1.99 3.22e-7 4.00 1.18e-7 4.001/128 1.22e-4 2.00 1.82e-8 4.14 7.35e-9 4.00表 2 t=0.25时刻不同空间步长 h的最大误差和收敛阶Table 2 Max imum error and convergencerate att=0.25 for differenthh HWAL(2,4)格式[7]FULL(4,4)格式[7]ADI(4,4)格式E rate E rate E rate 1/8 1.54e-2 2.47e-4 2.87e-5 1/162.46e-5 9.29 1.56e-53.98 1.73e-64.05 1/32 1.53e-6 4.00 9.75e-7 4.001.07e-7 4.01 1/64 8.96e-8 4.09 5.99e-8 4.02 6.68e-9 4.00 1/128 5.60e-9 4.002.73e-9 4.45 4.17e-10 4.00表 3 t=0.25时刻不同空间步长 h的 CPU时间Table 3 CPU t ime att=0.25 for differenthλ HWAL(2,4)格式[7]FULL(4,4)格式[7] ADI(4,4)格式1/8 <10e-8 <10e-8 <10e-8 1/16 0.125 0.015 0.015 1/32 1.985 0.125 0.031 1/64 57.50 1.391 0.219 1/128 2830.51 28.39 1.687表 4 当 h=1/64,t=3.2时刻,对不同λ的最大误差Table 4 Max imum erroratt=3.2 andh=1/64 for differentλλ 二阶格式[8] ADI(2,4)格式 ADI(4,4)格式0.4 6.78e-4 4.68e-4 1.93e-8 0.8 2.04e-3 1.84e-3 1.49e+51 1.6 7.03e-3 6.87e-3 3.09e+65 3.2 1.96e-2 1.95e-2 2.80e+39参考文献[1]李胜坤,冯民富,李珊.Benjamin-Bona-Mahony方程的有限差分近似解[J].四川师范大学学报:自然科学版,2003, 26(4):363-365.[2]吕胜关.一类高阶方程组的差分方法[J].郑州大学学报:理学版,1999,32(1):12-19.[3]罗明英,舒国皓,王殿志.RLW方程的有限差分逼近[J].四川师范大学学报:自然科学版,2001,24(2):138-143.[4]Visher J,Wandzura S,White A.Stable,high-order discretization forevolution of the wave equation in 1+1 dimensions[J].J ComputPhys,2004,194:395-408.[5]Wandzura S.Stable,high-order discretization for evolution of the wave equation in 2+1 dimensions[J].J Comput Phys,2004, 199:763-775.[6]葛永斌,吴文权,田振夫.二维波动方程加权平均隐格式及其多重网格方法[J].上海理工大学学报,2002,24(3):205-208.[7]葛永斌,吴文权,田振夫.二维波动方程高精度隐式格式及其多重网格方法 [J].厦门大学学报:自然科学版,2003, 42(6):691-696.[8]孙志忠.偏微分方程数值解法[M].北京:科学出版社,2005.[9]Karaa S,Zhang J.High orderADImethod for solving unsteady convection-diffusion problem[J].J Comput Phys,2004,108:1-9.[10]葛永斌,田振夫,吴文权.高维热传导方程的高精度交替方向隐式方法[J].上海理工大学学报,2007,29(1):55-58.[11]Hirsh R S.Higher order accurate difference solutions of fluid mechanics problem by a compact differencing technique[J].J Comput Phys,1975,19:90-109.[12]陆金甫,关冶.偏微分方程数值解法[M].北京:清华大学出版社,1987.。

一维抛物型偏微分方程初边值问题求解

一维抛物型偏微分方程初边值问题求解

一维抛物型偏微分方程初边值问题求解摘要:一、引言二、一维抛物型偏微分方程1.定义与性质2.初边值问题三、求解方法1.紧差分格式2.追赶法3.有限元算法四、Matlab程序实现1.紧差分格式程序2.追赶法程序五、结论与展望正文:一、引言在数学、物理等领域,偏微分方程是一类重要的方程。

其中,一维抛物型偏微分方程在科学研究和实际应用中具有广泛的意义。

本文将探讨一维抛物型偏微分方程的初边值问题的求解方法,并介绍相应的Matlab程序实现。

二、一维抛物型偏微分方程1.定义与性质一维抛物型偏微分方程是指具有如下形式的方程:u_t = a * u_xx其中,u(x, t) 表示未知函数,t 表示时间,x 表示空间坐标,a 为常数。

2.初边值问题初边值问题是指在给定的初始条件和边界条件下求解偏微分方程的问题。

在一维抛物型偏微分方程中,初边值问题可以表示为:u(x, 0) = u_0(x)u(x, t) = u_t(x, t) 在边界x=0,x=L上三、求解方法1.紧差分格式紧差分格式是一种求解偏微分方程的方法,其精度为O(h^(1/2) * Δt),无条件稳定。

在这种方法中,我们首先需要建立离散的网格系统,然后通过数值积分求解离散化的偏微分方程。

2.追赶法追赶法是一种求解线性方程组的方法,也可以用于求解初边值问题。

在这种方法中,我们首先需要将偏微分方程转化为线性方程组,然后使用追赶法求解线性方程组。

3.有限元算法有限元算法是一种基于变分原理的求解方法,可以将偏微分方程问题转化为求解有限元空间的线性方程组。

这种方法在求解一维抛物型偏微分方程时具有较高的精度和可靠性。

微分方程数值解法课程设计---抛物型方程问题的差分格式[9页].doc

微分方程数值解法课程设计---抛物型方程问题的差分格式[9页].doc

目录一、问题的描述 (1)二、算法设计及流程图 (1)2.1 算法设计 (1)2.2 流程图 (2)三、算法的理论依据及其推导 (2)3.1 截断误差分析 (2)3.2 稳定性分析 (3)四、数值结果及分析 (3)五、总结 (5)六、附件(源代码) (6)抛物型方程问题的差分格式一、问题的描述有限差分方法就是一种数值解法,它的基本思想是先把问题的定义域进行网格剖分,然后在网格点上,按适当的数值微分公式把定解问题中的微商换成差商,从而把原问题离散化为差分格式,进而求出数值解。

此外,还要研究差分格式的解的存在性和唯一性、解的求法、解法的数值稳定性、差分格式的解与原定解问题的真解的误差估计、差分格式的解当网格大小趋于零时是否趋于真解(即收敛性),等等。

偏微分方程边值问题的差分法是物理上的定常问题,其定解问题为各种边值问题, 即要求解在某个区域内满足微分方程,在边界上满足给定的边界条件。

常系数扩散方程的差分解法可归结为选取合理的差分网格,建立差分格式求解。

常系数扩散问题的有限差分格式求常系数扩散问题为正常数其中a ,0,,22>∈∂∂=∂∂t R x xua t u (1.1) 的近似解,其初始条件为R x x g x u ∈=),()0,(二、算法设计及流程图2.1 算法设计运用加权隐式格式求解常系数扩散问题(1.1)02)1(22111112111=⎥⎥⎦⎤⎢⎢⎣⎡+--++-------+-+-h u u u h u u u a u u n j n j n j n j n j n j n jn j θθτ,(1.6) 10≤≤θ,h τ其中分为时间步长和空间步长。

步骤1 输入初始值,确定加权隐式格式的参数;步骤2 定义向量A ,把初边值条件离散,得到0j u ,j=0,1,…,J 的值存入向量A 步骤3 利用加权隐式差分格式由第n 层计算第n+1层,建立相应线性方程组,求解并且存入向量A;步骤4 计算到t=1,输出u2.2 流程图三、算法的理论依据及其推导3.1 截断误差分析常系数扩散问题(1.1)的加权隐式格式如下:02)1(22111112111=⎥⎥⎦⎤⎢⎢⎣⎡+--++-------+-+-h u u u h u u u a u u n j n j n j n j n j n j n jn j θθτ,(1.6) 其中10≤≤θ,,h τ其中分为时间步长和空间步长。

10_抛物型方程的有限差分方法

10_抛物型方程的有限差分方法

10_抛物型方程的有限差分方法抛物型方程是一类常见的偏微分方程,广泛应用于自然科学和工程学的领域中。

有限差分方法是一种常用的数值求解抛物型方程的方法之一、本文将介绍抛物型方程的有限差分方法(II)。

有限差分方法主要基于离散化的思想,将偏微分方程转化为差分方程,进而求解差分方程的数值解。

对于抛物型方程,其一般形式可以表示为:∂u/∂t=Δu+f(x,t)其中,u(x, t)是未知函数,表示空间位置x和时间t上的解,Δu表示Laplace算子作用于u的结果,f(x, t)是已知函数。

有限差分方法的基本思想是将空间和时间域进行离散化,将连续的空间和时间划分为有限个网格点,然后使用差分近似代替偏导数,得到差分方程。

假设空间域被划分为Nx个网格点,时间域被划分为Nt个网格点,对于每个网格点(i,j),可以表示为(x_i,t_j),其中i=0,1,...,Nx,j=0,1,...,Nt。

在有限差分方法中,我们使用中心差分近似来代替偏导数。

对于时间导数,可以使用向前差分或向后差分,这里我们使用向前差分,即:∂u/∂t≈(u_i,j+1-u_i,j)/Δt对于空间导数,可以使用中心差分,即:∂^2u/∂x^2≈(u_i-1,j-2u_i,j+u_i+1,j)/Δx^2将上述差分近似代入抛物型方程中,可以得到差分方程的离散形式:(u_i,j+1-u_i,j)/Δt=(u_i-1,j-2u_i,j+u_i+1,j)/Δx^2+f_i,j其中,f_i,j=f(x_i,t_j)。

重排上式,可以得到递推关系式:u_i,j+1=αu_i-1,j+(1-2α)u_i,j+αu_i+1,j+Δt*f_i,j其中,α=Δt/Δx^2通过设置初始条件和边界条件,可以利用以上递推关系式得到抛物型方程的数值解。

总结来说,抛物型方程的有限差分方法(II)是一种常用的数值求解抛物型方程的方法。

它基于离散化的思想,将偏微分方程转化为差分方程,然后利用中心差分近似代替偏导数,得到差分方程的离散形式。

一维抛物型偏微分方程初边值问题求解

一维抛物型偏微分方程初边值问题求解

一维抛物型偏微分方程初边值问题求解摘要:一、引言1.抛物型偏微分方程简介2.初边值问题的意义和重要性二、一维抛物型偏微分方程初边值问题的求解方法1.分离变量法2.紧差分法3.Crank-Nicolson 方法4.Richardson 外推法三、Matlab程序实现1.紧差分格式求解2.追赶法解线性方程组四、案例分析1.热传导方程的初边值问题求解五、结论与展望1.初边值问题求解的重要性2.未来研究方向和挑战正文:一、引言抛物型偏微分方程是一类重要的偏微分方程,其在物理、工程、数学等领域具有广泛的应用。

其中,一维抛物型偏微分方程的初边值问题更是研究的热点。

初边值问题是指在给定的边界条件下,求解方程在空间和时间上的演化过程。

本文将介绍一维抛物型偏微分方程初边值问题的求解方法,并以热传导方程为例进行具体分析。

二、一维抛物型偏微分方程初边值问题的求解方法1.分离变量法:这是一种常用的求解初边值问题的方法,主要思想是将偏微分方程分解为多个独立的常微分方程。

通过对每个常微分方程求解,最后得到偏微分方程的解。

2.紧差分法:这是一种求解偏微分方程的数值方法。

通过在空间和时间上进行离散化,将偏微分方程转化为线性代数方程组。

然后采用追赶法或迭代法求解线性方程组,从而得到偏微分方程的数值解。

3.Crank-Nicolson 方法:这是一种经典的有限差分法,用于求解一维抛物型偏微分方程。

通过在空间和时间上进行离散化,并采用中心差分公式,将偏微分方程转化为线性代数方程组。

然后求解线性方程组,得到偏微分方程的解。

4.Richardson 外推法:这是一种提高数值解精度的方法,通过多次迭代,逐渐减少空间和时间步长,使数值解接近真实解。

无条件稳定的交替方向隐式FDTD算法

无条件稳定的交替方向隐式FDTD算法
方法并将其应用于线随机比例延迟微分方程得到方程数值方法的差分方程并了在随机比例延迟微分方程解析解均方稳定的条件下当半隐式euler方法中的参数满足条件ab2a1时此方法
无条件稳定的交替方向隐式FDTD算法
介绍了一种新的FDTD算法--交替方向隐式时域有限差分法(ADI-FDTD).该方法采用求解微分方程的交替方向隐格式改造了FDTD算法,使其能无条件稳定,从而极大地节约计算时间,成为一种计算时域电磁场分布的高效算法.同时,首次尝试利用ADI-FDTD方法结合时域近远场变换技术计算天线方向图.数值实验的结果和传统FDTD方法及理论值进行了对比,数值结果一致*较好,并节约了运算所占用的资源,提高了计算效率.

抛物型方程的O(h 4)精度新交替分段显隐格式

Z HANG h u h i, .W A S o — u 一 NG n q a We — i
( .Sho o  ̄ l ts n yt i cs hnogU i rt, i n200 ,Sadn ,C i ; 1 col f ln i dSs m S e e,Sadn n e i J a 10 hnog hn Ma e ac a e c n v sy n 5 a
w ih Wa rv d t e hg l e u a y n me ia e p r n s h c s p o e b i y a c _ ̄ b u r l x ei o h re c me t.
Ke yw
:8 1 at , i s g e tsh me o v cin df s n p be 1 ̄ l e  ̄ , e ̄ n c e ;c n e t iu i rl m;p rl l loi m 1 r  ̄ n o o o aal g r h ea t
c m n e a ll m u t n w i a e eucnio l al. h f u c t i O h ) s e e a e sdi prl o pt o , h hws rvdt b nod i aysbe T esaM acr y a ( , h c bu n ae c a i c p o o tnl t pi c a r e s
维普资讯
第4卷 2
V0 . 2 14
第 l 期 2
N .2 o 1






( 理

版)
20 年 l O7 2月
De c.2 r 007
Junl f hn ogU i r t( a r cec) ora o adn nv sy N t a Si e S ei ul n

一个解3维抛物型方程的对称形式的交替方向隐格式

第35卷 第2期2007年5月 河南师范大学学报(自然科学版)J ournal of Henan N ormal Universit y(N atural Science)V ol.35 N o.2M ay.2007 文章编号:1000-2367(2007)02-0174-03一个解3维抛物型方程的对称形式的交替方向隐格式马明书,谷淑敏,朱霖霖(河南师范大学数学与信息科学学院,河南新乡453007)摘 要:构造了一个解3维抛物型方程的交替方向隐式格式,格式绝对稳定,截断误差为O(Δt2+Δx4).关键词:3维抛物型方程;交替方向隐式格式;截断误差;稳定性中图分类号:O241.82 文献标识码:A考虑区域D:{0Φx,y,zΦ1,0ΦtΦT}上3维热传导方程初边值问题(定解条件略)5u 5t=52u5x2+52u5y2+52u5z2.(1) 用差分方法解上述问题需要构造出精度高、稳定性好、计算量小的差分格式,古典显格式[1]精度不高,截断误差阶仅为O(Δt+Δx2),且稳定性条件rΦ1/6也较为苛刻,文献[2]中的格式虽绝对稳定,精度高,其截断误差阶达O(Δt2+Δx4),但它是3层隐式格式,因计算量和存储量都很大而难以使用.文献[3-4]给出了3层高精度显式格式,但其稳定性条件都将网比r限制在1/6~1/2上,文献[5]中的3层显格式尽管绝对稳定,但精度较低,截断误差仅为O(Δt2+Δx2).所有这些3层格式都不能计算第1层上的网函数值,需用其它方法先启动,这在使用上是不方便的.本文构造了一个交替方向隐式差分格式,格式绝对稳定不仅能计算第1层上的网函数值,而且比现有的同类型的格式[1]精度都高,截断误差阶达O(Δt2+Δx4).文末的数值例子表明理论分析的正确性.1 差分格式的构造设Δt为时间步长,Δx,Δy,Δz分别为x,y,z方向的空间步长,为简便计,取Δx=Δy=Δz=1/M(M 为正整数),u n j kl表示在节点(jΔx,kΔy,lΔz,nΔt)处的网函数值,方程(1)的解函数为u(x,y,z,t),记u(j Δx,kΔy,lΔz,nΔt)=u(j,k,l,n).对于一维的抛物型方程,双层6点隐式差分格式(1-12(r-16)δ2x)u n+1j=(1+12(r+16)δ2x)u n j,(2)其中r=Δt/Δx2,δ2x是关于x的2阶中心差分,被称为Douglas格式[6],格式绝对稳定,截断误差阶达O(Δt2 +Δx4)我们将这个格式推广到3维情形,得到差分格式(1-12(r-16)(δ2x+δ2y+δ2z))u n+1jkl=(1+12(r+16)(δ2x+δ2y+δ2z))u n jkl,(3)其中r,δ2x的意义同前,δ2y,δ2z是关于y和z的2阶中心差分.可以证明,格式(3)绝对稳定,但截断误差阶只有O(Δx2+Δt2)在(3)式的两边分别加上(14(r-16)2(δ2xδ2y+δ2xδ2z+δ2yδ2z)-18(r-16)3δ2xδ2yδ2z)u n+1jkl与(14(r+16)2(δ2xδ2y+δ2xδ2z+δ2yδ2z)+18(r+16)3δ2xδ2yδ2z)u n jkl,且分解因式可得到格式收稿日期:2006-06-15基金项目:河南省教育厅自然科学基础研究基金资助项目(20031100010)作者简介:马明书(1941-),男,河南修武人,河南师范大学教授,主要从事微分方程数值解法研究.(1-12(r-16)δ2x)(1-12(r-16)δ2y)(1-12(r-16)δ2z)u n+1jkl=(1+12(r+16)δ2x)(1+12(r+16)δ2y)(1+12(r+16)δ2z)u n jkl.(4)格式(4)可以分解为交替方向隐格式(1-12(r-16)δ2x)u n+13jkl=(1+12(r+16)δ2y)u n jkl(1-12(r-16)δ2z)u n+23jkl=(1+12(r+16)δ2x)u n+13jkl(1-12(r-16)δ2y)u n+1jkl=(1+12(r+16)δ2z)u n+23jkl.(5) 使用格式(5)进行计算时每个时间层上只需解3个三对角方程组,可用追赶法求解,计算量较小.2 截断误差阶估计为估计格式(5)的截断误差阶和对其进行稳定性分析,消去过渡层u n+1/3jkl,u n+2/3jkl.即得到(4)式,故格式(4)与(5)有相同的截断误差阶和稳定性质.上面的论述以及下面的理论分析都用到了2阶中心差分算子乘积的的可交换性,这个性质仅在空间变量的变化区域为矩形或长方体时才成立[1].将(4)式写成(1-12(r-16)δ2x)(1-12(r-16)δ2y)(1-12(r-16)δ2z)un+1jkl-unjklΔt=1Δx2((δ2x+δ2y+δ2z)+ 16(δ2xδ2y+δ2xδ2z+δ2yδ2z))u n jkl+14Δx2(112+Δt2Δx4)δ2xδ2yδ2z u n jkl,(6)将(6)式中的u在节点(jΔx,kΔy,lΔz,nΔt)处展开的Taylor级数代入,经整理可得5u 5t+12Δt52u5t2-12Δt(53u5t5x2+53u5t5y2+53u5t5z2)+112Δx2(53u5t5x2+53u5t5y2+53u5t5z2)+O(Δt2+ΔtΔx2+Δx4)=52u5x2+52u5y2+52u5z2+112Δx2(54u5x4+54u5y4+54u5z4+254u5x25y2+254u5x25z2+254u5y25z2)+O(Δx4),(7)当方程(1)的解充分光滑时,有如下关系式成立:5p 5t p(525x2+525y2+525z2)q u=(525x2+525y2+525z2)p+q u,(p,q为非负整数)(8) 根据(7)式并注意到关系式(8)可知格式(5)逼近方程(1)的截断误差阶为O(Δt2+ΔtΔx2+Δx4),由于O(ΔtΔx2)Φmax{O(Δt2),O(Δx4)},故格式(5)的截断误差阶实为O(Δt2+Δx4).3 稳定性分析利用Fo urier稳定性分析方法,令u n jkl=λn e i(jθ+kφ+lψ), (i=-1)(9)将(9)式代入(4)式并注意到δ2x u n jkl=-4s1u n jkl, δ2y u n jkl=-4s2u n jkl, δ2z u n jkl=-4s3u n jkl,其中s1=sin2(θ/2),s2=sin2(φ/2),s3=sin2(ψ/2)∈[0,1],经整理可得格式(4)的传播因子为λ=[(1-2(r+16)s1)(1-2(r+16)s2)(1-2(r+16)s3)]/[(1+2(r-16)s1)(1+2(r-16)s2)(1+2(r-16)s3)]根据s1,s2,s3的取值范围可知-1Φ(1-2(r+16)s1)/(1+2(r-16)s1)Φ1,-1Φ(1-2(r+16)s2)/(1+2(r-16)s2)Φ571第2期 马明书等:一个解3维抛物型方程的对称形式的交替方向隐格式1,-1Φ(1-2(r+16)s3)/(1+2(r-16)s3)Φ1于是|λ|Φ1,根据Von Neumann条件以及差分格式(4),(5)的等价性知格式(5)无条件稳定.4 数值实验在区域D:{0Φx,y,zΦπ,tΕ0}上对初边值问题5u 5t=52u5x2+52u5y2+52u5z2,u|t=0=sin x sin y sin z, u|x=0=0, u|x=π=0,u|y=0=0, u|y=π=0, u|z=0=0, u|z=π=0,用本文格式(5)和文献[5]格式求数值解并与精确解u(x,y,z,t)=e-3t sin x sin y sin z相比较,取Δx=Δy=Δz=π/16,Δt=rΔx2,r=1/4,1/2.计算到n=200时的结果如表1.表1 各种格式计算结果比较(i,j,k)r=1/4精确解格式(5)文献[5]格式r=1/2精确解格式(5)文献[5]格式(4,4,4)0.00108870.00108870.00989930.0000033520.0000033520.000001053(9,9,9)0.00290540.00290540.00264160.0000089470.0000089440.000002810(13,13,13)0.000528080.000528080.000480140.0000016260.0000016250.000000510 以上结果看出,对所取的r,本文格式解与精确解均有很好的吻合,它较文献[5]格式解精确3~4位有效数字,与理论分析完全一致.参 考 文 献[1] 胡建伟,汤怀民.微分方程数值方法[M].北京:科学出版社,1999:204-240.[2] Douglas J,Gum E.Two2order correct difference analogues for t he equation of multi2dimensional heat flow[J].Mat h Comput,1963,17(81):71-80.[3] 曾文平.解三维抛物型方程的高精度显式格式[J].华侨大学学报,1995,16(2):128-133.[4] 马明书,王同科.三维抛物型方程的一族高精度分支稳定显格式[J].应用数学和力学,2000:21(10):1087-1092.[5] 曾文平.多维抛物型方程的分支绝对稳定的显格式[J].高等学校计算数学学报,1997;19(2):112-121.[6] 余德浩,汤华中.微分方程数值解法[M].北京:科学出版社,2003:115-116.A Symmetrical Alternating Direction Implicit Scheme for SolvingParabolic Equation of Three2dimensionMA Ming2shu,GU Shu2min,ZHU Lin2lin(College of Mat hematics and Information Science,Henan Normal University,Xinxiang453007,China)Abstract:This paper presents an Alternating Direction Implicit Scheme for solving parabolic equation of three2dimension. The scheme is absolutely stable and the truncation error for the method is O(Δt2+Δx4).K ey w ords:three2dimension parabolic equation;Alternating Direction Implicit Scheme;truncation error;stability 671河南师范大学学报(自然科学版) 2007年。

交替方向隐式CFD解法器的GPU并行计算及其优化

文章编号 : 1 0 0 1 — 9 0 8 1 ( 2 0 1 3 ) 1 0 — 2 7 8 3 — 0 4
C ODE N J YI I DU
h t t p : / / w w w . j o c a . c n
d o i : 1 0 . 1 1 7 7 2 / j . i s s n . 1 0 0 1 - 9 0 8 1 . 2 0 1 3 . 1 0 . 2 7 8 3
交 替 方 向 隐式 C F D解 法 器 的 G P U并 行 计 算 及 其优 化
邓 亮 , 徐传福, 刘 巍 , 张理 论
( 国防科学技术大学 计算机学院, 长沙 4 1 0 0 7 3 )
( } 通信作者电子邮箱 d e n g l n u d t @1 2 6 . c o m )
p r o c e s s e s o f ADI s o l v e r i n a p r a c t i c a l C F D a p p l i c a t i o n ,t h e a u t h o r s i mp l e me n t e d i f n e — g r a i n e d GP U p a r a l l e l i z a t i o n a l g o it r h m or f
A b s t r a c t :A l t e r n a t i n g D i r e c t i o n I mp l i c i t ( A D I )s c h e m e i s a t y p i c a l d i s c r e t i z a t i o n s c h e m e f o r s o l v i n g p a r t i a l d i f f e r e n t i l a

《工程数值计算Python教程》第7章 偏微分方程

2
, − = ,
将上式代入五点格式方程可以排除假格点,于是得到:

, = + ℎ, 0 + − ℎ, 0 + 1 − , 0
2

= +ℎ + −ℎ + 1−
2
利用上式计算 = 一行格点处的值,然后再用五点格式方程计算n ≥ 2时的值。
其中: = /ℎ2 。上式可用作关于变量逐步求解的工具,如果 , 在0 ≤ ≤ 1和
0 ≤ ≤ 0 时已知,那么由上式可求得 = 0 + 的解。由于解在区域边界上是已知的,
反复运用上式即可求得区域内部的近似解。该法求解过程很直接,称为显式法。
方程中四点位置如图7-2所示。

2
2
2



1
,−2
1
= , − , −

1
2
由于的值仅在的整数倍处是已知的,因此将形如 , − 的项都由在上下两个
相邻格点的算术平均值代替。
1
1
, − ≈ , + , −
2
2
代入偏微分方程得到:
1
ሾ + ℎ, + + ℎ, − − 2 , − 2 , − + − ℎ,
网格示意图
将上述方程组写为矩阵形式,已知项移至右端,未知向量排列为:
= 11 , 21 , 31 , 12 , 22 , 32 , 13 , 23 , 33
系数矩阵为:
4 − ℎ2 11
−1
0
−1
0
0
0
0
0
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % Example of ADI Method for 2D heat equation % % % % u_t = u_{xx} + u_{yy} + f(x,t) % %a<x<b;c<y<d % % Test problme: % % Exact solution: u(t,x,y) = exp(-t) sin(pi*x)sin(pi*y) % % Source term: f(t,x,y) = exp(-t) sin(pi*x) sin(pi*y) (2pi^2-1) % % % % Files needed for the test: % % % % adi.m: This file, the main calling code. % % f.m: The file defines the f(t,x,y) % % uexact.m: The exactsolution. % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear; close all; a = 0; b=1; c=0; d=1; n = 40; tfinal = 1; m = n; h = (b-a)/n; dt=h; h1 = h*h; r=dt/h1; x=a:h:b; y=c:h:d; %-- Initial condition: t = 0; for i=1:m+1, for j=1:m+1, u1(i,j) = uexact(t,x(i),y(j)); end end %---------- Big loop for time t -------------------------------------- k_t = fix(tfinal/dt); for k=1:k_t t1 = t + dt; t2 = t + dt/2; %--- sweep in x-direction -------------------------------------- for i=2:m, % Boundary condition. u2(1,i) = -r/2*uexact(t1,x(1),y(i-1)) + (1+r)*uexact(t1,x(1),y(i)) + -r/2*uexact(t1,x(1),y(i+1));u2(m+1,i) = -r/2*uexact(t1,x(m+1),y(i-1)) + (1+r)*uexact(t1,x(m+1),y(i)) + -r/2*uexact(t1,x(m+1),y(i+1)); end for j = 2:n, % Look for fixed y(j) b=zeros(m-1,1); for i=2:m, b(i-1) = r^2/4*( u1(i-1,j-1) + u1(i-1,j+1) + u1(i+1,j-1) + u1(i+1,j+1) ) ... +r/2*( 1-r )*( u1(i-1,j) + u1(i+1,j) + u1(i,j-1) + u1(i,j+1) )... + ( 1-2*r+r^2 )*u1(i,j) -3/2*dt*exp( 1/2*( (i-1)*h + (j-1)*h ) -t2 ); end b(1) = b(1) + r/2 * u2(1,j); b(m-1) =b(m-1) + r/2 * u2(m+1,j); A = diag( (1+r)*ones(m-1,1) ) + diag( -r/2*ones(m-2,1),1 ) ... + diag(-r/2*ones(m-2,1),-1); ut = A\b; % Solve the diagonal matrix. for i=1:m-1,u2(i+1,j) = ut(i); end end % Finish x-sweep. %-------------- loop in y -direction -------------------------------- for i=1:m+1, % Boundary condition u1(i,1) = uexact(t1,x(i),y(1));u1(i,n+1) = uexact(t1,x(i),y(m+1)); u1(1,i) = uexact(t1,x(1),y(i)); u1(m+1,i) =uexact(t1,x(m+1),y(i)); end for i = 2:m, for j=2:m, b(j-1) = u2(i,j); end b(1) = b(1) + r/2*u1(i,1); b(m-1) = b(m-1) + r/2*u1(i,m+1);A = diag( (1+r)*ones(m-1,1) ) + diag( -r/2*ones(m-2,1),1 ) ... + diag(-r/2*ones(m-2,1),-1); ut = A\b; for j=1:n-1, u1(i,j+1) = ut(j); end end % Finish y-sweep. t = t + dt; %--- finish ADI method at this time level, go to the next time level. end %-- Finished with the loop in time %----------- Data analysis ---------------------------------- for i=1:m+1, for j=1:n+1, ue(i,j) = uexact(tfinal,x(i),y(j)); end end [xx,yy]=meshgrid(x,y); xx=xx';yy=yy'; mesh(xx,yy,u1) title('数值解图') pause surf(xx,yy,ue) title('精确解图') pausesurf(xx,yy,u1-ue) title('误差图') e = max(max(abs(u1-ue))) % The infinityerror. %******%表示原来程序编写错误 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % Example of ADI Method for 2D heat equation % % % % u_tt = u_{xx} + u_{yy} + f(x,t) % %a<x<b;c<y<d % % Test problme: % % 精确解: u(t,x,y) = exp( 1/2*(x+y) - t ) % % 右端函数: f(t,x,y) = 1/2*exp( 1/2*(x+y) - t ) % % 初始条件: g1 = exp( 1/2*(x+y) ) % % 初始导数条件: g2 = -exp( 1/2*(x+y) ) % % 需要函数 % % g1.m g2.m 初始条件 % % f2.m: The file defines the f(t,x,y) % % uexact.m: The exactsolution. % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear; close all; a = 0; b=1; c=0; d=1; n = 10; tfinal = 1; m = n; h = (b-a)/n; dt=h; s=(dt/h)^2; x=a:h:b; y=c:h:d; %-- Initial condition: t = 0; for i=1:m+1, for j=1:m+1, u1(i,j) = g1(x(i),y(j)); end end %-- Initial deriavtive condition: for i=1:m+1, for j=1:m+1, %******% u2(i,j) = g1(x(i),y(j)) + dt * g2( x(i),y(j) ) + dt^2/4 * ( g1(x(i),y(j)) + f2 (t, x(i),y(j) ) ); u2(i,j) =g1(x(i),y(j)) + dt * g2( x(i),y(j) ) + dt^2/2 * ( g1(x(i),y(j))/2 + f2 (t, x(i),y(j) ) ); endend %---------- Big loop for time t -------------------------------------- k_t = fix(tfinal/dt); for k=2:k_t t1 = t + dt; t2 = t1 + dt; %--- sweep in x-direction --------------------------------------for i=1:m+1, % Boundary condition. u4(1,i) = ( uexact(t,x(1),y(i)) -2*uexact(t1,x(1),y(i)) + uexact(t2,x(1),y(i)) )/dt^2 ; u4(m+1,i) = ( uexact(t,x(m+1),y(i)) - 2*uexact(t1,x(m+1),y(i)) + uexact(t2,x(m+1),y(i)) )/dt^2 ; u4(i,1) = ( uexact(t,x(i),y(1)) - 2*uexact(t1,x(i),y(1)) + uexact(t2,x(i),y(1)) )/dt^2 ; u4(i,m+1) = ( uexact(t,x(i),y(m+1)) - 2*uexact(t1,x(i),y(m+1)) + uexact(t2,x(i),y(m+1)) )/dt^2 ; end for i=2:m u3(1,i)=-s/2*u4(1,i-1) + (1+s)*u4(1,i) - s/2*u4(1,i+1); u3(m+1,i)=-s/2*u4(m+1,i-1) +(1+s)*u4(m+1,i) - s/2*u4(m+1,i+1); end for j = 2:n, % Look for fixed y(j) b=zeros(m-1,1); for i=2:m, b(i-1) = ( u2(i-1,j) + u2(i+1,j) + u2(i,j-1) + u2(i,j+1) -4*u2(i,j) )/h^2 +f2 (t1, x(i),y(j) ); end b(1) = b(1) + s/2 * u3(1,j); b(m-1) = b(m-1) + s/2 * u3(m+1,j); A = diag( (1+s)*ones(m-1,1) ) + diag( -s/2*ones(m-2,1),1 ) ... + diag(-s/2*ones(m-2,1),-1); ut = A\b; % Solve the diagonal matrix. for i=1:m-1, u3(i+1,j) = ut(i); end end % Finish x-sweep. %-------------- loop in y -direction -------------------------------- for i = 2:m, forj=2:m, b(j-1) = u3(i,j); end b(1) = b(1) + s/2*u4(i,1); b(m-1) = b(m-1) + s/2*u4(i,m+1); ut = A\b; for j=1:n-1, u4(i,j+1) = ut(j); end end % Finish y-sweep. for i=1:m+1, % Boundary condition u5(i,1) = uexact(t2,x(i),y(1)); u5(i,m+1) = uexact(t2,x(i),y(m+1));u5(1,i) = uexact(t2,x(1),y(i)); u5(m+1,i) = uexact(t2,x(m+1),y(i)); end for i=2:m, forj=2:m, u5(i,j) = dt^2 * u4(i,j) + 2*u2(i,j) - u1(i,j); end end u1=u2; u2=u5; %******%t = t2 + dt; t = t+dt ; %--- finish ADI method at this time level, go to the next time level. end %-- Finished with the loop in time %----------- Data analysis ---------------------------------- for i=1:m+1, for j=1:n+1, ue(i,j) = uexact(tfinal,x(i),y(j)); end end[xx,yy]=meshgrid(x,y); xx=xx';yy=yy'; mesh(xx,yy,u2) title('数值解图') pausesurf(xx,yy,ue) title('精确解图') pause %******%surf(xx,yy,u2-ue) surf(xx,yy,u2-ue) title('误差图') e = max(max(abs(u2-ue))) %******%e = max(max(abs(u1-ue))) % The infinityerror. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % % Example of ADI Method for 2D heat equation % % % % u_t = u_{xx} + u_{yy} + f(x,t) % %a<x<b;c<y<d % % Test problme: % % Exact solution: u(t,x,y) = exp(-t) sin(pi*x) sin(pi*y) % % Source term: f(t,x,y) = exp(-t) sin(pi*x) sin(pi*y) (2pi^2-1) % % % % Files needed for the test: % % % % f.m: The file defines the f(t,x,y) % % uexact.m: The exact solution. % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% clear; close all; a = 0; b=1; c=0; d=1; n = 20; tfinal = 1; m = n; h = (b-a)/n; h1 = h*h; dt=h1; r=dt/h1; x=a:h:b; y=c:h:d; %-- Initial condition: t = 0; for i=1:m+1, for j=1:m+1, u1(i,j) = uexact(t,x(i),y(j)); end end %---------- Big loop for time t -------------------------------------- k_t = ceil(tfinal/dt); for k=1:k_t t1 = t + dt; t2 = t + dt/2; %--- sweep in x-direction -------------------------------------- for j=2:m, % Boundary condition. u2(1,j) = (1/12-r/2)*uexact(t1,x(1),y(j-1)) + (5/6+r)*uexact(t1,x(1),y(j)) + (1/12-r/2) *uexact(t1,x(1),y(j+1)); u2(m+1,j) = (1/12-r/2)*uexact(t1,x(m+1),y(j-1)) +(5/6+r)*uexact(t1,x(m+1),y(j)) + (1/12-r/2) * uexact(t1,x(m+1),y(j+1)); end for j =2:n, % Look for fixed y(j) b=zeros(m-1,1); for i=2:m, b(i-1) = (1/144+r/12+r^2/4) * ( u1(i-1,j-1) + u1(i-1,j+1) + u1(i+1,j-1) + u1(i+1,j+1) ) ... + ( 10/144+r/3-r^2/2 )*( u1(i-1,j) + u1(i+1,j) + u1(i,j-1) + u1(i,j+1) )... + ( 100/144 - 40 * r/24 + r^2 )*u1(i,j)... + dt * ( f(t2,x(i-1),y(j-1)) + f(t2,x(i-1),y(j+1)) + f(t2,x(i+1),y(j-1)) + f(t2,x(i+1),y(j+1))... + 10 * ( f(t2,x(i-1),y(j)) + f(t2,x(i+1),y(j)) + f(t2,x(i),y(j-1)) + f(t2,x(i),y(j+1)) )... + 100 *f(t2,x(i),y(j)) ) / 144; end b(1) = b(1) + ( r/2 -1/12 ) * u2(1,j); b(m-1) = b(m-1) + ( r/2 -1/12 ) * u2(m+1,j); A = diag( (5/6+r)*ones(m-1,1) ) + diag( (1/12-r/2) * ones(m-2,1),1 ) ... + diag( (1/12-r/2) * ones(m-2,1),-1); ut = A\b; % Solve the diagonal matrix. for i=1:m-1, u2(i+1,j) = ut(i); end end % Finish x-sweep. %-------------- loop in y -direction -------------------------------- for i=1:m+1, % Boundary condition u1(i,1) = uexact(t1,x(i),y(1)); u1(i,n+1) = uexact(t1,x(i),y(m+1)); u1(1,i) = uexact(t1,x(1),y(i));u1(m+1,i) = uexact(t1,x(m+1),y(i)); end for i = 2:m, for j=2:m, b(j-1) = u2(i,j); end b(1) =b(1) + ( r/2 -1/12 ) * u1(i,1); b(m-1) = b(m-1) + ( r/2 -1/12 ) * u1(i,m+1); ut = A\b; for j=1:n-1, u1(i,j+1) = ut(j); end end % Finish y-sweep. t = t + dt; %--- finish ADI method at this time level, go to the next time level. end %-- Finished with the loop in time %----------- Data analysis ---------------------------------- for i=1:m+1, for j=1:n+1, ue(i,j) = uexact(tfinal,x(i),y(j)); end end [xx,yy]=meshgrid(x,y); xx=xx';yy=yy'; mesh(xx,yy,u1) title('数值解图') pause surf(xx,yy,ue) title('精确解图') pause surf(xx,yy,u1-ue) title('误差图') e = max(max(abs(u1-ue))) % The infinity error. %算例1.1.3,见课本第17页 %首先网格剖分 xl=0.; xr=pi;%左右端点 n=20; h=(xr-xl)/n;%步长 %解差分格式形成的方程组 U=zeros(n-1,1); aa=-1+h^2/12; bb=2+5/6*h^2; A=diag(bb*ones(n-1,1))+diag(aa*ones(n-2,1),1)+diag(aa*ones(n-2,1),-1);%系数矩阵 F=zeros(n-1,1); for i=1:n-1 F(i)=h^2/12*(exp(xl+(i-1)*h)*(sin(xl+(i-1)*h)-2*cos(xl+(i-1)*h))+10*exp(xl+i*h)*(sin(xl+i*h)-2*cos(xl+i*h))+exp(xl+(i+1)*h)*(sin(xl+(i+1)*h)-2*cos(xl+(i+1)*h)));%右端项 end U=inv(A)*F;%得到数值解 %精确解与数值解误差绝对值的最大值 ana=zeros(n-1,1); for i=1:n-1 ana(i)=exp(xl+i*h)*sin(xl+i*h);%精确解 end wucha=max(abs(U-ana)) %可视化处理 x=xl:h:xr; U=[0 U' 0];%U表示数值解ana=[0 ana' 0];%ana表示精确解 plot(x,U,'r*',x,ana); %向后Euler差分格式 %Ut-Uxx=0,0<x<1,0<t<1 %u(x,0)=exp(x) %u(0,t)=exp(t),u(1,t)=exp(1+t) %%%%%%%网格剖分%%%%%%%%% % first set up the attributes h = 0.1; % the step-size in the x dimension%空间步长 r=1; %步长比 dt = h^2 * r; % the step-size in the t dimension%%时间步长 T0 = 1; % the time-interval over which we will calculate u 时间长度 TN = ceil(T0 / dt); % the number of steps in the time-interval%时间网格 X0 = 1; % the x-interval over which we will calculate u空间长度 XN = ceil(X0 / h); % the number of steps in the x-interval%空间网格 U = zeros(XN + 1, TN + 1); % set up an array of zeros %初始化 % Next we set up the initial conditions%初始值及边界值 x = 0 :h: X0; for j=1:XN+1 U(j , 1) = exp( (j-1)*h ); end for k=1:TN+1 U(1,k) =exp( (k-1) *dt);U(XN+1,k)=exp(1+ ( k-1 ) *dt); end %%%%%%形成系数矩阵%%%%%%%% A=diag((5/6+r)*ones(XN-1,1))+diag( (1/12-r/2) *ones(XN-2,1),1 )+diag( (1/12-r/2)*ones(XN-2,1),-1 ); B=diag((5/6-r)*ones(XN-1,1))+diag( (1/12+r/2) *ones(XN-2,1),1 )+diag( (1/12+r/2)*ones(XN-2,1),-1 ); %右端项初始化 F = zeros( XN-1,1 ); %迭代求解 for k = 1 : TN F(1)=(1/12+r/2)*exp((k-1)*dt)-(1/12-r/2)*exp(k*dt); F(XN-1)=(1/12+r/2)*exp((k-1)*dt+1)-(1/12-r/2)*exp(k*dt+1); U(2:XN,k+1)= A \ ( B * U(2:XN,k) + F ); end; %给出精确解 ana = zeros(XN + 1, TN + 1); for k = 1 : TN+1 for j = 1 : XN+1 ana(j, k ) = exp ( (j-1) *h + (k-1)*dt); end; end; %给出在T=1时的最大误差 max( abs ( U (:,TN+1) -ana (:,TN+1) ) ) %图示化 t=0:dt:T0; [xx,tt]= meshgrid(x,t); xx=xx'; tt=tt'; mesh(xx,tt,ana); title('analytical solution');ylabel(sprintf('steps in x domain\nfrom 0 to 1')); xlabel(sprintf('steps in time-domain\nfrom 0 to 1')); zlabel('U(x,y)'); pause mesh(xx,tt,U); title('numerical solution'); ylabel(sprintf('steps in x domain\nfrom 0 to 1')); xlabel(sprintf('steps in time-domain\nfrom 0 to 1')); zlabel('U(xi,tk)'); pause mesh(xx,tt,U-ana); title('diffeence beween analical and numeical soluion'); ylabel(sprintf('steps in x domain\nfrom 0 to 1')); xlabel(sprintf('steps in time-domain\nfrom 0 to 1')); zlabel('U(xi,tk)'); pauseplot(x,U(:,TN+1),'-or',x,ana(:,TN+1)) title('向前Euler格式数值解与精确解的比较') legend('uh(x,1)','u(x,1)')%Crank-Nicolson schemes % 显差分格式 %算例4.1.1 %%%%%%%网格剖分%%%%%%%%% h = 1/10; %%空间步长 a = 1; dt =1/20; %时间步长 s = a * dt/h; %步长比 T = 1; % 时间长度 n = ceil(T / dt); % 时间网格 X0 = 1; % 空间长度 m = ceil(X0 / h); %空间网格 u = zeros(n + 1, m + 1); %%数解初始化u(tk,xi) % %初始值及边界值 x=0:h:X0; phi=exp(x); u(1,:)=phi; %u的第一行表示初始状态 t=0:dt:T; u(:,1)=exp(t'); %左界u(:,m+1)=exp(1+t'); %%%%%%%%%%%%%%% f=zeros(n-1,m-1); for k=1:n+1 for i=1:m+1 f(k,i)=0; end end %%%%%%%%%%%%%%% psi=exp(x); for i = 2:m u(2,i) = dt * psi(i) + s^2/2 * ( phi(i-1) + phi(i+1) ) ... + ( 1 - s^2 ) * phi(i) + dt^2/2 * f(1,i);end %%%%%%%%%%%%%%% for k=2:n for i=2:m u (k+1 , i) = s^2 * u (k , i-1) +2*( 1-s^2 )*u (k , i) ... + s^2 * u (k , i+1) - u (k-1 , i) + dt^2 * f( k , i); endend %%%%%%%%%%%%%%%%给出精确解 ana = zeros(n + 1, m + 1); for k = 1 : n+1 for i = 1 : m+1 ana( k, i ) = exp ( (i-1) *h + (k-1)*dt); end;end; %%%%%%%%%%%%%%%给出在T=1时的最大误差 max( abs ( u (n+1,:) -ana (n+1,:) ) ) %图示化 [tt,xx]= meshgrid(t,x); xx=xx'; tt=tt'; mesh(tt,xx,ana);title('analytical solution'); xlabel(sprintf('steps in time domain\nfrom 0 to 1'));ylabel(sprintf('steps in x-domain\nfrom 0 to 1')); zlabel('U(x,y)'); pause mesh(tt,xx,u); title('numerical solution'); xlabel(sprintf('steps in time domain\nfrom 0 to 1'));ylabel(sprintf('steps in x-domain\nfrom 0 to 1')); zlabel('u(xi,tk)'); pause mesh(tt,xx,u-ana); title('diffeence beween analical and numeical soluion'); xlabel(sprintf('steps in time- domain\nfrom 0 to 1')); ylabel(sprintf('steps in x domain\nfrom 0 to 1')); zlabel('u-ana'); pause plot(x,u(n+1,:),'-or',x,ana(n+1,:)) title('显格式数值解与精确解的比较') legend('uh(x,1)','u(x,1)') % compace 差分格式 %算例4.3.1 %%%%%%%网格剖分%%%%%%%%% h = 1/10; %%空间步长 a = 1; dt =1/100; %时间步长 s = a * dt/h; %步长比 T = 1; % 时间长度 n = ceil(T / dt); % 时间网格 X0 = 1; % 空间长度 m = ceil(X0 / h); %空间网格 u = zeros(n + 1, m + 1); %%数解初始化u(tk,xi) % %初始值及边界值 x=0:h:X0; phi=exp(x); u(1,:)=phi; %u的第一行表示初始状态 t=0:dt:T; u(:,1)=exp(t'); %左界u(:,m+1)=exp(1+t'); %%%%%%%%%%%%%%% f=zeros(n+1,m+1); for k=1:n+1 fori=1:m+1 f(k,i)=0; end end %%%%%%%%%%%%%%% psi=exp(x); for i = 2:m u(2,i) = dt * psi(i) + s^2/2 * ( phi(i-1) + phi(i+1) ) ... + ( 1 - s^2 ) * phi(i) + dt^2/2 * f(1,i);end %%%%%%%%%%%%%%% A=diag( (5/6+s^2 )*ones( m-1,1 ) )+diag( (1/12-1/2*s^2) * ones (m-2,1),1 )+diag( (1/12-1/2*s^2) * ones (m-2,1),-1 ); B=diag( -(5/6+s^2 )*ones( m-1,1 ) )+diag( (1/2*s^2-1/12) * ones (m-2,1),1 )+diag( (1/2*s^2-1/12) * ones (m-2,1),-1 ); C=diag(5/3*ones(m-1,1))+diag(1/6*ones(m-2,1),1)+diag(1/6*ones(m-2,1),-1); for k=2:n for i=2:m F(i-1)=1/12*dt^2*( f(i-1)+10*f(i)+f(i+1) ); end F(1)=F(1) + (1/2*s^2-1/12) * ( u(k+1,1) + u(k-1,1) )+1/6*u(k,1); F(m-1)=F(m-1) + (1/2*s^2-1/12) * ( u(k+1,m+1) + u(k-1,m+1) )+1/6*u(k,m+1); y = A \ ( B * u(k-1,2:m )' + F' + C* u( k,2:m )' ); for i=1:m-1u(k+1,i+1)=y(i); end end %%%%%%%%%%%%%%%%给出精确解 ana = zeros(n + 1, m + 1); for k = 1 : n+1 for i = 1 : m+1 ana( k, i ) = exp ( (i-1) *h + (k-1)*dt); end; end; %%%%%%%%%%%%%%%给出在T=1时的最大误差 max( abs ( u (n+1,:) -ana (n+1,:) ) ) %图示化 [tt,xx]= meshgrid(t,x); xx=xx'; tt=tt'; mesh(tt,xx,ana);title('analytical solution'); xlabel(sprintf('steps in time domain\nfrom 0 to 1'));ylabel(sprintf('steps in x-domain\nfrom 0 to 1')); zlabel('U(x,y)'); pause mesh(tt,xx,u); title('numerical solution'); xlabel(sprintf('steps in time domain\nfrom 0 to 1'));ylabel(sprintf('steps in x-domain\nfrom 0 to 1')); zlabel('u(xi,tk)'); pause mesh(tt,xx,u-ana); title('diffeence beween analical and numeical soluion'); xlabel(sprintf('steps in time- domain\nfrom 0 to 1')); ylabel(sprintf('steps in x domain\nfrom 0 to 1')); zlabel('u-ana'); pause plot(x,u(n+1,:),'-or',x,ana(n+1,:)) title('显格式数值解与精确解的比较') legend('uh(x,1)','u(x,1)') % implicit difference scheme %算例4.2.1 %%%%%%%网格剖分%%%%%%%%% h = 1/10; %%空间步长 a = 1; dt = 1/10; %时间步长 s = a *dt/h; %步长比 T = 1; % 时间长度 n = ceil(T / dt); % 时间网格 X0 = 1; % 空间长度 m = ceil(X0 / h); %空间网格 u = zeros(n + 1, m + 1); %%数解初始化u(tk,xi) % %初始值及边界值 x=0:h:X0; phi=exp(x); u(1,:)=phi; %u的第一行表示初始状态 t=0:dt:T; u(:,1)=exp(t'); %左界u(:,m+1)=exp(1+t'); %%%%%%%%%%%%%%% f=zeros(n+1,m+1); for k=1:n+1 for i=1:m+1 f(k,i)=0; end end %%%%%%%%%%%%%%% psi=exp(x); for i = 2:m u(2,i) = dt * psi(i) + s^2/2 * ( phi(i-1) + phi(i+1) ) ... + ( 1 - s^2 ) * phi(i) + dt^2/2 * f(1,i);end %%%%%%%%%%%%%%% A=diag( (1+s^2 )*ones( m-1,1 ) )+diag( -1/2*s^2 * ones (m-2,1),1 )+diag( -1/2*s^2 * ones (m-2,1),-1 ); B=diag( -(1+s^2 )*ones( m-1,1 ) )+diag( 1/2*s^2 * ones (m-2,1),1 )+diag( 1/2*s^2 * ones (m-2,1),-1 ); for k=2:n F = dt^2 * f( k,2:m ); F(1)=F(1) + 1/2*s^2*( u(k+1,1) + u(k-1,1) ); F(m-1)=F(m-1) +1/2*s^2*( u(k+1,m+1) + u(k-1,m+1) ); y = A \ ( B * u(k-1,2:m )' + F' + 2 * u( k,2:m )' ); for i=1:m-1 u(k+1,i+1)=y(i); end end %%%%%%%%%%%%%%%%给出精确解 ana= zeros(n + 1, m + 1); for k = 1 : n+1 for i = 1 : m+1 ana( k, i ) = exp ( (i-1) *h + (k-1)*dt); end; end; %%%%%%%%%%%%%%%给出在T=1时的最大误差 max( abs ( u (n+1,:) -ana (n+1,:) ) ) %图示化 [tt,xx]= meshgrid(t,x); xx=xx'; tt=tt';mesh(tt,xx,ana); title('analytical solution'); xlabel(sprintf('steps in time domain\nfrom 0 to 1')); ylabel(sprintf('steps in x-domain\nfrom 0 to 1')); zlabel('U(x,y)'); pausemesh(tt,xx,u); title('numerical solution'); xlabel(sprintf('steps in time domain\nfrom 0 to 1')); ylabel(sprintf('steps in x-domain\nfrom 0 to 1')); zlabel('u(xi,tk)'); pausemesh(tt,xx,u-ana); title('diffeence beween analical and numeical soluion');xlabel(sprintf('steps in time- domain\nfrom 0 to 1')); ylabel(sprintf('steps in xdomain\nfrom 0 to 1')); zlabel('u-ana'); pause plot(x,u(n+1,:),'-or',x,ana(n+1,:)) title('显格式数值解与精确解的比较') legend('uh(x,1)','u(x,1)') % compace 差分格式 %算例4.3.1 %%%%%%%网格剖分%%%%%%%%% h = 1/10; %%空间步长 a = 1; dt =1/100; %时间步长 s = a * dt/h; %步长比 T = 1; % 时间长度 n = ceil(T / dt); % 时间网格 X0 = 1; % 空间长度 m = ceil(X0 / h); %空间网格 u = zeros(n + 1, m + 1); %%数解初始化u(tk,xi) % %初始值及边界值 x=0:h:X0; phi=exp(x); u(1,:)=phi; %u的第一行表示初始状态 t=0:dt:T; u(:,1)=exp(t'); %左界u(:,m+1)=exp(1+t'); %%%%%%%%%%%%%%% f=zeros(n+1,m+1); for k=1:n+1 for i=1:m+1 f(k,i)=0; end end %%%%%%%%%%%%%%% psi=exp(x); for i = 2:m u(2,i) = dt * psi(i) + s^2/2 * ( phi(i-1) + phi(i+1) ) ... + ( 1 - s^2 ) * phi(i) + dt^2/2 * f(1,i);end %%%%%%%%%%%%%%% A=diag( (5/6+s^2 )*ones( m-1,1 ) )+diag( (1/12-1/2*s^2) * ones (m-2,1),1 )+diag( (1/12-1/2*s^2) * ones (m-2,1),-1 ); B=diag( -(5/6+s^2 )*ones( m-1,1 ) )+diag( (1/2*s^2-1/12) * ones (m-2,1),1 )+diag( (1/2*s^2-1/12) * ones (m-2,1),-1 ); C=diag(5/3*ones(m-1,1))+diag(1/6*ones(m-2,1),1)+diag(1/6*ones(m-2,1),-1); for k=2:n for i=2:m F(i-1)=1/12*dt^2*( f(i-1)+10*f(i)+f(i+1) ); end F(1)=F(1) + (1/2*s^2-1/12) * ( u(k+1,1) + u(k-1,1) )+1/6*u(k,1); F(m-1)=F(m-1) + (1/2*s^2-1/12) * ( u(k+1,m+1) + u(k-1,m+1) )+1/6*u(k,m+1); y = A \ ( B * u(k-1,2:m )' + F' + C* u( k,2:m )' ); for i=1:m-1u(k+1,i+1)=y(i); end end %%%%%%%%%%%%%%%%给出精确解 ana = zeros(n + 1, m + 1); for k = 1 : n+1 for i = 1 : m+1 ana( k, i ) = exp ( (i-1) *h + (k-1)*dt); end; end; %%%%%%%%%%%%%%%给出在T=1时的最大误差 max( abs ( u (n+1,:) -ana (n+1,:) ) ) %图示化 [tt,xx]= meshgrid(t,x); xx=xx'; tt=tt'; mesh(tt,xx,ana);title('analytical solution'); xlabel(sprintf('steps in time domain\nfrom 0 to 1'));ylabel(sprintf('steps in x-domain\nfrom 0 to 1')); zlabel('U(x,y)'); pause mesh(tt,xx,u); title('numerical solution'); xlabel(sprintf('steps in time domain\nfrom 0 to 1'));ylabel(sprintf('steps in x-domain\nfrom 0 to 1')); zlabel('u(xi,tk)'); pause mesh(tt,xx,u-ana); title('diffeence beween analical and numeical soluion'); xlabel(sprintf('steps intime- domain\nfrom 0 to 1')); ylabel(sprintf('steps in x domain\nfrom 0 to 1')); zlabel('u-ana'); pause plot(x,u(n+1,:),'-or',x,ana(n+1,:)) title('显格式数值解与精确解的比较') legend('uh(x,1)','u(x,1)') function pdegui(action) %PDEGUI Demonstrate soluton of model partial differential equations. % PDEGUI demonstrates the finite difference solution of model problems % involving Laplace's operator: % del^2_U =partial^2_U/partial_x^2 + partial^2_U/partial_y^2. % The PDEs are: % Poisson equation, del^2_U = 1. % Eigenvalue equation, del^2_U + lambda U = 0. % Heat equation, partial_U/dt = del^2_U. % Wave equation, partial^2_U/partial_t^2 =del^2_U. % Various regions can be selected: % Square, -1 = x = 1, -1 = y = 1. % L-shaped MathWorks logo made from 3/4 of the square. % Curved 'L', with a quarter circle in the 4-th square. % Disc of radius one. % Annulus with outer radius = 1, inner radius = 1/3; % Heart-shaped cardioid. % Exterior of a "Butterfly". % Cross section of a double ridge microwave waveguide. % For the eigenvalue problem, you can set the eigenvalue index. % For the heat equation, you can set sigma = (delta_t)/(delta_x)^2. % For the wave equation, you can set sigma = ((delta_t)/(delta_x))^2. % If sigma is too large, the time-stepping methods are unstable. if nargin == 0 Initialize_GUI action ='restart'; end set(findobj('tag','stop'),'userdata',0) drawnow if isequal(action,'restart') % Retrieve parameters from GUI r = findobj('tag','region'); s = get(r,'string'); region = s{get(r,'value')}; eqn = get(findobj('tag','eqn'),'value'); n =eval(get(findobj('tag','edit1'),'string')); text2 = findobj('tag','text2'); edit2 =findobj('tag','edit2'); if eqn == 1 set(text2,'vis','off'); set(edit2,'vis','off');set(findobj('string','+'),'vis','off'); set(findobj('string','-'),'vis','off'); else set(text2,'vis','on'); set(edit2,'vis','on'); set(findobj('string','+'),'vis','on'); set(findobj('string','-'),'vis','on'); end sigma = []; indx = []; switch eqn case 1 case 2 set(text2,'string','index =')set(edit2,'string','1') indx = 1; case 3 set(text2,'string','sigma =') set(edit2,'string','0.250') sigma = .250; case 4 set(text2,'string','sigma =') set(edit2,'string','0.500') sigma = .500; end % Generate the grid. G = gengrid(region(1),n); % Generate the discrete Laplacian.A = Laplacian(G); m = size(A,1); % Initialize the solution [x,y] = meshgrid(-1:2/n:1); if eqn == 1; u = []; v = []; lambda = []; elseif eqn == 2 v = []; h = 2/n; [u,lambda] = eigswatch(-(1/h^2)*A,10); else k = (G > 0); u = zeros(m,1); v = zeros(m,1); r =sqrt((x(k)-.5).^2 + (y(k)+.5).^2); if eqn == 3 u(:) = -(15/n)*exp(-5*r); else v(:) = -(15/n)*exp(-5*r); end lambda = []; end % Save everything in figure's userdata. data.eqn = eqn; data.G = G; data.A = A; data.x = x; data.y = y; data.n = n; data.m = m; data.u = u; data.v = v; data.sigma = sigma; data.indx = indx; mbda = lambda;set(gcf,'userdata',data); else % New value of sigma or index. data = get(gcf,'userdata'); edit2 = findobj('tag','edit2'); if data.eqn == 2 data.indx = eval(get(edit2,'string')); ifisequal(action,'+') data.indx = data.indx + 1; elseif isequal(action,'-') data.indx =max(1,data.indx - 1); end set(edit2,'string',sprintf('%6.0f',data.indx)) else data.sigma = eval(get(edit2,'string')); if isequal(action,'+') data.sigma = data.sigma + .001; elseif isequal(action,'-') data.sigma = data.sigma - .001; endset(edit2,'string',sprintf('%6.3f',data.sigma)) end set(gcf,'userdata',data); end data =get(gcf,'userdata'); A = data.A; u = data.u; v = data.v; sigma = data.sigma; eqn = data.eqn; m = data.m; n = data.n; indx = data.indx; x = data.x; y = data.y; G = data.G; set(findobj('tag','stop'),'userdata',0); while get(findobj('tag','stop'),'userdata') == 0 switch eqn case 1 % Solve sparse linear system Au = 1 u = A\(ones(m,1)); u = u/max(max(abs(u))); set(findobj('tag','stop'),'userdata',1) case 2 % Some eigenvalues already computed; maybe need more. while indx >length(mbda) k = length(mbda)+10; h = 2/n; [u,lambda] = eigswatch(-(1/h^2)*A,k); mbda = lambda; data.u = u; end lambda = mbda(indx); u = data.u(:,indx); u = u/max(max(abs(u))); set(findobj('tag','stop'),'userdata',1) case 3 % One step for heat equation u = u + sigma*A*u; data.u = u; case 4 % One step for wave equation w = u; u = 2*u - v + sigma*A*u; v = w; data.u = u; data.v = v; end % Map the solution onto the grid and display it. z = G; k = find(G>0); z(k) = u(z(k));contour(x,y,z,-1:.05:1) axis square set(gca,'color','k') if eqn == 2title(sprintf('lambda(%2d) = %9.4f',indx,lambda)) end drawnow set(gcf,'userdata',data) end % ------------------------------------------------------------------------ functionInitialize_GUI clf reset set(gcf,'pos',[150 260 710420],'doublebuffer','on','menubar','none', ... 'numbertitle','off','name','PDEgui','userdata',1); set(gca,'pos',[.11 .11 .6 .815]) uicontrol( ... 'tag','region', ...'style','list', ... 'units','normal', ... 'position',[.75 .55 .20 .35], ... 'string',{'Square','L (Logo)','Curved L','Disc','Annulus',... 'Heart','Butterfly','Waveguide'}, ... 'fontsize',12, ... 'value',2, ... 'callback','pdegui(''restart'')') uicontrol( ... 'tag','eqn', ... 'style','list', ...'units','normal', ... 'position',[.75 .35 .20 .18], ...'string',{'Poisson','Eigenvalue','Heat','Wave'}, ... 'fontsize',12, ... 'value',1, ...'callback','pdegui(''restart'')') uicontrol( ... 'tag','text1', ... 'style','text', ... 'units','normal', ... 'position',[.75 .27 .10 .06], ... 'string','grid size =', ... 'fontsize',12) uicontrol( ...'tag','edit1', ... 'style','edit', ... 'units','normal', ... 'position',[.85 .27 .10 .06], ...'string','32', ... 'backgroundcolor',[1 1 1], ... 'fontsize',12, ... 'callback','pdegui(''restart'')') uicontrol( ... 'tag','text2', ... 'style','text', ... 'units','normal', ... 'position',[.75 .19 .10 .06], ... 'string','', ... 'fontsize',12) uicontrol( ... 'tag','edit2', ... 'style','edit', ... 'units','normal', ... ' position',[.85 .19 .08 .06], ... 'string','0.5', ... 'backgroundcolor',[1 1 1], ... 'fontsize',12, ... 'callback','pdegui(''sigint'')') uicontrol( ... 'style','pushbutton', ... 'units','normal', ...'position',[.93 .19 .02 .03], ... 'string','-', ... 'fontsize',12, ... 'callback','pdegui(''-'')') uicontrol( ... 'style','pushbutton', ... 'units','normal', ... 'position',[.93 .22 .02 .03], ...'string','+', ... 'fontsize',12, ... 'callback','pdegui(''+'')') uicontrol( ... 'tag','stop', ...'style','pushbutton', ... 'units','normal', ... 'position',[.80 .11 .12 .06], ... 'string','stop', ... 'fontsize',12, ... 'userdata',0, ... 'callback','set(gcbo,''userdata'',1)'); uicontrol( ...'style','pushbutton', ... 'units','normal', ... 'position',[.80 .03 .12 .06], ... 'string','close', ... 'fontsize',12, ... 'callback','close(gcf)'); % ------------------------------------------------------------------------ function [u,lambda] = eigswatch(A,k) % [u,lambda] = eigswatch(A,k) % Modify pointer and text while computing k smallest % eigenvalues and eigenvectors of sparse matrix A. ps = get(gcf,'pointer'); set(gcf,'pointer','watch') text2 =findobj('tag','text2'); edit2 = findobj('tag','edit2'); str = get(text2,'string');set(text2,'string','computing') set(edit2,'vis','off') drawnow opts.disp = 0; [u,lambda] = eigs(A,k,0,opts); [lambda,p] = sort(diag(lambda)); u = u(:,p); % Make first eigenvector positive to % distinguish it from Poisson solution. u(:,1) = abs(u(:,1));set(text2,'string',str) set(edit2,'vis','on') set(gcf,'pointer',ps) % ------------------------------------------------------------------------ function G = gengrid(R,n) %GENGRID Number the grid points in a two dimensional region. % G = GENGRID('R',n) numbers the points on an n-by-n grid in % the subregion of -1=x=1 and -1=y=1 determined by 'R'. %SPY(GENGRID('R',n)) plots the points. % LAPLACIAN(GENGRID('R',n)) generates the 5-point discrete Laplacian. % The regions currently available are: % 'S' - the entire square. % 'L' - the L-shaped domain made from 3/4 of the entire square. % 'C' - like the 'L', but with a quarter circle in the 4-th square. % 'D' - the unit disc. % 'A' - an annulus. % 'H' - a heart-shaped cardioid. % 'B' - the exterior of a "Butterfly". % 'W' - cross section of a double ridge microwave waveguide. [x,y] = meshgrid(-1:2/n:1); switch(R) case 'S' G = (x > -1) & (x 1) & (y > -1) & (y 1); case 'L' G = (x > -1) & (x 1) & (y > -1) & (y 1) & ( (x > 0) | (y > 0)); case 'C' G = (x > -1) & (x 1) & (y > -1) & (y 1) & ((x+1).^2+(y+1).^2 > 1); case 'D' G = x.^2 + y.^2 1; case 'A' G = ( x.^2 + y.^2 1) & ( x.^2 + y.^2 > 1/3); case 'H' rho = .75; sigma= .75; G = (x.^2+y.^2).*(x.^2+y.^2-sigma*y) rho*x.^2; case 'B' t = atan2(y,x); r =sqrt(x.^2 + y.^2); G = (r >= sin(2*t) + .2*sin(8*t)) & ... (x > -1) & (x 1) & (y > -1) & (y 1); case 'W' G = (x > -1) & (x 1) & (y > -1) & (y 1) & (abs(x)>1/2 | abs(y)<1/2); otherwise error('Invalid region type.'); end k = find(G); G = zeros(size(G)); G(k) = (1:length(k))'; % ------------------------------------------------------------------------ function D = Laplacian(G) %LAPLACIAN Construct five-point finite difference Laplacian. % Laplacian(G) is the sparse form of the two-dimensional, % 5-point discrete Laplacian。

相关文档
最新文档