MATLAB Answers

How to set y to different functions depending on the limits of T

16 views (last 30 days)
Cai Beacham
Cai Beacham on 2 Dec 2020
Edited: Alan Stevens on 2 Dec 2020
So I'm trying to program this into matlab:
where ai is 0.5 and a0 is 0.3. I have tried using this code for it but I'm having difficulty with the (-10 <= T && T <= 10) part. Many Thanks!
T = -20:0.5:20;
ai = 0.5;
a0 = 0.3;
if T <= -10
y = ai
end
if (-10 <= T && T <= 10)
y = ai + (a0 - ai)*((T+10)/20)
end
if 10 <= T
y = a0
end
plot(T, y, 'bo-');
grid on;

  0 Comments

Sign in to comment.

Accepted Answer

Alan Stevens
Alan Stevens on 2 Dec 2020
Edited: Alan Stevens on 2 Dec 2020
Might be easier as
T = -20:0.5:20;
ai = 0.5;
a0 = 0.3;
y = ai + (a0 - ai)*((T+10)/20);
id = find(T<=-10);
y(id) = ai;
id = find(10<=T);
y(id) = a0;
plot(T, y, 'bo-');
grid on;

  0 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by