plots with if, elseif and else

1 view (last 30 days)
Gabrielle Gu
Gabrielle Gu on 29 Mar 2015
Edited: Jan on 30 Mar 2015
c2=1800;
c1=1500;
for ai=0:0.01:pi/2;
at=asin(sin(ai).*c2./c1);R=(c2./cos(at)-c1./cos(ai))./(c2./cos(at)+c1./cos(ai));
if ai<ac
phase=2.*atan(sqrt((cos(ac)./cos(ai)).^2-1));
R=(c2./cos(at)-c2./cos(ai))./(c2./cos(at)+c1./cos(ai));
elseif ai>=ac
phase=0;
R=1;
end
end
figure
plot(ai,R);
hold on
plot(ai,phase);
when I run the codes, nothing appears in the plots. and when I check the values of R and phase, and find that R=1 and phase=0. anyone can help me correct the codes? thank you very much
[EDITED, Jan, please use a proper code formatting - thanks]

Answers (1)

Star Strider
Star Strider on 29 Mar 2015
You didn’t supply a value for ‘ac’ so I made one up to be sure the code works.
Your code can (I believe) be reduced to a few lines:
ac = 2; % <= This Needs Your Definition!
c2=1800;
c1=1500;
ai=0:0.01:pi/2;
at=asin(sin(ai).*c2./c1);
R = @(ai) [((c2./cos(at)-c2./cos(ai))./(c2./cos(at)+c1./cos(ai))).*(ai<ac) + (1).*(ai>=ac)];
phase = @(ai) [(2.*atan(sqrt((cos(ac)./cos(ai)).^2-1))).*(ai<ac)];
figure(1)
plot(ai,R(ai));
hold on
plot(ai,phase(ai));
hold off
The anonymous functions for ‘R’ and ‘phase’ are simple vectors where the functions take the values you want to assign to them according to the logic tests you set for them.
The results are complex, so you will likely want to decide how to detail with that (plot imaginary parts separately for instance).
Does this do what you want?

Community Treasure Hunt

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

Start Hunting!