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!