Filter löschen
Filter löschen

How to add three if else conditions?

47 Ansichten (letzte 30 Tage)
Shantanu K
Shantanu K am 12 Mär. 2013
Here one if else loop is used. there i have used three conditions i want to use and operator.....means if all conditions are satisfied then it should display given function !
Sigma_x=-500;
Sigma_y=-500;
Sigma_xy=00;
theta=0;
o=(pi)*theta/180;
T=[cos(o) -sin(o); -sin(o) cos(0)];
Q= T*[Sigma_x Sigma_xy; Sigma_xy Sigma_y]/(T');
Sigma1=Q(1,1);
Sigma12=Q(1,2);
Sigma2=Q(2,2);
S1t=1300;
S1c=-700;
S2t=1200;
S2c=-500;
S6=600;
S1=(S1c:S1t);
S2=(S2c:S2t);
plot(S1t,S2);
hold on;
plot(S1c,S2);
hold on;
plot(S1,S2t);
hold on;
plot(S1,S2c);
plot(Sigma1,Sigma2,'*')
axis([-800 1600 -800 1600])
xlabel('Sigma1')
ylabel('Sigma2')
title('Maximum Stress Theory')
if (S1c<Sigma1<S1t);
and
(S2c<Sigma2<S2t);
and
(0<Sigma12<S6);
display('Its safe zone');
else
display('Material has yielded');
end
%end

Akzeptierte Antwort

Friedrich
Friedrich am 12 Mär. 2013
Bearbeitet: Friedrich am 12 Mär. 2013
Hi,
do it like this
if ((S1c<Sigma1) && (Sigma1<S1t) && (S2c<Sigma2) && (Sigma2<S2t) && (0<Sigma12) && (Sigma12<S6))
display('Its safe zone');
else
display('Material has yielded');
end
You have to split up things like a<b<c into a<b && b<c otherwise you can get unwanted behavior, e.g.
>> 1<2<3 %yay it works
ans =
1
>> 1<pi<3 %oh wait it doesnt!
ans =
1
This happens because of the way MATLAB parses such an expression. It parses from left to right, so 1<pi is true, so 1. Then 1 < 3 is also true so you get true at the end. But thats not want you want

Weitere Antworten (0)

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by