近轴近似亥姆霍兹方程求解matlab偏微分方程

合集下载

(完整版)偏微分方程的MATLAB解法

(完整版)偏微分方程的MATLAB解法

引言偏微分方程定解问题有着广泛的应用背景。

人们用偏微分方程来描述、解释或者预见各种自然现象,并用于科学和工程技术的各个领域fll。

然而,对于广大应用工作者来说,从偏微分方程模型出发,使用有限元法或有限差分法求解都要耗费很大的工作量,才能得到数值解。

现在,MATLAB PDEToolbox已实现对于空间二维问题高速、准确的求解过程。

偏微分方程如果一个微分方程中出现的未知函数只含一个自变量,这个方程叫做常微分方程,也简称微分方程;如果一个微分方程中出现多元函数的偏导数,或者说如果未知函数和几个变量有关,而且方程中出现未知函数对几个变量的导数,那么这种微分方程就是偏微分方程。

常用的方法有变分法和有限差分法。

变分法是把定解问题转化成变分问题,再求变分问题的近似解;有限差分法是把定解问题转化成代数方程,然后用计算机进行计算;还有一种更有意义的模拟法,它用另一个物理的问题实验研究来代替所研究某个物理问题的定解。

虽然物理现象本质不同,但是抽象地表示在数学上是同一个定解问题,如研究某个不规则形状的物体里的稳定温度分布问题,由于求解比较困难,可作相应的静电场或稳恒电流场实验研究,测定场中各处的电势,从而也解决了所研究的稳定温度场中的温度分布问题。

随着物理科学所研究的现象在广度和深度两方面的扩展,偏微分方程的应用范围更广泛。

从数学自身的角度看,偏微分方程的求解促使数学在函数论、变分法、级数展开、常微分方程、代数、微分几何等各方面进行发展。

从这个角度说,偏微分方程变成了数学的中心。

一、MATLAB方法简介及应用1.1 MATLAB简介MATLAB是美国MathWorks公司出品的商业数学软件,用于算法开发、数据可视化、数据分析以及数值计算的高级技术计算语言和交互式环境,主要包括MATLAB和Simulink两大部分。

1.2 Matlab主要功能数值分析数值和符号计算工程与科学绘图控制系统的设计与仿真数字图像处理数字信号处理通讯系统设计与仿真财务与金融工程1.3 优势特点1) 高效的数值计算及符号计算功能,能使用户从繁杂的数学运算分析中解脱出来;2) 具有完备的图形处理功能,实现计算结果和编程的可视化;3) 友好的用户界面及接近数学表达式的自然化语言,使学者易于学习和掌握;4) 功能丰富的应用工具箱(如信号处理工具箱、通信工具箱等) ,为用户提供了大量方便实用的处理工具。

Matlab求解微分方程(组)及偏微分方程(组)之欧阳语创编

Matlab求解微分方程(组)及偏微分方程(组)之欧阳语创编

