MATLAB: How to add an initial position above x-axis in the projectile motion function

projectile motion

I want some help in my Projectile motion function where i can launch my projectile from a platform above x-axis and Still hit the ground Thanks in advance.
function [range,maxh,xmaxh]= projectilemotion1(x0,y0,v0,angle,g)
% inputs
% x0 = Initial x value (starting distance)
% y0 = Initial y value (starting height)
% v0 = Initial velocity
% angle = angle of projectile
% g=gravitational acceleration
% outputs
% range = maximum distance
% maxh = max height
% xmaxh = x co-ordinate at the highest point
range = (v0.^2)./g * sind(2*angle);
xstep = range ./150;
x = x0:xstep:range;
y = (x*tand(angle) - g /(2*(v0.^2)*(cosd(angle)).^2)*x.^2)+y0;
for i=1:length(x)
xlabel('Distance (Meters)');
ylabel('Height (Meters)');
hold on
zoom on;
maxh = max(y)
xmaxh = (v0.^2)./(2*g)*sind(2*angle)
Time = sqrt(2*maxh)./g

Best Answer

  • This is more of a physics problem than a coding one.
    But your code already has the feature you describe in it. The constant y0 allows you to input a starting height above the ground.
    If you're problem is that the code stops when your projectile gets back to this height y0, instead of ground level, then the solution is still fairly simple. You need to work out the extra range added by your projectile falling beyond y0 back to 'ground level', then 'add' the extra distance into your calculations. The max height shouldn't change, just the range.