How to use IF and ELSE commands for an array

1 view (last 30 days)
I want to plot and Length (L) vs Temperature (T) L is defined by an array L= [0:100:2300] T is defined by T = Tb-g*L*cinx g is constant=0.026 Tb is constant =143 x is an angle that I want to be 140 degrees for L<1300 and 90 degrees for L>=1300
I have tried
Tb=143.2;
T=Tb-(g*L*sind(x));
L=[0:100:2300];
if L > 1300
T=Tb-(g*L*sind(x));
else
L <= 1300;
T=Tb-(g*L*sind(x));
end
I have tried but no success yet!! I need to make a MATLAB code. Any help will be appreciated.
  2 Comments
Ryan
Ryan on 12 Aug 2014
Tb=143.2;
L = 0:100:2300
T = zeros(1,length(L))
for i = 1:1:length(L)
if L(i) >= 1300
T(i) = Tb-(g*L*sind(90));
else
T(i) = Tb-(g*L*sind(140));
end
end
Andrew Andrew
Andrew Andrew on 12 Aug 2014
Thanks Ryan, though i am getting an error in the Line with T(i) = Tb-(g*L*sind(140));

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 12 Aug 2014
Edited: Star Strider on 12 Aug 2014
You can use the logic that for (L < 1300), the logical value of (L < 1300)==1, and for (L >= 1300) the logical value of (L < 1300)==0. The same goes for for (L >= 1300).
Combining them in the argument for sind and using an anonymous function for your expression:
g = 0.026;
Tb=143.2;
L=[0:100:2300];
T = @(L) Tb-(g.*L.*sind(140*(L<1300) + 90*(L>=1300)));
Tr = T(L);
figure(1)
plot(L, Tr)
grid
Does this give you the result you want?
  2 Comments
Andrew Andrew
Andrew Andrew on 12 Aug 2014
Edited: Andrew Andrew on 12 Aug 2014
Star Strider,
Yes it does.....woow this is good! Thanks very much.
I guess because i have only used MATLAB on this project i am working on, i simply didn't get a proper understanding of how you arrived at that, even after several hours of trying to scrutinize it . I had wanted to incorporate the same idea in another equation but i can't even figure out how to start.
Tr and x(140 and 90) are also variables in another equation below which for which x is still 140 degrees for L<1300 and 90 degrees for L>=1300. The equation is
Tf=Tr-((a*sind(x))/(b*J*cp3*A1))+(Fc/A1)+((g*sind(x))/A1)+exp(-1*A1*(L-Lin))*(Tfin-Tein+(a*sind(x))/(b*J*cp3*A1)-(Fc/A1)-(g*sind(x)/A1))
a,b,J,cp3,A1,Fc,A1,g,Lin, Tfin and Tein are all constants in this equation. My final plot needs to have Tr & Tf against L
I will really appreciate if you can help me on this last one.
Star Strider
Star Strider on 12 Aug 2014
My pleasure!
To do the same thing for x for all your equations:
1. To calculate your x vector, create it as:
x = 140*(L<1300) + 90*(L>=1300);
2. Your equations should pick up x and your other variables from your workspace. I ‘vectorized’ your Tf equation, so it should not cause problems with array-valued variables (notice the ‘.*’ in place of ‘*’ and ‘./’ in place of ‘/’). (Your other variables are scalars, so you should have no problems. Your Tf and Tr variables will therefore be vectors the same size as L.)
Tf=Tr-((a.*sind(x))./(b.*J.*cp3.*A1))+(Fc./A1)+((g.*sind(x))./A1)+exp(-1*A1.*(L-Lin)).*(Tfin-Tein+(a.*sind(x))./(b.*J.*cp3.*A1)-(Fc./A1)-(g.*sind(x)./A1));
The logic here benefits from having two relatively uncomplicated inequalities. Consider your vector L. For all values of L less than 1300, the condition (L < 1300) evaluates as 'true' or numerically 1. For every value of (L >= 1300), (L < 1300) evaluates as 'false' or numerically as 0. Multiplying (L < 1300) by 140 creates a vector of values of 140 for all values of (L < 1300).
The same logic applies to the other condition, so you end up with a vector of values for x that has appropriate values for both conditions. Another experiment you can do to illustrate how this logic works is:
q1 = L < 1300;
q2 = L >= 1300;
Then look at q1 and q2. If you then multiply q1 by 140, q2 by 90 and add them, you can create x:
x = 140*q1 + 90*q2;
When you have time, experiment with various MATLAB logic constructions. See the documentation for Special Characters, and specifically explore the links to ‘Logical Operations’, ‘Relational Operations’ and ‘Array vs. Matrix Operations’ near the end of that page for more information.
We’re here to help!

Sign in to comment.

More Answers (0)

Categories

Find more on Multidimensional Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!