How to reduce angle values in a range between 0 and 360 deg?

57 visualizaciones (últimos 30 días)
Hi everyone! I have obtained the angles alpha, beta and gamma in deg. However I would like to make a plot with values of gamma in a range between 0 and 360 deg (a sort of step-plot). Can anyone help me?
%B = load('PosRel.txt');
B = readmatrix('PosRelORF.csv');
time = B(:,1);
num_times = size(time,1);
%assegno valori iniziali degli angoli di Eulero
alpha0 = convang(5,'deg','rad');
beta0 = convang(5,'deg','rad');
gamma0 = convang(5,'deg','rad');
vAlphaBetaGamma0 = [alpha0, beta0, gamma0];
[vTime, vAlphaBetaGamma] = ode45(@funzione,time,vAlphaBetaGamma0);
alpha = vAlphaBetaGamma(:,1);
beta = vAlphaBetaGamma(:,2);
gamma = vAlphaBetaGamma(:,3);
alpha = convang(alpha,'rad','deg');
beta = convang(beta,'rad','deg');
gamma = convang (gamma,'rad','deg');
figure
plot(vTime,alpha,'k',vTime,beta,'r', vTime,gamma,'b');
legend('alpha','beta','gamma');
xlabel('Time (seconds');
ylabel('Angles (deg)');
function angularvelocities=funzione(t,attitudeangles)
angularvelocities = [0;0;0.4]; %rad/sec
end

Respuesta aceptada

Star Strider
Star Strider el 30 de Oct. de 2021
I am not certain what the desired result is.
Something like this came up once before, and since wrap2pi and wrap2360 are available here (I do not have the Mapping Toolbox), I wrote a function to emulate both, comparing it to those functions while experimenting with the results here. (Replace ‘360’ with ‘2*pi’ and ‘180’ wiith ‘pi’ to get the wrap2pi equivalent, or other values to get different behaviour.)
See if this produces the desired result —
a = linspace(-720, 720);
figure
plot(a, wrap_180(a))
grid
figure
plot(a, wrap_360(a))
grid
function wP1 = wrap_180(P1) % EMulates 'wrap2180'
mf = mod(P1,360);
wP1 = mf.*(mf<=180) + (mf-360).*(mf>180);
end
function wP1 = wrap_360(P1) % EMulates 'wrap2360'
mf = mod(P1,720);
wP1 = mf.*(mf<=360) + (mf-720).*(mf>360);
end
.
  2 comentarios
Loren99
Loren99 el 30 de Oct. de 2021
@Star Striderthe problem is that I have an angular velocity of 0.4 rad/s, it means 0.0637 revolutions per seconds. Multiplying 0.0637 for my interval of times (600 seconds) it means that in total I should have 38 full revolutions (from 0 to 360 degrees). Using wrap_360 and watching your plot, it seems that I have only two full revolutions(from 0 to 360 degrees)
Star Strider
Star Strider el 30 de Oct. de 2021
My calculations do not agree with yours.
I get 19 revolutions, not 38
av = 0.4; % Angular Velocity (rad/s)
tvl = 600; % Record Length (s)
rrevs = av*tvl % Revolutions (rad)
rrevs = 240
drevs = string(rad2deg(rrevs)) % Revolutions (°) ('string' Call To Show PRecision)
drevs = "13750.9871"
tv = linspace(0, tvl, tvl); % Time Vector (s)
tot_revs = rad2deg(av) * tv; % Total Revolutions In 'tvl'
% endrevs = tot_revs(end)
Nr_revs = nnz(islocalmax(wrap_360(tot_revs))) % Total Number Of Revolutions (Counts Peaks)
Nr_revs = 19
figure
subplot(2,1,1)
plot(tv, tot_revs)
grid
xlabel('Time (s)')
ylabel('Degrees')
subplot(2,1,2)
plot(tv, wrap_360(tot_revs))
grid
xlabel('Time (s)')
ylabel('Degrees')
function wP1 = wrap_360(P1) % Emulates 'wrapTo360'
mf = mod(P1,720);
wP1 = mf.*(mf<=360) + (mf-720).*(mf>360);
end
I cannot test my code with the actual data without the actual data.
.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 29 de Oct. de 2021
mask = 0 <= gamma & gamma <= 360;
plot(vTime(mask), alpha(mask), 'k', vTime(mask), beta, 'r', vTime(mask), gamma(mask), 'b');
  1 comentario
Loren99
Loren99 el 30 de Oct. de 2021
@Walter Roberson I applied your suggestion, but that's not what I want to achieve. I would like to get a "zig zag plot", that is, when I get an angle value of 360 degrees (after one revolution), the plot goes back to zero and then starts growing again. 14000 degrees in the plot equals 23 revolutions per second.

Iniciar sesión para comentar.

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by