create a Moving Box Simulation in 1D
Ältere Kommentare anzeigen
I am trying to create a simulation like this one:

And I have problems making the rectangular box moves, here is the function I wrote:
function ac_plot(~, marker, color, size)
plot(rectangle('Position',[0 0 0.2 0.2],'EdgeColor','b','LineWidth',2),...
rectangle('Position',[0 0 0.2 0.2],'EdgeColor','b','LineWidth',2),...
marker, 'Color', color, 'MarkerSize', size)
The input should be an Array of positions, I don't know how to plug it in the input of the function, and I still have to work on the function so that the box moves.
I would appreciate any help!
3 Kommentare
Jan
am 11 Mär. 2021
The question is not clear yet. You show some code, but do not explain, what you want to do in the next step. Why is the first input of your function ignored?
rectangle is a matlab command, which draws a rectangle. You cannot insert in inside a plot command. What do you want to achieve?
dez
am 14 Nov. 2022
Do you know what the source of the gif is? I want to know the equations that were used to describe the motion.
Akzeptierte Antwort
Weitere Antworten (1)
% Set up initial conditions
x = [0 10]; % Box starts at position 0, ends at position 10
v = 1; % Box moves at a constant velocity of 1 unit per timestep
dt = 0.1; % Timestep size
t = 0; % Start time
% Set up plot
figure;
xlim([-5 15]);
ylim([-1 1]);
xlabel('Position');
title('Moving Box Simulation');
% Main loop
while x(2) < 15 % Keep moving until the box reaches position 15
% Update position
x = x + v*dt;
% Clear plot and draw box
clf;
plot(x, [0 0], 'k-', 'LineWidth', 2);
drawnow;
pause(0.01);
% Update time
t = t + dt;
end
Kategorien
Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

