How can I define a variable Y as a function of another variable X, where the first function (Y) changes over a specific range of the dependent variable (X)?

Hello,
I am trying to define two variables (YTF and YAF) in ways that vary by the range of the variable 'AGE':
AGE = 20:1:65;
syms YTF if ge(50-AGE,0), YTF = 50-AGE;, else YTF = 0;, end
syms YAF if ge(65-AGE,15), YAF = 15;, else YAF = 65-AGE;, end
But when I do this, MatLab seems to recognize 'AGE' as the value '65'. The result is that 'YAF' is a '46x251 Double', but 'YTF' is a single value of 0.
I need MatLab to recognize neither YTF nor YAF as a single value, since I'll be using YAF and YTF in various equations.
Thanks for any advice.

Answers (1)

There’s no reason to use the Symbolic Math Toolbox for this. It is straightforward to use anonymous functions:
AGE = 20:1:65;
YTF = @(AGE) ((50-AGE) >= 0).*(50-AGE);
YAF = @(AGE) ((65-AGE) >= 15).*15 + ((65-AGE) < 15).*(65-AGE);
figure(1)
subplot(2,1,1)
plot(AGE, YTF(AGE), 'LineWidth',1.5)
ylabel('YTF')
grid
subplot(2,1,2)
plot(AGE, YAF(AGE), 'LineWidth',1.5)
ylabel('YAF')
xlabel('AGE')
grid

Categories

Find more on Mathematics in Help Center and File Exchange

Asked:

on 1 Aug 2015

Answered:

on 1 Aug 2015

Community Treasure Hunt

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

Start Hunting!