# Projectile Motion Equation with inputs of degree and first velocity

1 view (last 30 days)
damla y on 26 Feb 2022
Edited: AndresVar on 27 Feb 2022
I want to write a Matlab code about projectile motion equation for my project but after a while I am stuck.
I cant plot the V_x,t diagram and I want to change V_z equation after V_z = 0, when z has its max value, but I couldnt do this. Can you help me?
And its also debugging.
clc
clear all
v0 = 262.22 ; %m/s
g= 9.801 ; %m/s^2
theta_0 = 85 ; %derece
v0_x = v0*cosd(theta_0) ; %m/s
v0_z = -v0*sind(theta_0) ; %m/s
t = 0:0.01:80 ;
for i = 0: length(t)
v_x = v0_x ;
x = v0_x.*t ;
v_z = g.*t + v0_z ;
v_z(1) = v0_z ;
z = 0.5*g.*t.^2 + v0_z.*t ;
theta = atand( -v_z / v_x ) ;
theta(1) = theta_0 ;
if v_z == 0
v_z = g.*t;
end
figure(1)
plot (t, theta) ;
figure(2)
plot (t, x) ;
figure(3)
plot (t, -z) ;
figure(4)
plot (t, -v_z) ;
figure(5)
plot(t,v0_x) ;
figure(6)
plot(x,-z) ;
end
AndresVar on 27 Feb 2022
@damla y I am curious why you want to change the v_z funciton at the peak, something happens to make it change?

AndresVar on 27 Feb 2022
Edited: AndresVar on 27 Feb 2022
Here is an example
Note you don't have to loop for each time value because matlab can perform calculations on the vector
Note: i changed v0_z and t and z a little, but you can see how it works in general.
v0 = 262.22 ; %m/s
g = 9.801 ; %m/s^2
theta_0 = 85 ; %derece
x0 = 0;
z0 = 0;
v0_x = v0*cosd(theta_0) ; %m/s
v0_z = v0*sind(theta_0) ; %m/s
t = 0:0.1:60;
% velocity
v_x = v0_x*ones(size(t));
v_z = -g*t+v0_z;
% position
x = v0_x.*t+x0;
z = -0.5*g*t.^2+v0_z*t+z0;
figure;
tiledlayout(3,1)
nexttile;
plot(x,z)
xlabel('x'),ylabel('z');
nexttile;
plot(t,v_x);
xlabel('t'),ylabel('v_x');
nexttile;
plot(t,v_z);
xlabel('t'),ylabel('v_z');
% to change v_z at maximum height
% for example here i set it to constant 2 m/s
% then the location also changes z=2*t+z0...
[~,peakIdx]=min(abs(v_z));
t2 = t(peakIdx:end)-t(peakIdx);
v_z(peakIdx:end)=2;
z(peakIdx:end)=2*t2+z(peakIdx);
figure
tiledlayout(1,2)
nexttile;
plot(t,v_z)
xlabel('t');ylabel('v_z');
nexttile;
plot(x,z);
xlabel('x');ylabel('z');

R2019a

### Community Treasure Hunt

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

Start Hunting!