Projectile Motion

Projectile Motion

Solution 

%input theta from users

theta=-1;

while(theta<0 || theta>90)

theta = inputdlg(‘Enter an angle within 0 and 90 degrees’,’input’,1);

theta = str2double(cell2mat(theta));

end

%input initial velocity from users

v=-1;

while(v<=0)

v = inputdlg(‘Enter an initial airspeed’,’input’,1);

v = str2double(cell2mat(v));

end

%input associated unit for velocity from users

d1=-1;

while(d1<=0)

unitv = inputdlg(‘Enter associated unit with velocity (km/h, m/s, mph or ft/s)’,’input’,1);

unitv = char(cell2mat(unitv));

if(strcmp(‘km/h’,unitv))

v = (v*5)/18;d1=1; %converting km/h to m/s

elseif (strcmp(‘m/s’,unitv))

d1=1;

elseif (strcmp(‘mph’,unitv))

v = v/60;d1=1; %converting mph to m/s

elseif (strcmp(‘km/h’,unitv))

v = v*0.3048;d1=1; %converting ft/s to m/s

end

end

%input time vector from users

time = inputdlg(‘Enter time vector (seperated by space)’,’input’);

time = str2num(cell2mat(time));

%input associated unit for time from users

unitt = inputdlg(‘Enter associated unit of time (s or h)’,’input’,1);

unitt = char(cell2mat(unitt));

d1=-1;

while(d1<=0)

if(strcmp(‘s’,unitt))

d1=1;

% converting hours into sec

elseif(strcmp(‘h’,unitt))

time = time.*60;d1=1;

end

end

%calculating horizontal and vertical displacements

x = v.*cos((theta*pi)/180)*time;

y = (0.5.*-9.8*power(time,2)) + v.*sin((theta*pi)/180)*time;

% finding min and max vertical displacements

maxdisp = max(y);

mindisp = min(y);

% input unit for displacements from users

unit = inputdlg(‘Enter the unit for ans (kilometer or meter or mile or foot)’,’output unit’,1);

unit = char(cell2mat(unit));

d1=-1;

while(d1<=0)

if(strcmp(unit,’kilometer’))

% converting meter to kilometer

maxdisp = maxdisp/1000;

mindisp = mindisp/1000;

x = x.*0.001;

d1=1;

elseif (strcmp(unit,’meter’))

d1=1;

elseif (strcmp(unit,’mile’))

%converting meter to mile

maxdisp = maxdisp*0.000621371;

mindisp = mindisp*0.000621371;

x = x.*0.000621371;

d1=1;

elseif (strcmp(unit,’foot’))

%converting meter to foot

maxdisp = maxdisp*3.28084;

mindisp = mindisp*3.28084;

x = x.*3.28084;

d1=1;

end

end

%printing the output  on command window

disp(sprintf(‘In y direction \n\t Max dislacement = %f \n\t Min displacement = %f’,maxdisp,mindisp));

disp(sprintf(‘Displacement in x direction at time instances – \n\t%s’,num2str(x)));