Error defining a discontinous function: "Output argument "y2" (and possibly others) not assigned a value in the execution with "x2" function
2 views (last 30 days)
Show older comments
Tomás Rodríguez Yagui
on 6 Apr 2022
Commented: Tomás Rodríguez Yagui
on 6 Apr 2022
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?
0 Comments
Accepted Answer
MJFcoNaN
on 6 Apr 2022
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.
2 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!