Hello everyone,
I have made this code to solve system of equations so when t=0 x should be at target distance.
I shared it with my professor today and she said she thinks it's something to do with the indices...and I am unable to figure it out. The plot should end at the target distance x_t at y=0 and as you can see it hits the target just below y=0. Any help is greatly appriciated.
g = -32.2; % Gravatational constant
%% Please enter angle and target distance.
a = 45 ; % User input for angle
x_t = 4; % User input target distance
y_o = .5; % Initial y position at launch 6 in.
% System of equations to be solved
syms v t
eqn1 = x_t == v * cosd (a) *t;
eqn5 = 0 == y_o + v * sind (a) * t + 1/2 *g * t^2;
eqns =[eqn1,eqn5];
% Solve
sol = solve(eqns,v,t);
vS = vpa(sol.v)/1i^2;
tS = vpa(sol.t)/1i^2;
% Extracting only positive roots for subsequent equations
vpos = vS(vS>=0);
tpos = tS(tS>=0);
% Time step and matrices length
dt=.01;
t_i = 0:dt:tpos;
numt = length(t_i);
y = zeros(1,numt);
x = zeros(1,numt);
% Simulate the trajectory
vy = vpa(vpos * sind(a));
for i = 2 :numt
y(1)= y_o;
vy (i) = vy(i-1) + g * dt;
x (i) = x (i-1) + vpos * cosd(a) * dt;
y (i) = y (i-1) + vy(i) * dt + 1/2 * g * dt.^2;
end
%% Plotting the balls path
plot (x,y)
xlim ([0,12.5])
xticks ([0:1:12.5])
ylim auto
hold off
grid on
xlabel ('distance')
ylabel ('Height')
title Projectile Motion
tpos
vpos

 Respuesta aceptada

James Tursa
James Tursa el 11 de Feb. de 2021
Editada: James Tursa el 11 de Feb. de 2021

0 votos

You are doing a numeric itegration to generate the plotting points. Numeric integration will build up errors over time, so the fact that your plot doesn't end at 0 exactly is to be expected. You could decrease the step size of your integration, or use a higher order numeric integration scheme, or simply plug some t values into your x and y equations directly to generate the plotting points. E.g., try this and then zoom in on the end point:
t = linspace(0,tpos,1000);
x = vpos * cosd(a) * t;
y = y_o + vpos * sind(a) * t + 1/2 * g * t.^2;
figure
plot(x,y,'.-')
grid on

1 comentario

Susan Chestnutt
Susan Chestnutt el 11 de Feb. de 2021
Thank you, you are amazing!!!! And don't forget to smile, you deserve it

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Mathematics en Centro de ayuda y File Exchange.

Preguntada:

el 11 de Feb. de 2021

Comentada:

el 11 de Feb. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by