Matlab笔记PDE CGS model create a M-file

tech2024-07-05  59

specifying the problem geometry:

ne=pdegeom d=pdegeom(bs) [x,y]=pdegeom(bs,s)

an example of how to create a cardioid.

ne=pdegeom is the number of edge segments.

d=pdegeom(bs) is a matrix with one column for each edge segment specified in bs.

Row 1 contains the start parameter value. Row 2 contains the end parameter value. Row 3 contains the label of the left-hand region (left with respect to direction induced by start and end from row 1 and 2). Row 4 contains the label of the right-hand region.

The complement of the union of all regions is assigned the region number 0.

[x,y]=pdegeom(bs,s) produces coordinates of edge segment points. bs specifies the edge segments and s the corresponding parameter values. bs can be a scalar.

注意: The parameter s should be approximately proportional to the curve length. All minimal regions should have at least two, and preferably three, edge segments in their boundary.

example:

The function cardg defines the geometry of a cardioid r = 2(1+cos(Φ)).

function [x,y]=cardg(bs,s) %CARDG Geometry File defining the geometry of a cardioid. nbs=4; if nargin==0 x=nbs; return end dl=[ 0 pi/2 pi 3*pi/2 pi/2 pi 3*pi/2 2*pi; 1 1 1 1 0 0 0 0]; if nargin==1 x=dl(:,bs); return end x=zeros(size(s)); y=zeros(size(s)); [m,n]=size(bs); if m==1 n==1, bs=bs*ones(size(s)); expand bs elseif m =size(s,1) n =size(s,2), error('bs must be scalar or of same size as s'); end nth=400; th=linspace(0,2*pi,nth); r=2*(1+cos(th)); xt=r.*cos(th); yt=r.*sin(th); th=pdearcl(th,[xt;yt],s,0,2*pi); r=2*(1+cos(th)); x(:)=r.*cos(th); y(:)=r.*sin(th);

We use the function pdearcl to make the parameter s proportional to arc length. You can test the function by typing:

» pdegplot('cardg'), axis equal » [p,e,t]=initmesh('cardg'); » pdemesh(p,e,t), axis equal

Then solve the PDE problem -Δu=1 on the geometry defined by the the cardioid. Use Dirichlet boundary conditions u = 0 . Finally plot the solution.

» u=assempde('cardb',p,e,t,1,0,1); » pdesurf(p,t,u);

m file补充

MATLAB可以执行存储在文件中的一系列语句。这样的文件被称为“m -file”,因为它们的扩展名必须是“.m"作为文件名。 m file可分为script files 和 function files

script file

由一系列普通的MATLAB语句组成。如果文件有文件名,如rotate.m,然后MATLAB命令rotate将导致执行文件中的语句。script file中的变量是全局变量,它将更改当前MATLAB会话环境中同名变量的值。 script file通常用于将数据输入到一个大的矩阵中,避免编辑输入的疏忽和错误。例如,在diskfile中输入data.m:

A = [ 1 2 3 4 5 6 7 8 ];

那么MATLAB语句data将执行data.m中的赋值。 一个m文件也可以引用其他m文件,包括递归地引用它自己。

function file

可以根据问题创建新的函数,这些函数将具有与其他MATLAB函数相同的状态。(函数文件中的变量默认为局部变量。但也可以将变量声明为全局变量。) 命名方式 函数名.m 第一行声明了函数名、输入参数和输出参数(区别于script file)。 A more general version of this function is given as follow:

function y = randint(m,n,a,b) % RANDINT Randomly generated integral matrix. % randint(m,n) returns an m-by-n such matrix with % entries between 0 and 9. % randint(m,n,a,b) returns entries between integers a and b. if nargin < 3, a=0; b=9; end y = floor((b-a+1)*rand(m,n))+a;

函数文件为randint.m。例如,MATLAB语句z = randint (4,5) 可将数字4和5是传递到变量m和n的function file,输出结果传递到变量z。由于变量在函数文件是局部的,在当前的MATLAB环境他们的名字是独立的。 注:使用nargin(“输入参数的数量”)允许为省略的输入变量设置默认值——比如上面示例中的a和b。

A function may also have multiple output arguments. For example:

function [mean, stdev] = stat(x) % STAT Mean and standard deviation % For a vector x, stat(x) returns the % mean and standard deviation of x. % For a matrix x, stat(x) returns two row % vectors containing, respectively, the % mean and standard deviation of each column. [m n] = size(x); if m == 1 m = n; % handle case of a row vector end mean = sum(x)/m; stdev = sqrt(sum(x.^2)/m -mean.^2);

将其放入磁盘文件stat.m后,例如,一个MATLAB命令**[xm, xd] = stat(x)**。将把向量x中的项的均值和标准偏差分别分配给xm和xd。对于具有多个输出参数的函数,也可以进行单个赋值。例如,xm = stat(x) (xm周围不需要括号)将把x的平均值分配给xm。

最新回复(0)