偏微分方程解的几道算例(差分、有限元)-含matlab程序(1)
matlab_pde

8
五点差分格式在MATLAB中实现
A=-4*eye((nx-2)*(ny-2),(nx-2)*(ny-2)); b=zeros((nx-2)*(ny-2),1); for i=1:(nx-2)*(ny-2); if mod(i,nx-2)==1 if i==1 A(1,2)=1; A(1,nx-1)=1; b(1)=-u0(1,2)-u0(2,1); else if i==(ny-3)*(nx-2)+1 A(i,i+1)=1; A(i,i-nx+2)=1; %注意边界节点的离散方式 b(i)=-u0(ny-1,1)-u0(ny,2); else A(i,i+1)=1; A(i,i-nx+2)=1; A(i,i+nx-2)=1; b(i)=-u0(floor(i/(nx-2))+2,1); end end else if mod(i,nx-2)==0 if i==nx-2 9
一维对流方程——迎风格式算例
end u0=u1; end if a>0 u=u1((M+1):M+n); else u=u1(1:n); end format long;
20
一维对流方程——迎风格式算例
然后在MATLAB窗口输入下列命令: u=peHypbYF(1,0.005,101,0,1,100);
基于Matlab的偏微分 方程数值解
求数值解方法
差分方法 有限元方法
MATLAB的pedpe函数
MATLAB的PDEtool工具箱
偏微分方程分类
椭圆偏微分方程 双曲线偏微分方程 抛物线偏微分方程
椭圆偏微分方程特例—拉普拉斯方程
拉普拉斯方程是最简单的椭圆偏微分方程,以下以拉
(完整版)偏微分方程的MATLAB解法

引言偏微分方程定解问题有着广泛的应用背景。
人们用偏微分方程来描述、解释或者预见各种自然现象,并用于科学和工程技术的各个领域fll。
然而,对于广大应用工作者来说,从偏微分方程模型出发,使用有限元法或有限差分法求解都要耗费很大的工作量,才能得到数值解。
现在,MATLAB PDEToolbox已实现对于空间二维问题高速、准确的求解过程。
偏微分方程如果一个微分方程中出现的未知函数只含一个自变量,这个方程叫做常微分方程,也简称微分方程;如果一个微分方程中出现多元函数的偏导数,或者说如果未知函数和几个变量有关,而且方程中出现未知函数对几个变量的导数,那么这种微分方程就是偏微分方程。
常用的方法有变分法和有限差分法。
变分法是把定解问题转化成变分问题,再求变分问题的近似解;有限差分法是把定解问题转化成代数方程,然后用计算机进行计算;还有一种更有意义的模拟法,它用另一个物理的问题实验研究来代替所研究某个物理问题的定解。
虽然物理现象本质不同,但是抽象地表示在数学上是同一个定解问题,如研究某个不规则形状的物体里的稳定温度分布问题,由于求解比较困难,可作相应的静电场或稳恒电流场实验研究,测定场中各处的电势,从而也解决了所研究的稳定温度场中的温度分布问题。
随着物理科学所研究的现象在广度和深度两方面的扩展,偏微分方程的应用范围更广泛。
从数学自身的角度看,偏微分方程的求解促使数学在函数论、变分法、级数展开、常微分方程、代数、微分几何等各方面进行发展。
从这个角度说,偏微分方程变成了数学的中心。
一、MATLAB方法简介及应用1.1 MATLAB简介MATLAB是美国MathWorks公司出品的商业数学软件,用于算法开发、数据可视化、数据分析以及数值计算的高级技术计算语言和交互式环境,主要包括MATLAB和Simulink两大部分。
1.2 Matlab主要功能数值分析数值和符号计算工程与科学绘图控制系统的设计与仿真数字图像处理数字信号处理通讯系统设计与仿真财务与金融工程1.3 优势特点1) 高效的数值计算及符号计算功能,能使用户从繁杂的数学运算分析中解脱出来;2) 具有完备的图形处理功能,实现计算结果和编程的可视化;3) 友好的用户界面及接近数学表达式的自然化语言,使学者易于学习和掌握;4) 功能丰富的应用工具箱(如信号处理工具箱、通信工具箱等) ,为用户提供了大量方便实用的处理工具。
二阶椭圆偏微分方程实例求解(附matlab代码)