第四讲 Matlab求解微分方程(组)理论介绍:Matlab求解微分方程(组)命令求解实例:Matlab求解微分方程(组)实例实际应用问题通过数学建模所归纳得到的方程,绝大多数都是微分方程,真正能得到代数方程的机会很少.另一方面,能够求解的微分方程也是十分有限的,特别是高阶方程和偏微分方程(组).这就要求我们必须研究微分方程(组)的解法:解析解法和数值解法.一.相关函数、命令及简介1.在Matlab中,用大写字母D表示导数,Dy表示y关于自变量的一阶导数,D2y表示y关于自变量的二阶导数,依此类推.函数dsolve用来解决常微分方程(组)的求解问题,调用格式为:X=dsolve(‘eqn1’,’eqn2’,…)函数dsolve用来解符号常微分方程、方程组,如果没有初始条件,则求出通解,如果有初始条件,则求出特解.注意,系统缺省的自变量为t2.函数dsolve 求解的是常微分方程的精确解法,也称为常微分方程的符号解.但是,有大量的常微分方程虽然从理论上讲,其解是存在的,但我们却无法求出其解析解,此时,我们需要寻求方程的数值解,在求常微分方程数值解方面,MATLAB 具有丰富的函数,我们将其统称为solver ,其一般格式为:[T,Y]=solver(odefun,tspan,y0)说明:(1)solver 为命令ode45、ode23、ode113、ode15s 、ode23s 、ode23t 、ode23tb 、ode15i 之一.(2)odefun 是显示微分方程'(,)y f t y =在积分区间tspan 0[,]f t t =上从0t 到f t 用初始条件0y 求解.(3)如果要获得微分方程问题在其他指定时间点012,,,,f t t t t 上的解,则令tspan 012[,,,]f t t t t =(要求是单调的).(4)因为没有一种算法可以有效的解决所有的ODE 问题,为此,Matlab 提供了多种求解器solver ,对于不同的ODE 问题,采用不同的solver.表1 Matlab 中文本文件读写函数说明:ode23、ode45是极其常用的用来求解非刚性的标准形式的一阶微分方程(组)的初值问题的解的Matlab常用程序,其中:ode23采用龙格-库塔2阶算法,用3阶公式作误差估计来调节步长,具有低等的精度.ode45则采用龙格-库塔4阶算法,用5阶公式作误差估计来调节步长,具有中等的精度.3.在matlab命令窗口、程序或函数中创建局部函数时,可用内联函数inline,inline函数形式相当于编写M函数文件,但不需编写M-文件就可以描述出某种数学关系.调用inline函数,只能由一个matlab表达式组成,并且只能返回一个变量,不允许[u,v]这种向量形式.因而,任何要求逻辑运算或乘法运算以求得最终结果的场合,都不能应用inline函数,inline函数的一般形式为:FunctionName=inline(‘函数内容’, ‘所有自变量列表’)例如:(求解F(x)=x^2*cos(a*x)-b ,a,b是标量;x是向量)在命令窗口输入:Fofx=inline(‘x .^2*cos(a*x)-b’ , ‘x’,’a’,’b’);g= Fofx([pi/3 pi/3.5],4,1)系统输出为:g=-1.5483 -1.7259注意:由于使用内联对象函数inline不需要另外建立m文件,所有使用比较方便,另外在使用ode45函数的时候,定义函数往往需要编辑一个m文件来单独定义,这样不便于管理文件,这里可以使用inline来定义函数.二.实例介绍1.几个可以直接用Matlab求微分方程精确解的实例例1 求解微分方程2'2xy xy xe-+=程序:syms x y; y=dsolve(‘Dy+2*x*y=x*exp(-x^2)’,’x’)例2 求微分方程'0x+-=在初始条件(1)2xy y e=下的特y e解并画出解函数的图形.程序:syms x y; y=dsolve(‘x*Dy+y-exp(1)=0’,’y(1)=2*exp(1)’,’x ’);ezplot(y)例 3 求解微分方程组530t dx x y e dt dy x y dt ⎧++=⎪⎪⎨⎪--=⎪⎩在初始条件00|1,|0t t x y ====下的特解并画出解函数的图形.程序:syms x y t[x,y]=dsolve('Dx+5*x+y=exp(t)','Dy-x-3*y=0','x(0)=1','y(0)=0','t')simple(x);simple(y)ezplot(x,y,[0,1.3]);axis auto2.用ode23、ode45等求解非刚性标准形式的一阶微分方程(组)的初值问题的数值解(近似解)例4 求解微分方程初值问题2222(0)1dy y x x dx y ⎧=-++⎪⎨⎪=⎩的数值解,求解范围为区间[0,0.5].程序:fun=inline('-2*y+2*x^2+2*x','x','y');[x,y]=ode23(fun,[0,0.5],1);plot(x,y,'o-')例5 求解微分方程22'2(1)0,(0)1,(0)0d y dy y y y y dt dt μ--+===的解,并画出解的图形.分析:这是一个二阶非线性方程,我们可以通过变换,将二阶方程化为一阶方程组求解.令12,,7dy x y x dt μ===,则编写M-文件vdp.mfunction fy=vdp(t,x)fy=[x(2);7*(1-x(1)^2)*x(2)-x(1)];end在Matlab 命令窗口编写程序y0=[1;0][t,x]=ode45(@vdp,[0,40],y0);或[t,x]=ode45('vdp',[0,40],y0);y=x(:,1);dy=x(:,2);plot(t,y,t,dy)练习与思考:M-文件vdp.m 改写成inline 函数程序?3.用Euler 折线法求解Euler 折线法求解的基本思想是将微分方程初值问题 化成一个代数(差分)方程,主要步骤是用差商()()y x h y x h +-替代微商dy dx ,于是 记1,(),k k k k x x h y y x +=+=从而1(),k k y y x h +=+于是例6用Euler折线法求解微分方程初值问题的数值解(步长h取0.4),求解范围为区间[0,2].分析:本问题的差分方程为程序:>> clear>> f=sym('y+2*x/y^2');>> a=0;>> b=2;>> h=0.4;>> n=(b-a)/h+1;>> x=0;>> y=1;>> szj=[x,y];%数值解>> for i=1:n-1y=y+h*subs(f,{'x','y'},{x,y});%subs,替换函数x=x+h;szj=[szj;x,y];end>>szj>> plot(szj(:,1),szj(:,2))说明:替换函数subs例如:输入subs(a+b,a,4) 意思就是把a用4替换掉,返回 4+b,也可以替换多个变量,例如:subs(cos(a)+sin(b),{a,b},[sym('alpha'),2])分别用字符alpha替换a和2替换b,返回cos(alpha)+sin(2)特别说明:本问题可进一步利用四阶Runge-Kutta 法求解,Euler折线法实际上就是一阶Runge-Kutta 法,Runge-Kutta法的迭代公式为相应的Matlab程序为:>> clear>> f=sym('y+2*x/y^2');>> a=0;>> b=2;>> h=0.4;>> n=(b-a)/h+1;>> x=0;>> y=1;>> szj=[x,y];%数值解>> for i=1:n-1l1=subs(f,{'x','y'},{x,y});替换函数l2=subs(f,{'x','y'},{x+h/2,y+l1*h/2});l3=subs(f,{'x','y'},{x+h/2,y+l2*h/2});l4=subs(f,{'x','y'},{x+h,y+l3*h});y=y+h*(l1+2*l2+2*l3+l4)/6;x=x+h;szj=[szj;x,y];end>>szj>> plot(szj(:,1),szj(:,2))练习与思考:(1)ode45求解问题并比较差异.(2)利用Matlab 求微分方程(4)(3)''20y y y -+=的解. (3)求解微分方程''2',2(1)0,030,(0)1,(0)0y y y y x y y --+=≤≤==的特解.(4)利用Matlab 求微分方程初值问题2''''00(1)2,|1,|3x x x y xy y y ==+===的解. 提醒:尽可能多的考虑解法三.微分方程转换为一阶显式微分方程组Matlab 微分方程解算器只能求解标准形式的一阶显式微分方程(组)问题,因此在使用ODE 解算器之前,我们需要做的第一步,也是最重要的一步就是借助状态变量将微分方程(组)化成Matlab 可接受的标准形式.当然,如果ODEs 由一个或多个高阶微分方程给出,则我们应先将它变换成一阶显式常微分方程组.下面我们以两个高阶微分方程组构成的ODEs 为例介绍如何将它变换成一个一阶显式微分方程组.Step 1 将微分方程的最高阶变量移到等式左边,其它移到右边,并按阶次从低到高排列.形式为:Step 2 为每一阶微分式选择状态变量,最高阶除外注意:ODEs 中所有是因变量的最高阶次之和就是需要的状态变量的个数,最高阶的微分式不需要给它状态变量.Step 3 根据选用的状态变量,写出所有状态变量的一阶微分表达式练习与思考:(1)求解微分方程组 其中2r =1r =*1,μμ=-1/82.45,μ=(0) 1.2,x =(2)求解隐式微分方程组提示:使用符号计算函数solve求'''',x y ,然后利用求解微分方程的方法四.偏微分方程解法Matlab 提供了两种方法解决PDE 问题,一是使用pdepe 函数,它可以求解一般的PDEs,具有较大的通用性,但只支持命令形式调用;二是使用PDE 工具箱,可以求解特殊PDE 问题,PDEtoll 有较大的局限性,比如只能求解二阶PDE 问题,并且不能解决片微分方程组,但是它提供了GUI 界面,从复杂的编程中解脱出来,同时还可以通过File —>Save As 直接生成M 代码.1.一般偏微分方程(组)的求解(1)Matlab 提供的pdepe 函数,可以直接求解一般偏微分方程(组),它的调用格式为:sol=pdepe(m,@pdefun,@pdeic,@pdebc,x,t)@pdefun 是PDE 的问题描述函数,它必须换成标准形式:这样,PDE 就可以编写入口函数:[c,f,s]=pdefun(x,t,u,du),m,x,t 对应于式中相关参数,du 是u 的一阶导数,由给定的输入变量可表示出c,f,s 这三个函数.@pdebc 是PDE 的边界条件描述函数,它必须化为形式:于是边值条件可以编写函数描述为:[pa,qa,pb,qb]=pdebc(x,t,u,du),其中a 表示下边界,b 表示上边界.@pdeic 是PDE 的初值条件,必须化为形式:00(,)u x t u =,故可以使用函数描述为:u0=pdeic(x)sol 是一个三维数组,sol(:,:,i)表示i u 的解,换句话说,k u 对应x(i)和t(j)时的解为sol(i,j,k),通过sol ,我们可以使用pdeval 函数直接计算某个点的函数值.(2)实例说明求解偏微分其中, 5.7311.46()x x F x e e -=-且满足初始条件12(,0)1,(,0)0u x u x ==及边界条件1(0,)0,u t x ∂=∂221(0,)0,(1,)1,(1,)0u u t u t t x ∂===∂解:(1)对照给出的偏微分方程和pdepe 函数求解的标准形式,原方程改写为 可见1121220.024()10,,,()10.17u F u u x m c f s F u u u x ∂⎡⎤⎢⎥--⎡⎤⎡⎤∂====⎢⎥⎢⎥⎢⎥-∂⎣⎦⎣⎦⎢⎥⎢⎥∂⎣⎦%目标PDE 函数function [c,f,s]=pdefun(x,t,u,du)c=[1;1];f=[0.024*du(1);0.17*du(2)];temp=u(1)-u(2);s=[-1;1].*(exp(5.73*temp)-exp(-11.46*temp)) end(2)边界条件改写为:下边界2010.*00f u ⎡⎤⎡⎤⎡⎤+=⎢⎥⎢⎥⎢⎥⎣⎦⎣⎦⎣⎦上边界1110.*000u f -⎡⎤⎡⎤⎡⎤+=⎢⎥⎢⎥⎢⎥⎣⎦⎣⎦⎣⎦ %边界条件函数function [pa,qa,pb,qb]=pdebc(xa,ua,xb,ub,t) pa=[0;ua(2)];qa=[1;0];pb=[ub(1)-1;0];qb=[0;1];end(3)初值条件改写为:1210u u ⎡⎤⎡⎤=⎢⎥⎢⎥⎣⎦⎣⎦%初值条件函数function u0=pdeic(x)u0=[1;0];end(4)编写主调函数clcx=0:0.05:1;t=0:0.05:2;m=0;sol=pdepe(m,@pdefun,@pdeic,@pdebc,x,t); subplot(2,1,1)surf(x,t,sol(:,:,1))subplot(2,1,2)surf(x,t,sol(:,:,2))练习与思考: This example illustrates the straightforward formulation, computation, and plotting of the solution of a single PDE.This equation holds on an interval 01x ≤≤ for times 0t ≥. The PDE satisfies the initial condition (,0)sin u x x π= and boundary conditions2.PDEtool 求解偏微分方程(1)PDEtool (GUI )求解偏微分方程的一般步骤在Matlab命令窗口输入pdetool,回车,PDE工具箱的图形用户界面(GUI)系统就启动了.从定义一个偏微分方程问题到完成解偏微分方程的定解,整个过程大致可以分为六个阶段Step 1 “Draw模式”绘制平面有界区域Ω,通过公式把Matlab系统提供的实体模型:矩形、圆、椭圆和多边形,组合起来,生成需要的平面区域.Step 2 “Boundary模式”定义边界,声明不同边界段的边界条件.Step 3 “PDE模式”定义偏微分方程,确定方程类型和方程系数c,a,f,d,根据具体情况,还可以在不同子区域声明不同系数.Step 4 “Mesh模式”网格化区域Ω,可以控制自动生成网格的参数,对生成的网格进行多次细化,使网格分割更细更合理.Step 5 “Solve模式”解偏微分方程,对于椭圆型方程可以激活并控制非线性自适应解题器来处理非线性方程;对于抛物线型方程和双曲型方程,设置初始边界条件后可以求出给定时刻t的解;对于特征值问题,可以求出给定区间上的特征值.求解完成后,可以返回到Step 4,对网格进一步细化,进行再次求解.Step 6 “View模式”计算结果的可视化,可以通过设置系统提供的对话框,显示所求的解的表面图、网格图、等高线图和箭头梯形图.对于抛物线型和双曲线型问题的解还可以进行动画演示.(2)实例说明用法求解一个正方形区域上的特征值问题:正方形区域为:11,1 1.-≤≤-≤≤x x(1)使用PDE工具箱打开GUI求解方程(2)进入Draw模式,绘制一个矩形,然后双击矩形,在弹出的对话框中设置Left=-1,Bottom=-1,Width=2,Height=2,确认并关闭对话框(3)进入Boundary模式,边界条件采用Dirichlet 条件的默认值(4)进入PDE模式,单击工具栏PDE按钮,在弹出的对话框中方程类型选择Eigenmodes,参数设置c=1,a=-1/2,d=1,确认后关闭对话框(5)单击工具栏的∆按钮,对正方形区域进行初始网格剖分,然后再对网格进一步细化剖分一次(6)点开solve菜单,单击Parameters选项,在弹出的对话框中设置特征值区域为[-20,20](7)单击Plot菜单的Parameters项,在弹出的对话框中选中Color、Height(3-D plot)和show mesh 项,然后单击Done确认(8)单击工具栏的“=”按钮,开始求解。

