Borrar filtros
Borrar filtros

Error with addpoints when trying to animate a trajectory following a marker

58 visualizaciones (últimos 30 días)
Felix Oliver
Felix Oliver el 20 de Jul. de 2024 a las 16:27
Comentada: Umar el 21 de Jul. de 2024 a las 6:44
I am trying to animate a trajectory following a marker.
Here is the problematic part of my code:
figure(1)
axis([0, ceil(max(x)), 0, ceil(max(y))])
daspect([1 1 1])
grid on
xlabel('x (m)'), ylabel('y (m)'), title('Bouncing Projectile'), subtitle(['{\theta} = ', num2str(theta), '°, u = ', num2str(u), ' ms^{-1}, h = ', num2str(h), ' m, g = ', num2str(g), ' ms^{-2}, C = ', num2str(C), ', N = ', num2str(N)])
h = animatedline('Color', 'b');
p = plot(x(1), y(1), 'o', 'MarkerEdgeColor', 'red', 'MarkerFaceColor', 'red');
for k = 1:length(x)
p.XData = x(k);
p.YData = y(k);
addpoints(h, x(k), y(k));
drawnow limitrate
pause(dt)
end
Whithout the code for the marker, the program runs well. However, for some strange reason, adding the code for the marker seems to create an error:
Error using matlab.graphics.animation.AnimatedLine/addpoints
Value must be a handle.
Error in file (line 54)
addpoints(h, x(k), y(k));
A graph is produced when I run the program, however the axes are not labeled, there is no title, and only one point is plotted.
What mistake have I made?
  8 comentarios
Umar
Umar el 20 de Jul. de 2024 a las 23:46
@Walter,
Could you please do me a favor, the code that you edited above, could you please execute this code on your mobile and provide a screenshot of the plot to see what results are you getting.
Walter Roberson
Walter Roberson el 21 de Jul. de 2024 a las 4:10
% Define initial parameters
theta = 45; % Launch angle in degrees
u = 10; % Initial velocity in m/s
h = 0; % Initial height in meters
g = 9.81; % Acceleration due to gravity in m/s^2
C = 0; % Air resistance constant
N = 100; % Number of time steps
dt = 0.1; % Time step
% Calculate trajectory
[x, y] = calculate_trajectory(theta, u, h, g, C, N, dt);
% Add red marker at the starting point
p = plot(x(1), y(1), 'o', 'MarkerEdgeColor', 'red', 'MarkerFaceColor', 'red');
% Initialize the animated line
h = animatedline('Color', 'b');
addpoints(h, x(1), y(1));
for k = 2:length(x)
% Update the position of the red marker
p.XData = x(k);
p.YData = y(k);
% Add points to the animated line for the trajectory
addpoints(h, x(k), y(k));
drawnow limitrate
pause(dt)
end
% Plot the bouncing projectile trajectory at the end of animation
h = animatedline('Color', 'b', 'MaximumNumPoints', N);
addpoints(h, x(k), y(k));
drawnow limitrate
function [x, y] = calculate_trajectory(theta, u, h, g, C, N, dt)
% Initialize arrays to store x and y coordinates
x = zeros(1, N);
y = zeros(1, N);
% Convert launch angle from degrees to radians
theta = deg2rad(theta);
% Calculate the trajectory using projectile motion equations
for i = 1:N
x(i) = u * cos(theta) * i * dt;
y(i) = h + u * sin(theta) * i * dt - 0.5 * g * (i * dt)^2;
% Check for ground collision and update y position accordingly
if y(i) < 0
y(i) = 0;
break;
end
end
end

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 20 de Jul. de 2024 a las 20:51
h = animatedline('Color', 'b');
p = plot(x(1), y(1), 'o', 'MarkerEdgeColor', 'red', 'MarkerFaceColor', 'red');
You do not have hold turned on. The call to plot() is a "high level" plotting call. "high level" plotting calls erase the current axes unless "hold" is turned on.
  2 comentarios
Umar
Umar el 21 de Jul. de 2024 a las 6:44
Hi Felix,
Walter and I answered your question. Do you have any further questions for us, please let us know. Also, please give a vote to Walter to go above and beyond. He is one of the most respected person on Mathworks platform.

Iniciar sesión para comentar.

Categorías

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

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by