《微分方程数值解法》期中作业实验报告二阶椭圆偏微分方程第一边值问题姓名:学号:班级:2013年11月19日二阶椭圆偏微分方程第一边值问题摘要对于解二阶椭圆偏微分方程第一边值问题.课本上已经给出了相应的差分方程。
而留给我的难题就是把差分方程组表示成系数矩阵的形式.以及对系数进行赋值。
解决完这个问题之后.我在利用matlab 解线性方程组时.又出现“out of memory ”的问题。
因为99*99阶的矩阵太大.超出了分配给matlab 的使用内存。
退而求其次.当n=10.h=1/10或n=70.h=1/70时.我都得出了很好的计算结果。
然而在解线性方程组时.无论是LU 分解法或高斯消去法.还是gauseidel 迭代法.都能达到很高的精度。
关键字:二阶椭圆偏微分方程 差分方程 out of memory LU 分解 高斯消去法 gauseidel 迭代法一、题目重述解微分方程:()()2222((,))((,))()(,)()(,)(,)1y x x x y y x y yxxyxye u x y e u x y x y u x y x y u x y u x y y e x e e y x e--+++-+=-++++已知边界:(0,)1,(1,),(,0)1,(,1)y x u y u y e u x u x e ====求数值解, 把区域[0,1][0,1]G =?分成121/100,1/100h h ==.n =100 注:老师你给的题F 好像写错了.应该把22x y y e x e +改成22y x y e x e +。
二、问题分析与模型建立2.1微分方程上的符号说明()()22221y x xy xy y e x e e y x e -++++2.2课本上差分方程的缺陷课本上的差分方程为:举一个例子:当i=2,j=3时.;当i=3,j=3时.。
但是.显然这两个不是同一个数.其大小也不相等。
matlab解偏微分方程

ui,j +1 − ui,j = Hui,j +1 ∆t Hui,j = a2 ui+1,j − 2ui,j + ui−1,j (∆x)2
ui,j +1 − ui,j = Hui,j ∆t 将显式与隐式相加,得平均公式 ui,j +1 − ui,j 1 1 = Hui,j + Hui,j +1 ∆t 2 2
得ui,0 = ui,2 − 2ψi
1 ui,2 = [c(ui+1,1 + ui−1,1) + 2(1 − c)ui,1 + 2ψi t] 2
3.3
例题 两端固定的弦振动
两端固定的弦, 初速为零,初位移是 h x, (0 ≤ x ≤ 2/3) 2 / 3 u(x, 0) = 1−x , (2/3 < x ≤ 1) h 1 − 2/3
作图所用程序如下,其中取c = 0.05, l = 1, h = 0.05.这里使用的方程 与初始条件表示方法与上一节相同. N=4000; c=0.05; x=linspace(0,1,420)’; u1(1:420)=0; u2(1:420)=0; u3(1:420)=0; u1(2:280)=0.05/279*(1:279)’; u1(281:419)=0.05/(419-281)*(419-(281:419)’); u2(2:419)=u1(2:419)+c/2*(u1(3:420)-2*u1(2:419)+u1(1:418)); h=plot(x,u1,’linewidth’,3); axis([0,1,-0.05,0.05]); set(h,’EraseMode’,’xor’,’MarkerSize’,18) for k=2:N set(h,’XData’,x,’YData’,u2) ; drawnow; u3(2:419)=2*u2(2:419)-u1(2:419)+c*(u2(3:420)... -2*u2(2:419)+u2(1:418)); u1=u2; u2=u3; end
Matlab偏微分方程求解方法

