MATLAB - 绘制表达式或函数



MATLAB 提供强大的工具来可视化数学表达式或函数。您可以绘制各种函数,从简单的线性方程到复杂的数学表达式,并在二维或三维空间中将其可视化。这种功能对于需要分析和理解数学函数行为的工程师、科学家和数学家尤其有用。

可以使用以下方法在 matlab 中绘制表达式或函数。

  • fplot() 用于二维绘图
  • fplot3() 用于三维绘图

在 Matlab 中使用 fplot()

MATLAB 中的 fplot() 函数用于在指定范围内绘制一个变量的函数。它对于可视化数学函数和表达式特别有用。

语法

fplot(f)
fplot(f,xinterval)
fplot(funx,funy)
fplot(funx,funy,tinterval)
fplot(___,LineSpec)
fplot(___,Name,Value)
fplot(ax,___)

让我们详细了解语法。

fplot(f) − 函数在 x 的默认区间 [-5 5] 上显示函数 y = f(x) 的图形。

fplot(f,xinterval) − 函数在指定区间上绘制图形。区间应以 [xmin xmax] 的形式指定为一个二元素向量。

fplot(funx,funy) − 函数显示由参数方程 x = funx(t) 和 y = funy(t) 定义的曲线,t 的默认区间为 [-5 5]。

fplot(funx,funy,tinterval) − fplot(funx, funy, tinterval) 函数绘制由 x = funx(t) 和 y = funy(t) 定义的参数曲线,并在指定区间内绘制。区间应以 [tmin tmax] 的形式指定为一个二元素向量。

fplot(___,LineSpec) − fplot(___, LineSpec) 选项允许您指定绘图的线型、标记符号和线条颜色。例如,使用 '-r' 将绘制一条红线。此选项可以在前面语法中的任何输入参数组合之后使用。

fplot(___,Name,Value) − 使用 fplot(___, Name, Value) 允许您使用一个或多个名称-值对参数来指定线条属性。例如,'LineWidth', 2 指定 2 个点的线宽。此选项可以在前面语法中的任何输入参数组合之后使用。

fplot(ax,___) − 函数将图形绘制到 ax 指定的坐标轴中,而不是当前坐标轴 (gca)。坐标轴应指定为第一个输入参数。

让我们为上面列出的每个语法执行一些示例。

示例 1:使用 fplot(f)

假设我们要为函数 y = x2 绘图

使用 fplot() 函数。

% Define the function
f = @(x) x.^2;

% Plot the function
fplot(f)

当您在 matlab 命令窗口中执行上述代码时,输出为:

fplot

示例 2:使用 fplot(f,xinterval)

假设我们要在区间 [-2, 2] 上绘制函数 y = x3

我们的代码如下:

% Define the function
f = @(x) x.^3;

% Specify the interval
xinterval = [-2, 2];

% Plot the function over the specified interval
fplot(f, xinterval)

在此示例中,我们首先使用匿名函数 f = @(x) x.^3 定义函数 y = x3。然后我们将区间指定为 xinterval = [-2, 2]。fplot(f, xinterval) 函数在此区间 [-2, 2] 上绘制此函数。最后,我们添加标题和标签以更好地理解绘图。

执行代码后,输出为:

fplot xinterval

示例 3:使用 fplot(funx,funy)

假设我们要使用参数方程绘制一个圆:

x=cos(t)

y=sin(t)

% Define the parametric equations for a circle
funx = @(t) cos(t);
funy = @(t) sin(t);

% Plot the circle
fplot(funx, funy)

在此示例中,我们使用匿名函数 funx = @(t) cos(t) 和 funy = @(t) sin(t) 定义圆的参数方程。然后,fplot(funx, funy) 函数在 t 的默认区间 [-5 5] 上绘制由这些参数方程定义的圆。

执行代码后,输出为:

funx funy

示例 4:使用 fplot(funx,funy,tinterval)

假设我们要使用参数方程绘制一个心形线:

x=2cos(t)(1−cos(t))

y=2sin(t)(1−cos(t))

区间为 [0,2π]

% Define the parametric equations for a cardioid
funx = @(t) 2 * cos(t) .* (1 - cos(t));
funy = @(t) 2 * sin(t) .* (1 - cos(t));

% Specify the interval
tinterval = [0, 2*pi];

% Plot the cardioid
fplot(funx, funy, tinterval)

在此示例中,我们使用匿名函数 funx 和 funy 定义心形线的参数方程。然后我们为参数 t 指定区间 tinterval = [0, 2*pi]。fplot(funx, funy, tinterval) 函数然后在此指定区间内绘制心形线。

执行代码后,输出为:

fplot tinterval

示例 5:使用 fplot(___,LineSpec)

假设我们要使用虚线绘制函数 y = x2

% Define the function
f = @(x) x.^2;

% Plot the function with line style '-.' (dashed) and color 'g' (green)
fplot(f, '-.g')

在此示例中,我们使用 '-.' LineSpec 指定虚线 ('-')、标记 ('.') 和颜色 ('g' 表示绿色)。然后,fplot(f, '-.g') 函数使用指定的线型、标记和颜色绘制函数 y = x2

执行代码后,输出为:

fplot linespec

示例 6:使用 fplot(___,Name,Value)

假设我们要绘制一条较粗的红线函数 y=sin(x)。

% Define the function
f = @(x) sin(x);

% Plot the function with line width of 2 points and color 'r' (red)
fplot(f, 'LineWidth', 2, 'Color', 'r')

