필터 지우기
필터 지우기

I am confused on why I am getting huge numbers at the values 0,75,150, and 300 in my code

조회 수: 3 (최근 30일)
For some reason, I am getting huge numbers in c for x= 0, 75, 150, and 300. Which is obviously not correct. I am not sure what is wrong. Here is my code:
x = [0:300];
lda = 300; % in km
k = ((2*pi)/(lda));
a = (cos(k*x));
b = (cos((k*x)-(pi/2)));
c = (a)./(b);
plot(x, c)

채택된 답변

Walter Roberson
Walter Roberson 2023년 9월 26일
Algebraically, b should be 0 at 0, 150, 300 (but not 75), and division by 0 gives infinity. In the below plot, you do not see the infinity because they are only single points and plot() automatically clips out infinite values.
Now, I said "Algebarically". But you are using finite double precision representations and the calculated values are not exact multiples of and numeric cos(pi/2) does not give exactly 1 because of finite precision reasons.
How can you avoid the problem? Well, you can use symbolic calculations.. or you could rewrite a little and use sinpi and cospi
Pi = sym(pi);
x = [0:300];
lda = 300; % in km
k = ((2*Pi)/(lda));
a = (cos(k*x));
b = (cos((k*x)-(Pi/2)));
c = (a)./(b);
plot(x, c)
temp = [a;b;c];
mask = ismember(x, [0, 75, 150, 300]);
temp(:,mask)
ans = 

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Model Building and Assessment에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by