issue using indices...maybe?
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Mathematics 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!