Projecting circular motion on axes to derive linear accelerations
Show older comments
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 Comment
Aniket
on 7 Aug 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.

Accepted Answer
More Answers (1)
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!
Categories
Find more on MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!