plot x(t) = u(t+1) - u(t-2) in matlab, without using heaviside in matlab

To plot the signal x(t) = u(t+1) - u(t-2) in MATLAB without using the heaviside() function, you can create the unit step function u(t) using simple logical conditions and then plot it.

Here is the MATLAB code to plot x(t):

% Define the time range
t = -5:0.01:5;

% Compute the unit step function
u = zeros(size(t));
u(t >= 0) = 1;  % u(t) = 1 for t >= 0

% Compute the signal x(t) = u(t+1) - u(t-2)
x = (t >= -1) - (t >= 2);

% Plot the signal x(t)
plot(t, x, 'LineWidth', 2);
xlabel('t');
ylabel('x(t)');
title('Plot of x(t) = u(t+1) - u(t-2)');
grid on;
333 chars
17 lines

This code defines the time range, computes the unit step function u(t) using logical conditions, and then computes the signal x(t) = u(t+1) - u(t-2). Finally, it plots x(t) using the plot() function.

Make sure to adjust the time range according to your requirement to get a clear plot.

gistlibby LogSnag