I have a bug on my code

It keeps saying u(t) is not defined even though I did.
% t bounded between -1 and 1 with a step of 0.01
t = -1:0.01:1;
%Plot on a graph
x_1 = 5 * u(t-0.1);
x_2 = 2 * rectangularPulse(t/0.3);
x_3 = 4 * sinc(t/0.2);
figure
plot(t,x1,'g',t,x2fx,'b',t,x3,'c');
Unrecognized function or variable 'x1'.
% Label the graph
xlabel('Amplitude')
ylabel('Time t[s]')
title('Assignment 3')
legend("x1(t)","x2(t)","x3(t)");
grid on;
% Step function implementing 𝑦(𝑡) = 𝑢(𝑡)
function y =u(t)
y(t > 0) = 1;
y(t == 0) = 0.5;
y(t < 0) = 0;
end
% Rectangular pulse function implementing 𝑦(𝑡) = Π(𝑡)
function y = rectangularPulse(t)
y = (t >= -0.5 & t <= 0.5);
end
% Sinc function implementing 𝑦(𝑡) = 𝑠𝑖𝑛𝑐(𝑡)
function y = sinc(t)
y = (sin(pi.*t) / pi.*t);
end

1 comentario

Stephen23
Stephen23 el 14 de Sept. de 2025
Movida: Image Analyst el 14 de Sept. de 2025
You have not defined the following variables/functions: x1, x2fx, x3.
Once you define those variables then your code works:
% t bounded between -1 and 1 with a step of 0.01
t = -1:0.01:1;
%Plot on a graph
x1 = 5 * u(t-0.1); % fixed name
x2 = 2 * rectangularPulse(t/0.3); % fixed name
x3 = 4 * sinc(t/0.2); % fixed name
figure
plot(t,x1,'g',t,x2,'b',t,x3,'c');
% Label the graph
xlabel('Amplitude')
ylabel('Time t[s]')
title('Assignment 3')
legend("x1(t)","x2(t)","x3(t)");
grid on;
% Step function implementing 𝑦(𝑡) = 𝑢(𝑡)
function y = u(t)
y(t > 0) = 1;
y(t == 0) = 0.5;
y(t < 0) = 0;
end
% Rectangular pulse function implementing 𝑦(𝑡) = Π(𝑡)
function y = rectangularPulse(t)
y = (t >= -0.5 & t <= 0.5);
end
% Sinc function implementing 𝑦(𝑡) = 𝑠𝑖𝑛𝑐(𝑡)
function y = sinc(t)
y = (sin(pi.*t) / pi.*t);
end

Iniciar sesión para comentar.

Respuestas (1)

Image Analyst
Image Analyst el 14 de Sept. de 2025

0 votos

You have not defined x1. x1 is a differently-named variable than x_1.
  1. If they should be the same variable, then use a consistent name, either with or without the underline.
  2. Otherwise if they are different variables you need to define exactly what x1 is.

Categorías

Más información sobre Animation en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 14 de Sept. de 2025

Respondida:

el 14 de Sept. de 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by