I have a bug on my code

5 Ansichten (letzte 30 Tage)
Eugenie
Eugenie am 14 Sep. 2025
Beantwortet: Image Analyst am 14 Sep. 2025
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 Kommentar
Stephen23
Stephen23 am 14 Sep. 2025
Verschoben: Image Analyst am 14 Sep. 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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Image Analyst
Image Analyst am 14 Sep. 2025
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.

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by