Projecting circular motion on axes to derive linear accelerations

7 visualizaciones (últimos 30 días)
Good morning, everyone.
I am trying to create a .csv file that takes linear accelerations on the x and y axes, to recreate a circular motion as in the figure.
I have created the script to trace these values, and was wondering if I might be missing something.
Thanks in advance, i hope i haven't violated any forum behaviour, in case, i apologise, it's my first time posting.
% Parametri
r1 = 0.126; % raggio della prima circonferenza (m)
r2 = 0.042; % raggio della seconda circonferenza (m)
omega1 = 1.598; % velocità angolare prima circonferenza (rad/s)
omega2 = 4.794; % velocità angolare seconda circonferenza (rad/s)
dt = 0.0035; % intervallo di tempo (s)
t_total = 2.625; % tempo totale (s)
% Numero totale di punti
N = t_total / dt;
% Preallocazione di variabili
t = (0:N-1)' * dt;
x = zeros(N, 1);
y = zeros(N, 1);
ax = zeros(N, 1);
ay = zeros(N, 1);
% Moto sulla prima circonferenza
t1 = 0:dt:(pi/omega1);
x(1:length(t1)) = -r1 * cos(omega1 * t1);
y(1:length(t1)) = r1 * sin(omega1 * t1);
ax(1:length(t1)) = -r1 * (omega1^2) * cos(omega1 * t1);
ay(1:length(t1)) = -r1 * (omega1^2) * sin(omega1 * t1);
% Moto sulla seconda circonferenza
% Calcolare il punto di partenza sulla seconda circonferenza (tangente alla prima)
x_shift = r1; % punto di partenza x sulla seconda circonferenza (tangente alla prima)
y_shift = 0; % punto di partenza y sulla seconda circonferenza
t2 = (pi/omega1):dt:t_total;
x(length(t1)+1:length(t1)+length(t2)) = (x_shift + r2) - r2 * cos(omega2 * (t2 - pi/omega1));
y(length(t1)+1:length(t1)+length(t2)) = -(y_shift + r2 * sin(omega2 * (t2 - pi/omega1)));
ax(length(t1)+1:length(t1)+length(t2)) = -r2 * (omega2^2) * cos(omega2 * (t2 - pi/omega1));
ay(length(t1)+1:length(t1)+length(t2)) = r2 * (omega2^2) * sin(omega2 * (t2 - pi/omega1));
  1 comentario
Aniket
Aniket el 7 de Ag. de 2024
Your script looks well-structured and seems to address the problem of generating a circular motion by calculating the linear accelerations on the x and y axes. But ensure lengths and indices match correctly when transitioning from first circle to second one. Some points: 1. Use floor to ensure N is an integer. 2. Make sure the starting point for t2 is correctly set to follow immediately after t1.

Iniciar sesión para comentar.

Respuesta aceptada

Umar
Umar el 8 de Ag. de 2024
Hi @ Matteo GIUFFREDI,
I agree with @Aniket,the provided script seems to be on the right track. My only suggestions for improvement for your script would be data output and visualization. For data output, I would suggest creating a matrix with columns for time, x, y, ax, and ay, and then write this matrix to a .csv file. Then, To export the data to a .csv file, you can use the writematrix function in MATLAB as shown below
data = [t, x, y, ax, ay];
writematrix(data, 'circular_motion_data.csv');
Now addressing visualization part, I will consider plotting the circular motion using the plot function to visualize the trajectory and accelerations. As an example,
figure;
subplot(2, 1, 1);
plot(x, y);
title('Circular Motion Trajectory');
xlabel('x');
ylabel('y');
subplot(2, 1, 2);
plot(t, ax, 'r', t, ay, 'b');
title('Linear Accelerations');
xlabel('Time');
ylabel('Acceleration');
legend('ax', 'ay');
By incorporating these suggestions, you can enhance the visualization and data output of your circular motion simulation. Feel free to adjust the code according to your specific requirements and desired output format. If you have any further questions or need additional assistance, please don't hesitate to ask. Happy coding!

