I am confused on why I am getting huge numbers at the values 0,75,150, and 300 in my code
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Matthew
el 26 de Sept. de 2023
Comentada: Matthew
el 27 de Sept. de 2023
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 comentarios
Respuesta aceptada
Walter Roberson
el 26 de Sept. de 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)
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!