MATLAB Answers

2d projecti basic; It is not letting me plot x,y,t and the input was working now it only takes inital velocity

1 view (last 30 days)
Akshat Shrivastava
Akshat Shrivastava on 7 May 2021
Answered: Amrtanshu Raj on 11 May 2021
function [x,y,v]=projectile(vO,angle,time)
vO=input('enter initial velocity greater than 0')
angle=input('enter angle')
time=input('enter time ')
vOy= vO*sind(angle);
vOx= vO*cosd(angle);
ay= -9.81;
x=vOx*time;
y=vOy*time+0.5*ay*(time^2);
vx=vOx;
vy=sqrt(vx^2+2*ay*y);
v=sqrt(vx^2+vy^2);
if vO==0
msg='error initial velocity must be greater than 0'
error(msg)
else
end
x0=0;
y0=0;
k=0;
v=vO;
g=9.81;
t=0:0.1:time;
x=x0+v*cosd(angle)*t;
y=y0+v*sind(angle)*t-(g*t.^2)/2;
figure
hold on
plot(x,y)
xlabel('time[s]')
ylabel('position[m]')
title('postion vs time')
hold off

Answers (1)

Amrtanshu Raj
Amrtanshu Raj on 11 May 2021
Hi,
I have updated the code and it is working now.
You should also note that you are plotting y vs x and not position vs time. I have added all the plots as subplot. Also you can refine the code.
function [x,y,v]=projectile()
vO=input('enter initial velocity greater than 0 :');
angle=input('enter angle :');
time=input('enter time :');
if vO==0
msg='error initial velocity must be greater than 0';
error(msg)
end
vOy= vO*sind(angle);
vOx= vO*cosd(angle);
ay= -9.81;
x=vOx*time;
y=vOy*time+0.5*ay*(time^2);
vx=vOx;
vy=sqrt(vx^2+2*ay*y);
v=sqrt(vx^2+vy^2);
x0=0;
y0=0;
k=0;
v=vO;
g=9.81;
t=0:0.1:time;
x=x0+v*cosd(angle)*t;
y=y0+v*sind(angle)*t-(g*t.^2)/2;
subplot(3,1,1)
plot(x,y)
xlabel('Position_x[m]')
ylabel('Position_y[m]')
title('postion')
subplot(3,1,2)
plot(x,t)
xlabel('Time[s]')
ylabel('Position_x[m]')
title('Position_x vs Time')
subplot(3,1,3)
plot(x,y)
xlabel('Time[s]')
ylabel('Position_y[m]')
title('Position_y vs Time')
Hope this helps !!

Tags

Community Treasure Hunt

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

Start Hunting!