Matlab偏微分方程求解方法

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偏微分方程组求解

matlab偏微分方程组求解

matlab偏微分方程组求解摘要:一、引言1.介绍Matlab 在偏微分方程组求解中的应用2.阐述偏微分方程组的重要性和应用领域3.说明Matlab 在偏微分方程组求解中的优势二、Matlab 偏微分方程组求解方法1.有限差分法2.有限元法3.边界元法4.其他求解方法三、Matlab 偏微分方程组求解步骤1.准备模型和参数2.选择适当的求解方法3.编写求解脚本4.分析结果四、Matlab 偏微分方程组求解案例分析1.二维热传导方程2.二维亥姆霍兹方程3.三维波动方程五、结论1.总结Matlab 在偏微分方程组求解中的应用2.强调Matlab 在偏微分方程组求解中的重要性3.展望Matlab 在偏微分方程组求解领域的发展前景正文:一、引言Matlab 是一款功能强大的数学软件,广泛应用于科学计算、数据分析、建模等领域。

偏微分方程组是描述众多自然现象和工程问题的数学模型,求解偏微分方程组对于理解这些现象和问题具有重要意义。

Matlab 提供了丰富的工具箱和函数,可以方便地求解偏微分方程组,为科研和工程应用提供了强大的支持。

二、Matlab 偏微分方程组求解方法Matlab 提供了多种求解偏微分方程组的方法,包括有限差分法、有限元法、边界元法等。

