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

2 ビュー (過去 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by