Error defining a discontinous function: "Output argument "y2" (and possibly others) not assigned a value in the execution with "x2" function

1 次查看(过去 30 天)
I tried defining a function, x2(t), such that:
As such, I implemented in in MatLab as follows:
function y2 = x2(t)
if 0 <= t & t < 0.2 % First condition, 0 <= t < 0.2
y2 = 3;
elseif 0.2 <= t & t < 0.4 % Second condition, 0.2 <= t < 0.4
y2 = 1.5;
elseif 0.4 <= t & t < 0.7 % Third condition, 0.4 <= t < 0.7
y2 = -1.5;
elseif 0.7 <= t & t < 1 % Fourth condition, 0.7 <= t < 1
y2 = -3;
end
end
However, I wasn't able to run the following code:
t = 0:1/20000*pi:1;
a = x2(t);
As I got the following error message: Output argument "y2" (and possibly others) not assigned a value in the execution with "x2" function.
What exactly is causing the error and how should I implement this differently so that the function works?

采纳的回答

MJFcoNaN
MJFcoNaN 2022-4-6
Your function does not consider a vector as input.
Use for loop, such as:
a=NaN(length(t));
for ii = 1:length(t)
a(ii)=x2(t(ii));
end
or more efficiently modify your function by vectorization.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by