有限差分法是一种常用的数值求解方法,通过离散化方程组,将偏微分方程转化为离散形式的代数方程组,从而求解。

有限元法和边界元法是另外两种常用的数值求解方法,分别通过将偏微分方程转化为有限个单元的加权积分和边界上的加权积分,从而求解。

除了上述方法外,Matlab 还支持其他求解方法,如有限体积法、谱方法等。

有限体积法是将偏微分方程组的控制区域划分为有限个体积单元,通过对单元内的值进行插值,得到离散形式的偏微分方程组。

谱方法则是利用傅里叶变换将偏微分方程组转化为频域问题,从而求解。

三、Matlab 偏微分方程组求解步骤求解偏微分方程组的过程主要包括准备模型和参数、选择适当的求解方法、编写求解脚本和分析结果四个步骤。

matlab解偏微分方程

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中。

matlab 求解偏微分方程

matlab 求解偏微分方程

matlab 求解偏微分方程求解偏微分方程是数学中的一种重要问题,而MATLAB是一种功能强大的数学软件,可以用于求解各种数学问题,包括偏微分方程。

在本文中,我们将探讨如何使用MATLAB求解偏微分方程,并介绍一些常用的求解方法和技巧。

偏微分方程是描述自然界中许多现象的数学模型,例如热传导、扩散、波动等。

