meshgrid 的使用方法

本文详细介绍了meshgrid函数的使用方法,包括其语法、描述及注意事项。该函数主要用于生成二维或三维网格矩阵,适用于评估二维变量函数及创建三维表面图。
meshgrid 的使用方法:
[X,Y] = meshgrid(x,y) 将向量x和y定义的区域转换成矩阵X和Y,这两个矩阵可以用来表示mesh和surf的三维空间点以及两个变量的赋值。其中矩阵X的行向量是向量x的简单复制,而矩阵Y的列向量是向量y的简单复制。
 

Generate X and Y matrices for three-dimensional plots


Syntax:
[X,Y] = meshgrid(x,y)
[X,Y] = meshgrid(x)
[X,Y,Z] = meshgrid(x,y,z)

 

Description:

[X,Y] = meshgrid(x,y) transforms the domain specified by vectors x and y into arrays X and Y, which can be used to evaluate functions of two variables and three-dimensional mesh/surface plots. The rows of the output array X are copies of the vector x; columns of the output array Y are copies of the vector y.
[X,Y] = meshgrid(x) is the same as [X,Y] = meshgrid(x,x).
[X,Y,Z] = meshgrid(x,y,z) produces three-dimensional arrays used to evaluate functions of three variables and three-dimensional volumetric plots.

 
Remarks:

The meshgrid function is similar to ndgrid except that the order of the first two input and output arguments is switched. That is, the statement [X,Y,Z] = meshgrid(x,y,z)

produces the same result as [Y,X,Z] = ndgrid(y,x,z)

Because of this, meshgrid is better suited to problems in two- or three-dimensional Cartesian space, while ndgrid is better suited to multidimensional problems that aren't spatially based.
meshgrid is limited to two- or three-dimensional Cartesian space.

 

 

From:网易博客

详细解释:help meshgrid

meshgrid用于从数组a和b产生网格。生成的网格矩阵A和B大小是相同的。它也可以是更高维的。

[A,B]=Meshgrid(a,b)
生成size(b)Xsize(a)大小的矩阵A和B。它相当于a从一行重复增加到size(b)行,把b转置成一列再重复增加到size(a)列。因此命令等效于:

A=ones(size(b))*a;
B=b'*ones(size(a))

如下所示:

>> a=[1:2]

a =

        2

>> b=[3:5]

b =

           5

>> [A,B]=meshgrid(a,b)

A =

        2
        2
        2


B =

        3
        4
        5

 

>> [B,A]=meshgrid(b,a)

B =

           5
           5


A =

           1
           2

