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 CenterFile Exchange에서 Loops 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