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 Commento

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

Accedi per commentare.

Risposte (1)

Image Analyst
Image Analyst il 14 Set 2025

0 voti

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.

Categorie

Scopri di più su Language Fundamentals in Centro assistenza e File Exchange

Tag

Richiesto:

il 14 Set 2025

Risposto:

il 14 Set 2025

Community Treasure Hunt

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

Start Hunting!

Translated by