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

9 vues (au cours des 30 derniers jours)
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)

Réponse acceptée

Walter Roberson
Walter Roberson le 26 Sep 2023
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 = 

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by