Jun Seo-He
Jun Seo-He

Reputation: 131

Matlab set range of inputs and step

I have this signal:

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));

and I want to calculate the values only from -5pi to 5pi with a step of pi/100 using Matlab. How could I do it?

Upvotes: 0

Views: 291

Answers (1)

LNiederha
LNiederha

Reputation: 948

Provided you have defined m and k somewhere, and you have the matlab symbolic toolbox which provides the heaviside function, this is how it is done:

% 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)

Details

  1. First line we define your function as an anonymous function which is a handy tool in matlab.

  2. Then we create a vector of values from -5pi to 5*pi with steps of pi/100. For this we use the matlab colon syntax. It makes it short and efficient.

  3. Finally we evaluate the function on each of the t_values by passing the vector to the anonymous function.

Note: If you don't have the symbolic toolbox, you could easily implement heaviside yourself.

Upvotes: 1

Related Questions