Matlab 偏微分方程求解方法目录:§1 Function Summary on page 10-87§2 Initial Value Problems on page 10-88§3 PDE Solver on page 10-89§4 Integrator Options on page 10-92§5 Examples” on page 10-93§1 Function Summary1.1 PDE Solver” on page 10-871,2 PDE Helper Functi on” on page 10-871.3 PDE SolverThis is the MATLAB PDE solver.PDE Helper Function§2 Initial Value Problemspdepe solves systems of parabolic and elliptic PDEs in one spatial variable x and time t, of the form)xu ,u ,t ,x (s ))x u ,u ,t ,x (f x (x x t u )x u ,u ,t ,x (c m m ∂∂+∂∂∂∂=∂∂∂∂- (10-2) The PDEs hold for b x a ,t t t f 0≤≤≤≤.The interval [a, b] must be finite. mcan be 0, 1, or 2, corresponding to slab, cylindrical, or spherical symmetry,respectively. If m > 0, thena ≥0 must also hold.In Equation 10-2,)x /u ,u ,t ,x (f ∂∂ is a flux term and )x /u ,u ,t ,x (s ∂∂ is a source term. The flux term must depend on x /u ∂∂. The coupling of the partial derivatives with respect to time is restricted to multiplication by a diagonal matrix )x /u ,u ,t ,x (c ∂∂. The diagonal elements of this matrix are either identically zero or positive. An element that is identically zero corresponds to an elliptic equation and otherwise to a parabolic equation. There must be at least one parabolic equation. An element of c that corresponds to a parabolic equation can vanish at isolated values of x if they are mesh points.Discontinuities in c and/or s due to material interfaces are permitted provided that a mesh point is placed at each interface.At the initial time t = t0, for all x the solution components satisfy initial conditions of the form)x (u )t ,x (u 00= (10-3)At the boundary x = a or x = b, for all t the solution components satisfy a boundary condition of the form0)xu ,u ,t ,x (f )t ,x (q )u ,t ,x (p =∂∂+ (10-4) q(x, t) is a diagonal matrix with elements that are either identically zero or never zero. Note that the boundary conditions are expressed in terms of the f rather than partial derivative of u with respect to x-x /u ∂∂. Also, ofthe two coefficients, only p can depend on u.§3 PDE Solver3.1 The PDE SolverThe MATLAB PDE solver, pdepe, solves initial-boundary value problems for systems of parabolic and elliptic PDEs in the one space variable x and time t.There must be at least one parabolic equation in the system.The pdepe solver converts the PDEs to ODEs using a second-order accurate spatial discretization based on a fixed set of user-specified nodes. The discretization method is described in [9]. The time integration is done with ode15s. The pdepe solver exploits the capabilities of ode15s for solving the differential-algebraic equations that arise when Equation 10-2 contains elliptic equations, and for handling Jacobians with a specified sparsity pattern. ode15s changes both the time step and the formula dynamically.After discretization, elliptic equations give rise to algebraic equations. If the elements of the initial conditions vector that correspond to elliptic equations are not “consistent” with the discretization, pdepe tries to adjust them before eginning the time integration. For this reason, the solution returned for the initial time may have a discretization error comparable to that at any other time. If the mesh is sufficiently fine, pdepe can find consistent initial conditions close to the given ones. If pdepe displays amessage that it has difficulty finding consistent initial conditions, try refining the mesh. No adjustment is necessary for elements of the initial conditions vector that correspond to parabolic equations.PDE Solver SyntaxThe basic syntax of the solver is:sol = pdepe(m,pdefun,icfun,bcfun,xmesh,tspan)Note Correspondences given are to terms used in “Initial Value Problems” on page 10-88.The input arguments arem: Specifies the symmetry of the problem. m can be 0 =slab, 1 = cylindrical, or 2 = spherical. It corresponds to m in Equation 10-2. pdefun: Function that defines the components of the PDE. Itcomputes the terms f,c and s in Equation 10-2, and has the form[c,f,s] = pdefun(x,t,u,dudx)where x and t are scalars, and u and dudx are vectors that approximate the solution and its partial derivative with respect to . c, f, and s are column vectors. c stores the diagonal elements of the matrix .icfun: Function that evaluates the initial conditions. It has the formu = icfun(x)When called with an argument x, icfun evaluates and returns the initial values of the solution components at x in the column vector u.bcfun:Function that evaluates the terms and of the boundary conditions. Ithas the form[pl,ql,pr,qr] = bcfun(xl,ul,xr,ur,t)where ul is the approximate solution at the left boundary xl = a and ur is the approximate solution at the right boundary xr = b. pl and ql are column vectors corresponding to p and the diagonal of q evaluated at xl. Similarly, pr and qr correspond to xr. When m>0 and a = 0, boundedness of the solution near x = 0 requires that the f vanish at a = 0. pdepe imposes this boundary condition automatically and it ignores values returned in pl and ql.xmesh:Vector [x0, x1, ..., xn] specifying the points at which a numerical solution is requested for every value in tspan. x0 and xn correspond to a and b , respectively. Second-order approximation to the solution is made on the mesh specified in xmesh. Generally, it is best to use closely spaced mesh points where the solution changes rapidly. pdepe does not select the mesh in automatically. You must provide an appropriate fixed mesh in xmesh. The cost depends strongly on the length of xmesh. When , it is not necessary to use a fine mesh near to x=0 account for the coordinate singularity.The elements of xmesh must satisfy x0 < x1 < ... < xn.The length of xmesh must be ≥3.tspan:Vector [t0, t1, ..., tf] specifying the points at which a solution is requested for every value in xmesh. t0 and tf correspond tot and f t,respectively.pdepe performs the time integration with an ODE solver that selects both the time step and formula dynamically. The solutions at the points specified in tspan are obtained using the natural continuous extension of the integration formulas. The elements of tspan merely specify where you want answers and the cost depends weakly on the length of tspan.The elements of tspan must satisfy t0 < t1 < ... < tf.The length of tspan must be ≥3.The output argument sol is a three-dimensional array, such that•sol(:,:,k) approximates component k of the solution .•sol(i,:,k) approximates component k of the solution at time tspan(i) and mesh points xmesh(:).•sol(i,j,k) approximates component k of the solution at time tspan(i) and the mesh point xmesh(j).4.2 PDE Solver OptionsFor more advanced applications, you can also specify as input arguments solver options and additional parameters that are passed to the PDE functions.options:Structure of optional parameters that change the default integration properties. This is the seventh input argument.sol = pdepe(m,pdefun,icfun,bcfun,xmesh,tspan,options)See “Integrator Options” on page 10-92 for more information.Integrator OptionsThe default integration properties in the MATLAB PDE solver are selected to handle common problems. In some cases, you can improve solver performance by overriding these defaults. You do this by supplying pdepe with one or more property values in an options structure.sol = pdepe(m,pdefun,icfun,bcfun,xmesh,tspan,options)Use odeset to create the options structure. Only those options of the underlying ODE solver shown in the following table are available for pdepe.The defaults obtained by leaving off the input argument options are generally satisfactory. “Integrator Options” on page 10-9 tells you how to create the structure and describes the properties.PDE Properties§4 Examples•“Single PDE” on page 10-93•“System of PDEs” on page 10-98•“Additional Examples” on page 10-1031.Single PDE• “Solving the Equation” on page 10-93• “Evaluating the Solution” on page 10-98Solving the Equation. This example illustrates the straightforward formulation, solution, and plotting of the solution of a single PDE222x u t u ∂∂=∂∂π This equation holds on an interval 1x 0≤≤ for times t ≥ 0. At 0t = the solution satisfies the initial condition x sin )0,x (u π=.At 0x =and 1x = , the solution satisfies the boundary conditions0)t ,1(xu e ,0)t ,0(u t =∂∂+π=- Note The demo pdex1 contains the complete code for this example. The demo uses subfunctions to place all functions it requires in a single MATLAB file.To run the demo type pdex1 at the command line. See “PDE Solver Syntax” on page 10-89 for more information. 1 Rewrite the PDE. Write the PDE in the form)xu ,u ,t ,x (s ))x u ,u ,t ,x (f x (x x t u )x u ,u ,t ,x (c m m ∂∂+∂∂∂∂=∂∂∂∂- This is the form shown in Equation 10-2 and expected by pdepe. For this example, the resulting equation is0x u x x x t u 002+⎪⎭⎫ ⎝⎛∂∂∂∂=∂∂π with parameter and the terms 0m = and the term0s ,xu f ,c 2=∂∂=π=2 Code the PDE. Once you rewrite the PDE in the form shown above (Equation 10-2) and identify the terms, you can code the PDE in a function that pdepe can use. The function must be of the form[c,f,s] = pdefun(x,t,u,dudx)where c, f, and s correspond to the f ,c and s terms. The code below computes c, f, and s for the example problem.function [c,f,s] = pdex1pde(x,t,u,DuDx)c = pi^2;f = DuDx;s = 0;3 Code the initial conditions function. You must code the initial conditions in a function of the formu = icfun(x)The code below represents the initial conditions in the function pdex1ic. Partial Differential Equationsfunction u0 = pdex1ic(x)u0 = sin(pi*x);4 Code the boundary conditions function. You must also code the boundary conditions in a function of the form[pl,ql,pr,qr] = bcfun(xl,ul,xr,ur,t) The boundary conditions, written in the same form as Equation 10-4, are0x ,0)t ,0(x u .0)t ,0(u ==∂∂+and1x ,0)t ,1(xu .1e t ==∂∂+π- The code below evaluates the components )u ,t ,x (p and )u ,t ,x (q of the boundary conditions in the function pdex1bc.function [pl,ql,pr,qr] = pdex1bc(xl,ul,xr,ur,t)pl = ul;ql = 0;pr = pi * exp(-t);qr = 1;In the function pdex1bc, pl and ql correspond to the left boundary conditions (x=0 ), and pr and qr correspond to the right boundary condition (x=1).5 Select mesh points for the solution. Before you use the MATLAB PDE solver, you need to specify the mesh points at which you want pdepe to evaluate the solution. Specify the points as vectors t and x.The vectors t and x play different roles in the solver (see “PDE Solver” on page 10-89). In particular, the cost and the accuracy of the solution depend strongly on the length of the vector x. However, the computation is much less sensitive to the values in the vector t.10 CalculusThis example requests the solution on the mesh produced by 20 equally spaced points from the spatial interval [0,1] and five values of t from thetime interval [0,2].x = linspace(0,1,20);t = linspace(0,2,5);6 Apply the PDE solver. The example calls pdepe with m = 0, the functions pdex1pde, pdex1ic, and pdex1bc, and the mesh defined by x and t at which pdepe is to evaluate the solution. The pdepe function returns the numerical solution in a three-dimensional array sol, wheresol(i,j,k) approximates the kth component of the solution,u, evaluated atkt(i) and x(j).m = 0;sol = pdepe(m,@pdex1pde,@pdex1ic,@pdex1bc,x,t);This example uses @ to pass pdex1pde, pdex1ic, and pdex1bc as function handles to pdepe.Note See the function_handle (@), func2str, and str2func reference pages, and the @ section of MATLAB Programming Fundamentals for information about function handles.7 View the results. Complete the example by displaying the results:a Extract and display the first solution component. In this example, the solution has only one component, but for illustrative purposes, the example “extracts” it from the three-dimensional array. The surface plot shows the behavior of the solution.u = sol(:,:,1);surf(x,t,u)title('Numerical solution computed with 20 mesh points') xlabel('Distance x') ylabel('Time t')Distance xNumerical solution computed with 20 mesh points.Time tb Display a solution profile at f t , the final value of . In this example,2t t f ==.figure plot(x,u(end,:)) title('Solution at t = 2') xlabel('Distance x') ylabel('u(x,2)')Solutions at t = 2.Distance xu (x ,2)Evaluating the Solution. After obtaining and plotting the solution above, you might be interested in a solution profile for a particular value of t, or the time changes of the solution at a particular point x. The kth column u(:,k)(of the solution extracted in step 7) contains the time history of the solution at x(k). The jth row u(j,:) contains the solution profile at t(j). Using the vectors x and u(j,:), and the helper function pdeval, you can evaluate the solution u and its derivative at any set of points xout [uout,DuoutDx] = pdeval(m,x,u(j,:),xout)The example pdex3 uses pdeval to evaluate the derivative of the solution at xout = 0. See pdeval for details.2. System of PDEsThis example illustrates the solution of a system of partial differential equations. The problem is taken from electrodynamics. It has boundary layers at both ends of the interval, and the solution changes rapidly for small . The PDEs are)u u (F xu017.0t u )u u (F xu 024.0t u 212222212121-+∂∂=∂∂--∂∂=∂∂ where )y 46.11exp()y 73.5exp()y (F --=. The equations hold on an interval1x 0≤≤ for times 0t ≥.The solution satisfies the initial conditions0)0,x (u ,1)0,x (u 21≡≡and boundary conditions0)t ,1(xu,0)t ,1(u ,0)t ,0(u ,0)t ,0(x u 2121=∂∂===∂∂ Note The demo pdex4 contains the complete code for this example. The demo uses subfunctions to place all required functions in a single MATLAB file. To run this example type pdex4 at the command line.1 Rewrite the PDE. In the form expected by pdepe, the equations are⎥⎦⎤⎢⎣⎡---+⎥⎦⎤⎢⎣⎡∂∂∂∂∂∂=⎥⎦⎤⎢⎣⎡∂∂⎥⎦⎤⎢⎣⎡)u u (F )u u (F )x /u 170.0)x /u (024.0x u u t *.1121212121 The boundary conditions on the partial derivatives of have to be written in terms of the flux. In the form expected by pdepe, the left boundary condition is⎥⎦⎤⎢⎣⎡=⎥⎦⎤⎢⎣⎡∂∂∂∂⎥⎦⎤⎢⎣⎡+⎥⎦⎤⎢⎣⎡00)x /u (170.0)x /u (024.0*.01u 0212and the right boundary condition is⎥⎦⎤⎢⎣⎡=⎥⎦⎤⎢⎣⎡∂∂∂∂⎥⎦⎤⎢⎣⎡+⎥⎦⎤⎢⎣⎡-00)x /u (170.0)x /u (024.0*.1001u 2112 Code the PDE. After you rewrite the PDE in the form shown above, you can code it as a function that pdepe can use. The function must be of the form[c,f,s] = pdefun(x,t,u,dudx)where c, f, and s correspond to the , , and terms in Equation 10-2. function [c,f,s] = pdex4pde(x,t,u,DuDx)c = [1; 1];f = [0.024; 0.17] .* DuDx;y = u(1) - u(2);F = exp(5.73*y)-exp(-11.47*y);s = [-F; F];3 Code the initial conditions function. The initial conditions function must be of the formu = icfun(x)The code below represents the initial conditions in the function pdex4ic. function u0 = pdex4ic(x);u0 = [1; 0];4 Code the boundary conditions function. The boundary conditions functions must be of the form[pl,ql,pr,qr] = bcfun(xl,ul,xr,ur,t)The code below evaluates the components p(x,t,u) and q(x,t) (Equation 10-4) of the boundary conditions in the function pdex4bc.function [pl,ql,pr,qr] = pdex4bc(xl,ul,xr,ur,t)pl = [0; ul(2)];ql = [1; 0];pr = [ur(1)-1; 0];qr = [0; 1];5 Select mesh points for the solution. The solution changes rapidly for small t . The program selects the step size in time to resolve this sharp change, but to see this behavior in the plots, output times must be selected accordingly. There are boundary layers in the solution at both ends of [0,1], so mesh points must be placed there to resolve these sharp changes. Often some experimentation is needed to select the mesh that reveals the behavior of the solution.x = [0 0.005 0.01 0.05 0.1 0.2 0.5 0.7 0.9 0.95 0.99 0.995 1];t = [0 0.005 0.01 0.05 0.1 0.5 1 1.5 2];6 Apply the PDE solver. The example calls pdepe with m = 0, the functions pdex4pde, pdex4ic, and pdex4bc, and the mesh defined by x and t at which pdepe is to evaluate the solution. The pdepe function returns the numerical solution in a three-dimensional array sol, wheresol(i,j,k) approximates the kth component of the solution, μk, evaluated at t(i) and x(j).m = 0;sol = pdepe(m,@pdex4pde,@pdex4ic,@pdex4bc,x,t);7 View the results. The surface plots show the behavior of the solution components. u1 = sol(:,:,1); u2 = sol(:,:,2); figure surf(x,t,u1) title('u1(x,t)') xlabel('Distance x') 其输出图形为Distance xu1(x,t)Time tfigure surf(x,t,u2) title('u2(x,t)') xlabel('Distance x') ylabel('Time t')Distance xu2(x,t)Time tAdditional ExamplesThe following additional examples are available. Type edit examplename to view the code and examplename to run the example.。
有限差分法求解偏微分方程MATLAB

南京理工大学课程考核论文课程名称:高等数值分析论文题目:有限差分法求解偏微分方程*名:**学号: 1成绩:有限差分法求解偏微分方程一、主要内容1.有限差分法求解偏微分方程,偏微分方程如一般形式的一维抛物线型方程:22(,)()u uf x t t xαα∂∂-=∂∂其中为常数具体求解的偏微分方程如下:22001(,0)sin()(0,)(1,)00u u x t x u x x u t u t t π⎧∂∂-=≤≤⎪∂∂⎪⎪⎪=⎨⎪⎪==≥⎪⎪⎩2.推导五种差分格式、截断误差并分析其稳定性;3.编写MATLAB 程序实现五种差分格式对偏微分方程的求解及误差分析;4.结论及完成本次实验报告的感想。
二、推导几种差分格式的过程:有限差分法(finite-difference methods )是一种数值方法通过有限个微分方程近似求导从而寻求微分方程的近似解。
有限差分法的基本思想是把连续的定解区域用有限个离散点构成的网格来代替;把连续定解区域上的连续变量的函数用在网格上定义的离散变量函数来近似;把原方程和定解条件中的微商用差商来近似,积分用积分和来近似,于是原微分方程和定解条件就近似地代之以代数方程组,即有限差分方程组,解此方程组就可以得到原问题在离散点上的近似解。
推导差分方程的过程中需要用到的泰勒展开公式如下:()2100000000()()()()()()()......()(())1!2!!n n n f x f x f x f x f x x x x x x x o x x n +'''=+-+-++-+- (2-1)求解区域的网格划分步长参数如下:11k k k kt t x x h τ++-=⎧⎨-=⎩ (2-2) 2.1 古典显格式2.1.1 古典显格式的推导由泰勒展开公式将(,)u x t 对时间展开得 2,(,)(,)()()(())i i k i k k k uu x t u x t t t o t t t∂=+-+-∂ (2-3) 当1k t t +=时有21,112,(,)(,)()()(())(,)()()i k i k i k k k k k i k i k uu x t u x t t t o t t tuu x t o tττ+++∂=+-+-∂∂=+⋅+∂ (2-4)得到对时间的一阶偏导数1,(,)(,)()=()i k i k i k u x t u x t uo t ττ+-∂+∂ (2-5) 由泰勒展开公式将(,)u x t 对位置展开得223,,21(,)(,)()()()()(())2!k i k i k i i k i i u uu x t u x t x x x x o x x x x∂∂=+-+-+-∂∂ (2-6)当11i i x x x x +-==和时,代入式(2-6)得2231,1,1122231,1,1121(,)(,)()()()()(())2!1(,)(,)()()()()(())2!i k i k i k i i i k i i i i i k i k i k i i i k i i i iu uu x t u x t x x x x o x x x x u u u x t u x t x x x x o x x x x ++++----⎧∂∂=+-+-+-⎪⎪∂∂⎨∂∂⎪=+-+-+-⎪∂∂⎩(2-7) 因为1k k x x h +-=,代入上式得2231,,22231,,21(,)(,)()()()2!1(,)(,)()()()2!i k i k i k i k i k i k i k i ku uu x t u x t h h o h x xu u u x t u x t h h o h x x +-⎧∂∂=+⋅+⋅+⎪⎪∂∂⎨∂∂⎪=-⋅+⋅+⎪∂∂⎩ (2-8) 得到对位置的二阶偏导数2211,22(,)2(,)(,)()()i k i k i k i k u x t u x t u x t u o h x h+--+∂=+∂ (2-9) 将式(2-5)、(2-9)代入一般形式的抛物线型偏微分方程得21112(,)(,)(,)2(,)(,)(,)()i k i k i k i k i k i k u x t u x t u x t u x t u x t f x t o h h αττ++---+⎡⎤-=++⎢⎥⎣⎦(2-10)为了方便我们可以将式(2-10)写成11122k kk k k k i i i i i i u u u u u f h ατ++-⎡⎤--+-=⎢⎥⎣⎦(2-11) ()11122k k k k k k i i i i i i u u uu u f h τατ++----+= (2-12)最后得到古典显格式的差分格式为()111(12)k k k k k i i i i i u ra u r u u f ατ++-=-+++ (2-13)2r h τ=其中,古典显格式的差分格式的截断误差是2()o h τ+。
Matlab求解微分方程及偏微分方程

第四讲Matlab求解微分方程(组)理论介绍:Matlab求解微分方程(组)命令求解实例:Matlab求解微分方程(组)实例实际应用问题通过数学建模所归纳得到的方程,绝大多数都是微分方程,真正能得到代数方程的机会很少.另一方面,能够求解的微分方程也是十分有限的, 特别是高阶方程和偏微分方程(组).这就要求我们必须研究微分方程(组)的解法:解析解法和数值解法.一.相关函数、命令及简介1.在Matlab中,用大写字母D表示导数,Dy表示y关于自变量的一阶导数, D2y表示y关于自变量的二阶导数,依此类推.函数dsolve用来解决常微分方程(组)的求解问题,调用格式为:X=dsolve(<eqnl,,,eqn2函数dsolve用来解符号常微分方程、方程组,如果没有初始条件,则求出通解,如果有初始条件,则求出特解.注意,系统缺省的自变量为t2.函数dsolve求解的是常微分方程的精确解法,也称为常微分方程的符号解. 但是,有大量的常微分方程虽然从理论上讲,其解是存在的,但我们却无法求出其解析解,此时,我们需要寻求方程的数值解,在求常微分方程数值解方面,MATLAB具有丰富的函数,我们将其统称为solver,其一般格式为:[T,Y]=solver(odefun,tspan,yO)说明:(1 )solver 为命令ode45、ode23、odel 13、odel5s、ode23s、ode23t、ode23tb、odel5i 之一.(2)odefun是显示微分方程),=f (t,y)在积分区间tspan =[心心]上从心到“用初始条件儿求解.(3)如果要获得微分方程问题在其他指定时间点bG©…心上的解,则令(span = 『“,•••『/■](要单调的).(4)因为没有一种算法可以有效的解决所有的ODE问题,为此,Matlab提供T多种求解器solver,对于不同的ODE问题,采用不同的solver.程(组)的初值问题的解的Matlab常用程序,其中:ode23采用龙格-库塔2阶算法,用3阶公式作误差估计来调节步长,具有低等的精度.。
matlab解偏微分方程

matlab解偏微分方程Matlab是一种非常强大的数学计算工具,它可以用于解决各种数学问题。
在本文中,我们将学习如何使用Matlab解偏微分方程。
偏微分方程是一类包含未知函数的偏导数的方程。
通常,解偏微分方程是困难的,需要使用复杂的数学方法。
然而,Matlab可以大大简化这个过程。
在Matlab中,我们可以使用pdepe函数来解偏微分方程。
pdepe函数采用一个偏微分方程的系统,并返回一个包含解的向量的矩阵。
下面是一个解二维扩散方程的示例程序:%定义二维扩散方程 function [c,f,s] = diffusionpde(x,t,u,DuDx)c = 1; %系数f = DuDx; %带有时间和空间导数的项s = 0; %不带导数的项end%定义边界条件(例)function [pl,ql,pr,qr] =diffusionbc(xl,ul,xr,ur,t)pl = 0; ql = 1; %左边界(u=0)pr = 0; qr = 1; %右边界(u=0)end%定义初始条件(例)function u0 = diffusionic(x)u0 = sin(pi*x); %sin(pi*x)是初始条件方程end%主程序x = linspace(0,1,50); %空间网格t = linspace(0,1,10); %时间网格sol =pdepe(0,@diffusionpde,@diffusionic,@diffusionbc,x,t );u = sol(:,:,1); %提取第一个解%绘制解surfc(x,t,u)xlabel('位置')ylabel('时间')title('二维扩散方程的解')从上述程序中,我们可以看到pdepe的使用方法。
在主程序中,我们选择了空间和时间网格,然后定义了偏微分方程、初始条件和边界条件的函数。
最后,我们调用pdepe函数,并将解存储在变量sol中。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
A(i-1,i)=-r; A(i,i-1)=-r; end end u=zeros(N+1,M+1); u(N+1,:)=u1; for k=1:N b=u(N+2-k,2:M)+0.02; u(N+1-k,2:M)=inv(A)*b';%求解迭代方程组 end uT=u(1,:);%0.25时刻的解 %精确解与数值解画图 x1=[0,x,1]; plot(x1,uT,'o') hold u_xt = exp (-pi*pi*T)*sin (pi*x1) + x1.*(1 - x1); plot (x1, u_xt, ' r') e=u_xt-uT; 六点格式 function [e]=six(dx,dt,T) %用六点对称格式求解,dx为x方向步长,dt为t方向步长 % e为误差 M=1/dx; N=T/dt; %得到第一层的值 u1=zeros(1,M+1); x=[1:M-1]*dx; u1([2:M])= sin(pi*x)+x.*(1 - x); %网比 r=dt/dx/dx;r2=2+2*r;r3=2-2*r; %构造三对角矩阵A for i=1:M-1 A(i,i)=r2;
0.0070 0.0027
-0.0097 -0.0037
-0.0013 -0.0005
0.0082 0.0000
-0.0114 0.0000
-0.0015 0.0000
0.0087 -0.0120
-0.0016
注:这里的"误差"=精确解-数值解. 2.精确解与数值解结果图像对比
“向前差分格式”:
注:曲线表示精确解,. “向后差分格式”:
b(i+1)=r*a(i+2)+(1-2*r)*a(i+1)+r*a(i)+2*t; End 向后格式 function [e]=back(dx,dt,T) %用向后差分格式求解,dx为x方向步长,dt为t方向步长 % e为误差 M=1/dx;
N=T/dt; %得到第一层的值 u1=zeros(1,M+1); x=[1:M-1]*dx; u1([2:M])= sin(pi*x)+x.*(1 - x); %网比 r=dt/dx/dx;r2=1+2*r; %构造三对角矩阵 for i=1:M-1
u
0 j
=
0
,
j
=
0,1,....nx
,
∂u ∂t
|(0, j ) =
u
−1 j
−
u
0 j
ht
= sin x j ,
即u−j 1=u0j-ht sin xj ,
将上式代入到差分格式可以求得 u1j = ht sin x j , j = 0,1,.....nx ,
最后在迭代式中利用
u
0 j
,
u1j
,可以求得
1.区域 [−1,1]×[−1,1]被剖分成 10×10时的数值和图像结果
>>FE(-1,1,-1,1,10,10) ans=
(结果采用图片截得)
(相应的网格剖分情况) >>u=FE(-1,1,-1,1,10,10);
>>x=[-1:0.2:1]; >>y=[-1:0.2:1]; >>mesh(x,y,u)
用差分法求解如下自由振动问题的周期解:
⎧ ∂2u ∂2u
⎪ ⎪
∂t
2
−
∂x2
= 0,
− ∞ < x < ∞, t > 0,
⎨⎪u t=0 = 0,
∂u ∂t |t=0 = sin x,
⎪
⎩u(0,t) = u(2π ,t).
(一)算法描述:
1.网格剖分 取 t ∈[0, 2π ], x ∈[0, 2π ]
注:曲线表示精确解,"o"表示数值解(t=0.25 时). “六点差分格式”:
注:曲线表示精确解,"O"表示数值解(t=0.25 时). (三)结果分析
通过(一),(二),我们检验了三种方法都能很好的求解此一维热传导方程,其 中明显能发现“六点对称格式”的误差更小。 (四)程序(附最后)
实验内容 2:
for i=1:m x(i+(k-1)*m)=xl+(k-1)*(xr-xl)/(n-1); y(i+(k-1)*m)=yd+(i-1)*(yu-yd)/(m-1);
end end clear k i a(m,m)=2; a((n-1)*m+1,(n-1)*m+1)=2; a(1,1)=3; a(m*n,m*n)=3; for i=1:(n-2)
u
n j
,
n
=
2,.....nt
.
(二)实验结果:
1.时间、空间均为 0 − 2π ,且网格为 10× 10的数值与图像结果: u 在各个网点上的值(数值结果采用图片截得)
>>PP(0,2*pi,0,2*pi,2*pi/10,2*pi/10) ans=
>>u=PP(0,2*pi,0,2*pi,2*pi/10,2*pi/10); >>x=[0:2*pi/10:2*2pi]; >>y=[0:2*pi/10:2*2pi]; >>mesh(x,y,u)
《偏微分方程数值解》 上机报告
实验内容 1:
分别用向前差分格式、向后差分格式及六点对称格式,求解下列问题:
⎧ ∂u ∂2u
⎪⎪ ∂t
= ∂x 2
+ 2,
⎨⎪u(0,t) = u(1,t) = 0,
0 < x < 1,t > 0, t > 1,
⎪⎩u(x, 0) = sin(π x ) + x (1− x ).
2.时间、空间均为 0 − 2π ,且网格为 20 × 20 的图像结果(数据太多--略去):
>>u=PP(0,2*pi,0,2*pi,2*pi/20,2*pi/20); >>x=[0:2*pi/20:2*2pi]; >>y=[0:2*pi/20:2*2pi]; >>mesh(x,y,u)
(三)结果分析:
t ti = t0 + iht , ht = nt , i = 0,1,..., nt
xj
=
x0
+
jhx , hx
=
x − xo nx
,
j
= 0,1,..., nx
2.差分格式
u
i j
=
r u2 i−1 j +1
+
2(1−
r2 )uij−1
+
r u2 i−1 j −1
−
ui− 2 j
3.初值处理
, r = ht ; hx
if i>1 A(i-1,i)=-r; A(i,i-1)=-r;
end end %构造三对角矩阵B,方便得到迭代方程组的右端b for i=1:M-1
B(i,i)=r3; if i>1
B(i-1,i)=r; B(i,i-1)=r; end end u=zeros(N+1,M+1); u(N+1,:)=u1; for k=1:N b=B*(u(N+2-k,2:M))'+0.04; u(N+1-k,2:M)=inv(A)*b;%求解迭代方程组 end uT=u(1,:);%0.25时刻的解 %精确解与数值解画图 x1=[0,x,1]; plot(x1,uT,'o') hold u_xt = exp (-pi*pi*T)*sin (pi*x1) + x1.*(1 - x1); plot (x1, u_xt, ' r') e=u_xt-uT;
x 方向 h = 0.1 , t方向τ = 0.01.在 t = 0.25 时观察数值解与精确解
u = e−π 2 sin(π x) + x(1− x) 的误差. (一)算法描述:
(二)实验结果:
1.误差的数值解结果数值对比
(A)“向前差分格式”程序:
>>forward(0.1, 0.01, 0.25) Current plot held ans = 0.0000 0.0027 0.0051 0.0082 0.0070 0.0051 (B)“向后差分格式”程序: >>back(0.1, 0.01, 0.25) Current plot held ans = 0.0000 -0.0037 -0.0071 - 0.0114 -0.0097 -0.0071 (C)“六点差分格式”程序: >>six(0.1, 0.01, 0.25) Current plot held ans = 0.0000 -0.0005 -0.0009 -0.0015 - 0.0013 -0.0009
(三)程序(附最后)
实验内容 3:
用线性元求解下列问题的数值解:
⎧∆u = −2,
−1 < x, y <1,
⎪⎨u(x, −1) = u (x ,1) = 0,
−1 < x <1,
⎪⎩ux (−1, y ) = 1,ux (1, y ) = 0,
−1 < x < 1.
(一)算法描述:
(二)实验结果:
a(i*m+1,i*m+1)=4; a((i+1)*m,(i+1)*m)=4; end clear i for j=2:(m-1) a(j,j)=4;