例①:求出连续时间信号的拉氏变换式,并画出图形 求函数拉氏变换程序如下: syms t s%定义符号变量 ft=sym('sin(t)*Heaviside(t)');    %定义时间函数f(t)的表达式 Fs=laplace(ft) %求f(t)的拉氏变换式F(s) 运行结果:Fs = 1/(s^2+1) 绘制拉氏变换三维曲面图的方法有2种: 方法一:syms x y s s=x+i*y;%产生复变量s FFs=1/(s^2+1);%将F(s)表示成复变函数形式 FFss=abs(FFs); %求出F(s)的模 ezmesh(FFss);                %画出拉氏变换的网格曲面图 ezsurf(FFss);                 %画出带阴影效果的三维曲面图 colormap(hsv);               %设置图形中多条曲线的颜色顺序 方法二:figure(2)                    %打开另一个图形窗口 x1=-5: 0.1:5;                 %设置s平面的横坐标范围 y1=-5: 0.1: 5;                %设置s平面的纵坐标范围 [x,y]=meshgrid(x1,y1);        %产生矩阵 s=x+i*y;                    %产生矩阵s来表示所绘制曲面图的复平面区域, %其中矩阵s包含了复平面-6<σ<6,-6<jω<6范围内%以间隔0.01取样的所有样点 fs=1./(s.*s+1);               %计算拉氏变换在复平面上的样点值 ffs=abs(fs);                 %求幅值 mesh(x,y,ffs);               %绘制拉氏变换的三维网格曲面图 surf(x,y,ffs);                %绘制带阴影效果的三维曲面图 axis([-5,5,-5,5,0,8]);          %设置坐标显示范围 colormap(hsv);              %设置图形中多条曲线的颜色顺序用这个例题求出图中的题目
最新发布
11-28
![](https://cdn.jsdelivr.net/gh/cdn-x/xiangxixian@main/uPic/%E5%B1%8F%E5%B9%95%E6%88%AA%E5%9B%BE%202025-11-27%20150424.png) ### 题目重述 求下列函数的拉氏变换式,并用 MATLAB 绘制其在 $ s $ 平面的三维曲面图: 1. $ f_1(t) = 2e^{-t}u(t) + 5e^{-3t}u(t) $ 2. $ f_2(t) = u(t) - u(t-2) $ 3. $ f_3(t) = e^{-3t}\sin(t)u(t) $ 4. $ f_4(t) = \sin(t)[u(t) - u(t-2)] $ --- ### 详解 #### ① $ f_1(t) = 2e^{-t}u(t) + 5e^{-3t}u(t) $ - **拉氏变换**: $$ \mathcal{L}\{e^{-at}u(t)\} = \frac{1}{s+a} $$ 所以: $$ F_1(s) = \frac{2}{s+1} + \frac{5}{s+3} $$ - **MATLAB 程序**: ```matlab syms t s; ft1 = 2*exp(-t)*heaviside(t) + 5*exp(-3*t)*heaviside(t); Fs1 = laplace(ft1) % 绘图 x = -6:0.1:6; y = -6:0.1:6; [X,Y] = meshgrid(x,y); s = X + 1i*Y; Fs1_mesh = 2./(s+1) + 5./(s+3); F_abs = abs(Fs1_mesh); surf(X,Y,F_abs); xlabel('\sigma'); ylabel('j\omega'); zlabel('|F(s)|'); title('Magnitude of F_1(s)'); colormap(hsv); ``` --- #### ② $ f_2(t) = u(t) - u(t-2) $ - **拉氏变换**: $$ \mathcal{L}\{u(t-a)\} = \frac{e^{-as}}{s},\quad \mathcal{L}\{u(t)\} = \frac{1}{s} $$ 所以: $$ F_2(s) = \frac{1}{s} - \frac{e^{-2s}}{s} = \frac{1 - e^{-2s}}{s} $$ - **MATLAB 程序**: ```matlab syms t s; ft2 = heaviside(t) - heaviside(t-2); Fs2 = laplace(ft2) % 绘图 x = -1:0.05:3; y = -4:0.1:4; [X,Y] = meshgrid(x,y); s = X + 1i*Y; Fs2_mesh = (1 - exp(-2*s))./s; F_abs = abs(Fs2_mesh); % 避免除零警告 F_abs(isnan(F_abs)) = 0; surf(X,Y,F_abs); xlabel('\sigma'); ylabel('j\omega'); zlabel('|F(s)|'); title('Magnitude of F_2(s)'); colormap(hsv); ``` --- #### ③ $ f_3(t) = e^{-3t}\sin(t)u(t) $ - **拉氏变换**: $$ \mathcal{L}\{e^{-at}\sin(\omega t)u(t)\} = \frac{\omega}{(s+a)^2 + \omega^2} $$ 这里 $ a=3,\ \omega=1 $,所以: $$ F_3(s) = \frac{1}{(s+3)^2 + 1} $$ - **MATLAB 程序**: ```matlab syms t s; ft3 = exp(-3*t)*sin(t)*heaviside(t); Fs3 = laplace(ft3) % 绘图 x = -8:0.1:0; y = -4:0.1:4; [X,Y] = meshgrid(x,y); s = X + 1i*Y; Fs3_mesh = 1./((s+3).^2 + 1); F_abs = abs(Fs3_mesh); surf(X,Y,F_abs); xlabel('\sigma'); ylabel('j\omega'); zlabel('|F(s)|'); title('Magnitude of F_3(s)'); colormap(hsv); ``` --- #### ④ $ f_4(t) = \sin(t)[u(t) - u(t-2)] $ - **分析**:这是一个截断的正弦信号,持续时间为 $ [0,2] $ 秒。 - 可写为:$ f_4(t) = \sin(t)u(t) - \sin(t)u(t-2) $ - 利用时移性质: $$ \mathcal{L}\{\sin(t)u(t)\} = \frac{1}{s^2 + 1},\quad \mathcal{L}\{\sin(t)u(t-2)\} = e^{-2s} \mathcal{L}\{\sin(t+2)u(t)\} \ne e^{-2s} \cdot \frac{1}{s^2+1} $$ 注意:$ \sin(t)u(t-2) $ 不等于 $ \sin(t-2+2)u(t-2) $,需展开: $$ \sin(t) = \sin((t-2)+2) = \sin(t-2)\cos(2) + \cos(t-2)\sin(2) $$ 所以: $$ \mathcal{L}\{\sin(t)u(t-2)\} = e^{-2s} \left[ \frac{s\cos(2)}{s^2+1} + \frac{\sin(2)}{s^2+1} \right] = e^{-2s} \cdot \frac{s\cos(2) + \sin(2)}{s^2 + 1} $$ 因此: $$ F_4(s) = \frac{1}{s^2+1} - e^{-2s} \cdot \frac{s\cos(2) + \sin(2)}{s^2 + 1} = \frac{1 - e^{-2s}(s\cos(2) + \sin(2))}{s^2 + 1} $$ - **MATLAB 程序**: ```matlab syms t s; ft4 = sin(t)*(heaviside(t) - heaviside(t-2)); Fs4 = laplace(ft4) % MATLAB 自动处理 % 数值绘图 x = -2:0.1:2; y = -4:0.1:4; [X,Y] = meshgrid(x,y); s = X + 1i*Y; cos2 = double(cos(2)); sin2 = double(sin(2)); numerator = 1 - exp(-2*s).*(s*cos2 + sin2); denominator = s.^2 + 1; Fs4_mesh = numerator ./ denominator; F_abs = abs(Fs4_mesh); surf(X,Y,F_abs); xlabel('\sigma'); ylabel('j\omega'); zlabel('|F(s)|'); title('Magnitude of F_4(s)'); colormap(hsv); ``` --- ### 知识点 1. **拉普拉斯变换基本性质**: 包括线性性、时移性、频域平移等,是求解变换式的核心工具。 2. **常见函数的拉氏变换对**: 如 $ \mathcal{L}\{e^{-at}u(t)\} = \frac{1}{s+a} $,$ \mathcal{L}\{\sin(\omega t)u(t)\} = \frac{\omega}{s^2+\omega^2} $。 3. **MATLAB 绘制复变函数模曲面**: 通过构造复平面网格 `s = x + iy`,计算函数模值 `|F(s)|`,使用 `surf` 或 `mesh` 绘图。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值