I am confused on why I am getting huge numbers at the values 0,75,150, and 300 in my code
4 views (last 30 days)
Show older comments
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)
0 Comments
Accepted Answer
Walter Roberson
on 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)
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!