plotting using if and for loop

1 次查看(过去 30 天)
Hello,
I should programming this formula and plot the picture (pic1) in below with these condiction (pic2) but when I run my code it didn't give me any curve and since I didn't get any I only write first formula in here.
it would be great if someone can help me.
my code ;
close all;
clc;
clear all;
x=0:0.01:pi/2;
n2=1.5;
n1=1;
n=n1/n2;
C = asin(n);
P = atan(n);
B= sqrt((sin(x)^2 - (n^2));
D = (n^2)*cos(x);
y1=180;
y2=0;
y3= 2*atan(B/D);
z=zeros(1,length(x));
for j=1:length(x);
if (x(j) < C);
z(j)=y1(j);
elseif (x(j)>C) && (x(j)<P);
z(j)=y2(j);
else
z(j)=y3(j);
end
end
i=x*180/pi;
plot(i,z)

采纳的回答

Ganesh
Ganesh 2022-6-13
The issue seems to be 3 things.
a) The elements aren't dot squared(line 10)
b) y1, and y2 are integers but are indexed(only arrays and other such data types can be indexed)
c) Inconsistency between radians and degrees(z hasnt been converted to degrees)
The below code fixes the issues in order to plot Theta(tm).
close all;
clc;
clear all;
x=0:0.01:pi/2;
n2=1.5;
n1=1;
n=n1/n2;
C=asin(n);
P=atan(n);
B=sqrt(sin(x).^2 - (n^2));
D=(n^2)*cos(x);
y1=pi;
y2=0;
z=zeros(1,length(x));
for j=1:length(x);
if (x(j) < P);
z(j)=y1;
elseif (x(j)>P) && (x(j)<C);
z(j)=y2;
else
z(j)=2*atan(real(B(j)/D(j)));
end
end
i=x*180/pi;
z=z*180/pi;
plot(i,z)
  1 个评论
mhya k
mhya k 2022-6-13
编辑:mhya k 2022-6-13
omg thank you it worked. really thank for your help.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by