在此示例中,我们使用 'LineWidth' 名称-值对参数指定 2 个点的线宽,并使用 'Color' 名称-值对参数指定红色 ('r') 颜色。然后,fplot(f, 'LineWidth', 2, 'Color', 'r') 函数使用指定的线宽和颜色绘制函数 y=sin(x)。

执行代码后,输出为:

fplot name value

示例 7:使用 fplot(ax,___)

假设我们要将函数 y = x2 绘制到一组特定的坐标轴中,而不是默认坐标轴。

上面的代码如下:

% Define the function
f = @(x) x.^2;

% Create a new figure and axes
figure;
ax = axes;

% Plot the function into the specified axes
fplot(ax, f)

在此示例中,我们首先使用 figure 和 axes 函数创建一个新的图形和坐标轴。然后,我们使用 fplot(ax, f) 函数将函数 y = x2 绘制到 ax 指定的坐标轴中。

当您在 matlab 命令窗口中执行代码时,输出为:

fplot ax

在 Matlab 中使用 fplot3()

在 MATLAB 中,fplot3() 函数用于绘制三维参数曲线。它允许您将由三维空间中的参数方程定义的曲线可视化。这对于理解三维几何中复杂曲线的形状和行为可能很有用。

语法

fplot3(xt,yt,zt)
fplot3(xt,yt,zt,[tmin tmax])
fplot3(___,LineSpec)
fplot3(___,Name,Value)

让我们详细了解语法的解释。

fplot3(xt,yt,zt) − 函数绘制由 x(t)=xt、y(t)=yt 和 z(t)=zt 表示的参数曲线,默认区间为 -5<t<5。

fplot3(xt,yt,zt,[tmin tmax]) − 函数绘制由 x(t)=xt、y(t)=yt 和 z(t)=zt 表示的参数曲线,区间为 tmin<t<tmax。

fplot3(___,LineSpec) − 函数利用 LineSpec 指定绘图的线型、标记符号和线条颜色。

fplot3(___,Name,Value) − 允许您使用一个或多个 Name,Value 对参数指定线条属性。这些设置应用于所有绘制的线条。要为单个线条设置选项,请使用 fplot3 返回的对象。

现在让我们查看上面解释的每个语法的示例。

示例 1:使用 fplot3(xt,yt,zt)

假设我们要绘制三维空间中由参数方程给出的螺旋线:

x(t)=cos(t)

y(t)=sin(t)

z(t)=t

绘图代码如下:

% Define the parametric equations
xt = @(t) cos(t);
yt = @(t) sin(t);
zt = @(t) t;

% Plot the 3D parametric curve
fplot3(xt, yt, zt)

在这个例子中,fplot3(xt, yt, zt) 函数使用指定的参数方程在 3D 空间中绘制螺旋线。生成的绘图显示螺旋线随着 t 的增加沿 z 轴延伸,在默认区间 -5<t<5 上形成 3D 空间中的螺旋形状。

当您在 matlab 命令窗口中执行代码时,输出为:

fplot3 xt

示例 2:使用 fplot3(xt,yt,zt,[tmin tmax])

假设我们想绘制 3D 空间中由参数方程给出的螺旋线的一部分:

x(t)=cos(t)

y(t)=sin(t)

z(t)=t

在区间 0<t<4π 上。

绘图代码如下:

xt = @(t) cos(t);
yt = @(t) sin(t);
zt = @(t) t;

tmin = 0;
tmax = 4*pi;

fplot3(xt, yt, zt, [tmin, tmax])

在这个例子中,fplot3(xt, yt, zt, [tmin, tmax]) 函数在指定的区间 0<t<4π 上绘制 3D 空间中螺旋线的一部分。生成的绘图显示螺旋线随着 **t** 从 0 增加到 4π 沿 z 轴延伸,在 3D 空间中形成螺旋形状。

在 MATLAB 命令窗口中执行代码后,输出结果为:

fplot3 tmin

示例 3:使用 fplot3(___,LineSpec)

假设我们想在 3D 空间中绘制由函数 x(t)=cos(t)、y(t)=sin(t) 和 z(t)=t 表示的参数曲线,并且我们希望曲线显示为红色虚线。

其代码如下:

xt = @(t) cos(t);
yt = @(t) sin(t);
zt = @(t) t;

fplot3(xt, yt, zt, '-r')

在这个例子中,fplot3(xt, yt, zt, '-r') 函数使用指定的参数方程和 LineSpec -r(红色虚线)在 3D 空间中绘制参数曲线。生成的绘图显示曲线为红色虚线样式。

执行代码后,输出为:

fplot3 linespec

示例 4:使用 fplot3(___,Name,Value)

假设我们想在 3D 空间中绘制由函数 x(t)=cos(t)、y(t)=sin(t) 和 z(t)=t 表示的参数曲线,并且我们希望曲线显示为更粗的线宽和蓝色。

我们的代码如下:

xt = @(t) cos(t);
yt = @(t) sin(t);
zt = @(t) t;
fplot3(xt, yt, zt, 'LineWidth', 2, 'Color', 'b')

在这个例子中,fplot3(xt, yt, zt, 'LineWidth', 2, 'Color', 'b') 函数使用指定的参数方程和线属性在 3D 空间中绘制参数曲线。生成的绘图显示曲线具有 2 个像素点的较粗线宽和蓝色。

当您在 matlab 命令窗口中执行代码时,输出为:

fplot3 name value
广告
© . All rights reserved.