how to have the final vector?

5 visualizaciones (últimos 30 días)
Lilya
Lilya el 29 de Mzo. de 2020
Comentada: Lilya el 29 de Mzo. de 2020
Hi all,
I've written the following script to calculate the wind direction from u and v components, taking into consideration the quadrants.
The problem is that I should have the final vector for the plot.
Where am I mistaken?
Note: U10 and V10 have a dimension of (1*120).
Thanks for the help.
for i = 1:length (U10);
if(V10 > 0)
kk = ((180 / pi) * atan(U10(:,i)/V10(:,i)) + 180);
elseif (U10 < 0 & V10 < 0) ;
kk = ((180 / pi) * atan(U10(:,i)/V10(:,i)) + 0);
elseif (U10 > 0 & V10 < 0);
kk = ((180 / pi) * atan(U10(:,i)/V10(:,i)) + 360);
end
end
  2 comentarios
David Hill
David Hill el 29 de Mzo. de 2020
You should look at the atan2() or atan2d() functions.
Lilya
Lilya el 29 de Mzo. de 2020
Will check them, thanks a lot

Iniciar sesión para comentar.

Respuesta aceptada

Daemonic
Daemonic el 29 de Mzo. de 2020
Is the purpose of your loop to compare the values of V10 and U10 element-wise? If so, you would need to modify slightly:
if V10(i) > 0 %compares the i'th element of V10 to 0
Secondly is kk the vector you're trying to create? If so, you'd need to make a similar adjustment (also, with a small vector it doesn't matter so much, but for larger ones, you'll speed things up if you pre-populate kk). Example:
kk = nan(size(U10)); %creates empty vector for kk
for i = 1:length (U10);
if(V10(i) > 0) %compares the i'th element of V10 to 0
kk(i) = ((180 / pi) * atan(U10(:,i)/V10(:,i)) + 180);
elseif (U10(i) < 0 & V10(i) < 0) ;
kk(i) = ((180 / pi) * atan(U10(:,i)/V10(:,i)) + 0);
elseif (U10(i) > 0 & V10(i) < 0);
kk(i) = ((180 / pi) * atan(U10(:,i)/V10(:,i)) + 360);
end
end
Sorry if I misunderstan your question...

Más respuestas (1)

Ameer Hamza
Ameer Hamza el 29 de Mzo. de 2020
Editada: Ameer Hamza el 29 de Mzo. de 2020
Use atan2, it takes care of the quadrants
result = atan2(U10, V10)
  1 comentario
Lilya
Lilya el 29 de Mzo. de 2020
much appreciated, will look at that now

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by