求解偏微分方程的目标是找到满足方程条件的未知函数。

MATLAB提供了许多内置函数和工具箱,可以方便地进行偏微分方程的求解。

我们需要定义偏微分方程的边界条件和初始条件。

边界条件是指在求解区域的边界上给定的条件,而初始条件是指在求解区域内给定的初始状态。

这些条件将帮助我们确定偏微分方程的解。

接下来,我们可以使用MATLAB中的偏微分方程求解函数来求解方程。

MATLAB提供了几种常用的求解方法,例如有限差分法、有限元法和谱方法等。

通过选择合适的求解方法,我们可以得到偏微分方程的数值解。

在使用MATLAB求解偏微分方程时,我们还可以使用一些技巧和优化方法来提高求解效率。

例如,可以使用网格剖分方法来将求解区域划分为若干小区域,从而减少计算量。

此外,还可以使用迭代方法来逐步逼近偏微分方程的解,从而提高求解精度。

除了求解偏微分方程,MATLAB还可以用于可视化偏微分方程的解。

通过使用MATLAB的绘图函数,我们可以将数值解以图形的形式展示出来,从而更直观地理解偏微分方程的解。

需要注意的是,在使用MATLAB求解偏微分方程时,我们需要考虑计算资源的限制。

由于偏微分方程的求解通常需要大量的计算和存储资源,因此我们需要合理安排计算机的内存和处理器的使用,以避免计算过程中的错误或崩溃。

