Hi Felix,
The error "Value must be a handle" suggests that the handle used in the addpoints function is incorrect. Ensure that h is a valid handle to the graph object. To resolve this issue and improve the animation of the trajectory with a marker, I made some adjustments to the code.
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 % 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
Feel free to customize the above code based on your preferences. Now, you should be able to figure out to resolve your problem.
Please see attached code snippet with plot.