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

2 vues (au cours des 30 derniers jours)
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?

Réponse acceptée

MJFcoNaN
MJFcoNaN le 6 Avr 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.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by