总结起来,MATLAB是一种强大的数学软件,可以用于求解偏微分方程。

通过选择合适的求解方法和优化技巧,我们可以得到偏微分方程的数值解,并用图形的形式展示出来。

使用MATLAB求解偏微分方程可以帮助我们更好地理解和解决实际问题,是数学研究和工程应用中的重要工具之一。

MATLAB偏微分方程求解课件

MATLAB偏微分方程求解课件
• 偏微分方程分为①线性偏微分方程式与② 非线性偏微分方程式,常常有几个解而且 涉及额外的边界条件。
常微分方程:在微分方程中,若自变量的个数只有一个的微分方程。
偏微分方程:自变量的个数有两个或两个以上的微分方程。
求解偏微分方程的方法
• 求解偏微分方程的数值方法: • 1. 有限元法(Finite Element Method, FEM)---
hp-FEM • 2. 有限体积法(Finite Volume Method, FVM) • 3. 有限差分法(Finite Difference Method, FDM)。
• 其它:广义有限元法(Generalized Finite Element Method, FFEM)、扩展有限元法(eXtended Finite Element Method, XFEM)、无网格有限元法(Meshfree Finite Element Method)、离散迦辽金有限元法(Discontinuous Galerkin Finite Element Method, DGFEM)等。
step3:边界条件和初值条件
• 初值条件可以通过【Solve】->【 Parameters…】设置
• 边值条件设置如下 • (1)点击工具栏的第6 个按钮【区域边界】,
显示如下
• (2)【Boundary】->【Remove All Subdomain Borders】移除所有子域的边界, 将得到所有子域合并成一个求解域
是name后边的名字'NumberTitle','off'是关掉默认显示名字。 • subplot(211) • surf(x,t,sol(:,:,1))%sol(:,:,i)表示ui的解 • title('The Solution of u_1') • xlabel('X') • ylabel('T') • zlabel('U') • subplot(212) • surf(x,t,sol(:,:,2))%sol(:,:,i)表示ui的解 • title('The Solution of u_2') • xlabel('X') • ylabel('T') • zlabel('U')

偏微分方程的matlab解法PPT课件