Más respuestas (1)

Aniket
Aniket el 8 de Ag. de 2024
I see you're trying to simulate and visualize the motion of a point moving along two circular paths with different radii and angular velocities. But when plotting the same, there is some issue that can be resolved by making changes to the dimensions of the arrays.
To get the desired results, make the following changes in your code
1. Ensure the lengths of the arrays match up correctly.
2. Export the data to a CSV file.
Below is the final, corrected script. It should work without any dimension mismatches and generate the plots required.
% Parametri
r1 = 0.126; % raggio della prima circonferenza (m)
r2 = 0.042; % raggio della seconda circonferenza (m)
omega1 = 1.598; % velocità angolare prima circonferenza (rad/s)
omega2 = 4.794; % velocità angolare seconda circonferenza (rad/s)
dt = 0.0035; % intervallo di tempo (s)
t_total = 2.625; % tempo totale (s)
% Numero totale di punti
N = floor(t_total / dt);
% Preallocazione di variabili
t = (0:N-1)' * dt;
x = zeros(N, 1);
y = zeros(N, 1);
ax = zeros(N, 1);
ay = zeros(N, 1);
% Moto sulla prima circonferenza
t1_end = pi / omega1;
t1 = 0:dt:t1_end;
len_t1 = length(t1);
x(1:len_t1) = -r1 * cos(omega1 * t1);
y(1:len_t1) = r1 * sin(omega1 * t1);
ax(1:len_t1) = -r1 * (omega1^2) * cos(omega1 * t1);
ay(1:len_t1) = -r1 * (omega1^2) * sin(omega1 * t1);
% Moto sulla seconda circonferenza
% Calcolare il punto di partenza sulla seconda circonferenza (tangente alla prima)
x_shift = r1; % punto di partenza x sulla seconda circonferenza (tangente alla prima)
y_shift = 0; % punto di partenza y sulla seconda circonferenza
t2_start = t1_end + dt;
t2 = t2_start:dt:t_total;
len_t2 = length(t2);
x(len_t1+1:len_t1+len_t2) = (x_shift + r2) - r2 * cos(omega2 * (t2 - t1_end));
y(len_t1+1:len_t1+len_t2) = -(y_shift + r2 * sin(omega2 * (t2 - t1_end)));
ax(len_t1+1:len_t1+len_t2) = -r2 * (omega2^2) * cos(omega2 * (t2 - t1_end));
ay(len_t1+1:len_t1+len_t2) = r2 * (omega2^2) * sin(omega2 * (t2 - t1_end));
% Ensure the lengths match up
if len_t1 + len_t2 ~= N
error('The lengths of the time segments do not sum up to the total number of points.');
end
% Esportare i dati in un file CSV
data = [t, x, y, ax, ay];
csvwrite('circular_motion.csv', data);
% Plotting the results
figure;
% Plotting x and y positions
subplot(2, 1, 1);
plot(x, y);
title('Circular Motion');
xlabel('x (m)');
ylabel('y (m)');
axis equal;
grid on;
% Plotting accelerations
subplot(2, 1, 2);
plot(t, ax, 'r', t, ay, 'b');
title('Accelerations');
xlabel('Time (s)');
ylabel('Acceleration (m/s^2)');
legend('ax', 'ay');
grid on;
This should give the correct motion and acceleration plots.
Hope this helps!
  2 comentarios
Matteo GIUFFREDI
Matteo GIUFFREDI el 8 de Ag. de 2024
First of all, thank you so much @Aniket, you have been very helpful, the entire code runs.
But i can't understand why i don't see the graphs, the program doesn't plot them; maybe because my Matlab version is 2022?
Matteo GIUFFREDI
Matteo GIUFFREDI el 8 de Ag. de 2024
@Aniket now i see all the information in output, i don't know why before i can't and now yes.
Thank you so much for the help, have a nice day.

Iniciar sesión para comentar.

Categorías

Más información sobre Graphics Object Properties 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