Matlab 设置输入范围和步长

Matlab set range of inputs and step

我有这个信号:

x(t) = t*sin(m*pi*t)*heaviside(-m*pi-t)+t*cos(k*pi*t)*heaviside(t-k*pi)+sin(k*pi*t)*cos(m*pi*t)*(heaviside(t+m*pi)-heaviside(t-k*pi));

我只想使用 Matlab 以 pi/100 的步长计算从 -5pi 到 5pi 的值。我该怎么做?

假设你在某处定义了 mk,并且你有 matlab symbolic toolbox which provides the heaviside function,这是如何完成的:

% First we define the function
x = @(t) t*sin(m*pi*t)*heaviside(-m*pi-t)+t*cos(k*pi*t)*heaviside(t-k*pi)+sin(k*pi*t)*cos(m*pi*t)*(heaviside(t+m*pi)-heaviside(t-k*pi));

% Then we define the values for which we want to compute the function
t_values = -5*pi:pi/100:5*pi;

% Finally we evaluate the function
x_values = x(t_values)

详情

  1. 第一行我们将您的函数定义为 anonymous function,这是 matlab 中的一个方便工具。

  2. 然后我们用 pi/100 的步骤创建一个从 -5pi 到 5*pi 的值向量。为此,我们使用 matlab colon syntax。它使它简短而高效。

  3. 最后,我们通过将向量传递给匿名函数来评估每个 t_values 上的函数。

注意:如果您没有符号工具箱,您可以轻松地自己实现 heaviside