偏微分方程的matlab解法PPT课件

求解抛物型方程的例子
考虑一个带有矩形孔的金属板上的热传导问题。板的左边
保持在100 °C,板的右边热量从板向环境空气定常流动,
t t 其他边及内孔边界保持绝缘。初始
°C ,于是概括为如下定解问题;
是板的温度为0 0
d u u0 , t
u 1 0 0 ,在 左 边 界 上
u 1, 在 右 边 界 上 n u = 0, 其 他 边 界 上 n
ppt课件完整
3
先确定方程大类
ppt课件完整
4
Draw Mode
画图模式,先将处理的区域画出来,二 维,方形,圆形,支持多边形,可以手 动更改坐标,旋转rotate
例如,对于细杆导热,虽然是一维问题, 可以将宽度y虚拟出来,对应于y的边界 条件和初始条件按照题意制定
ppt课件完整
5
Boundary Mode
ppt课件完整
20
ppt课件完整
21
第七步:单击Plot菜单中Parameter选项,打开Plot Selection对话框,选中Color,Height(3D plot)和 Show mesh三项.再单击Polt按钮,显示三维图形解, 如图22.5所示.
ppt课件完整
22
第八步:若要画等值线图和矢量场图,单击plot菜单 中parameter 选项,在plot selection对话框中选中 contour 和arrow两选项。然后单击plot按钮,可显示 解的等值线图和矢量场图,如图2.6所示。
2u (2u t 2 x2
u t to 0
区域的边界顶点坐标为(-0.5,-0.8), (0.5,-0.8), (-0.5,0.8), (-0.05,0.4) ,(0.05,-0.4),
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

近轴近似亥姆霍兹方程求解matlab偏微分方

在Matlab中解决近轴近似亥姆霍兹方程需要使用PDE工具箱。

首先,我们需要编写一个包含方程和边界条件的PDE模型,并将其输入到PDE工具箱中。

假设我们要解决以下的近轴近似亥姆霍兹方程:
$\frac{\partial^2 u}{\partial r^2} +
\frac{1}{r}\frac{\partial u}{\partial r} +
\frac{1}{r^2}\frac{\partial^2 u}{\partial \theta^2} + k^2 u =0$
其中,$u=u(r,\theta)$是待求解的函数,$k$是一个已知的常数。

为了在Matlab中求解这个方程,我们需要将其转换为PDE工具箱可接受的形式。

具体来说,我们需要将二阶偏微分算子转换为一阶偏微分算子,通过重新定义变量 $u$ 和一些新的变量 $v$ 和 $w$ 。

令 $u(r,\theta) = e^{-ikz}v(r,\theta)$,则方程可以转换为以下形式:
$-\frac{\partial^2 v}{\partial z^2} + \frac{\partial^2 v}{\partial r^2} + \frac{1}{r}\frac{\partial v}{\partial r} + \frac{1}{r^2}\frac{\partial^2 v}{\partial \theta^2} -k^2 v
=0$
现在,我们可以将这个方程输入到PDE工具箱中,定义边界条件并求解该方程:
```
model = createpde();
geometryFromEdges(model,@circleg);
applyBoundaryCondition(model,'neumann','Edge',1:'Edge',4,'g', 0);
specifyCoefficients(model,'m',0,'d',1,'c',1,'a',0,'f',-k*k*v); setInitialConditions(model,v0);
tlist = linspace(0,2*pi/k,20);
results = solvepde(model,tlist);
```
这里,我们使用了circleg几何对象来定义圆形域,并将其应用到了
我们的PDE模型中。

我们还定义了边界条件,其中我们设置了所有边
缘上的法向导数为零。

我们使用specifyCoefficients函数指定了方
程中的各项系数,并使用setInitialConditions函数定义了初始条件。

最后,我们使用solvepde函数求解该方程并将结果存储到results变
量中。

当求解完成后,我们可以使用Matlab内置的PDE解决器绘制结果:
```
u = results.NodalSolution;
pdeplot(model,'XYData',u(:,:,end),'ZData',u(:,:,end));
```
这将绘制出在 $t = 2\pi/k$ 时刻的解。

相关文档
最新文档