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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1030950/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1030955/image.png)
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)
0 件のコメント
採用された回答
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)
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Graphics Performance についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!