Find initial and fine time values for motion

22 views (last 30 days)
Ken Cheema
Ken Cheema on 31 Mar 2015
Commented: Chad Greene on 31 Mar 2015
Neglecting air resistance I want to find the initial time and final time for when the height of the ball is greater than or equal to 6 and the velocity is equal to or less than 16. The ball is launched at a 40 degree angle with an initial velocity of 20m/s and gravity is defined as 9.81 m/s. I got the times for which the conditions are met (.841 to 1.78 seconds), is there a way just to see the first and final time?
clear;
g = 9.81;
v_0= 20;
theta = 40;
vx_0 = v_0*cosd(theta);
vy_0 = v_0*sind(theta);
x0 = 0;
y0 = 0;
T = roots ([-0.5*g vy_0 0]);
T = T(2,1);
t = [0:.01:T];
for t = [0:.001:T] vx(1) = vx_0; vy(1) = vy_0 -g*(t); y(1) = y0 + vy_0*(t) - (0.5)*g*(t)^2; v(1) = sqrt(vx.^2 + vy.^2); if y >= 6 & v<=16 ; vx(1) = vx_0; vy(1) = vy_0 -g*(1); y(1) = y0 + vy_0*(t) - (0.5)*g*(t)^2; v(1) = sqrt(vx.^2 + vy.^2); disp(t) else
t = t + 1;
end
end

Answers (2)

Chad Greene
Chad Greene on 31 Mar 2015
Edited: Chad Greene on 31 Mar 2015
It looks like
t(find(v>=16,1,'first'))
should be the first time the ball is greater than or equal to 16.
A tip: instead of using sqrt(vx.^2 + vy.^2), you can use hypot(vx,vy), which is less cluttered and leaves less room for error.
  2 Comments
Ken Cheema
Ken Cheema on 31 Mar 2015
Thank you for the quick response, I still get all the values from .8410 to 1.78 with t(find(v>=16,1,'first')) in my code. Is it possible to store the values into a single vector and define t_initial and t_final from there?
Chad Greene
Chad Greene on 31 Mar 2015
Whoa, I just took a longer look at your code. There are a number of issues I'm going to try and clear up for you. May take a few minutes.

Sign in to comment.


Chad Greene
Chad Greene on 31 Mar 2015
Edited: Chad Greene on 31 Mar 2015
Some major issues:
1. The first time t is declared, you then immediately overwrite it when you say for t = [0:.001:T]. Then you've got an outlandish t = t+1 in that for loop! Doh! Were you intending to continue to the next t?
2. Vectors never get populated in your loop. It just keeps overwriting the first value in vx every time it says vx(1), and then you keep overwriting vy(1) and so on.
Minor issues:
  • Your calculations could be done most efficiently without any loops at all if you use the diff function.
  • The diff function aside, there's really no reason to use loops for most of your variables. vx never changes, and v and y can be calculated at the end of the loop, after vy is populated.
I usually don't do folks' homework for them, but you showed real effort, and you already have the answers, so why the heck not. Run this:
% Set initial conditions:
v_0= 20;
theta = 40;
vx_0 = v_0*cosd(theta);
vy_0 = v_0*sind(theta);
x0 = 0;
y0 = 0;
% constants:
g = 9.81;
T = roots ([-0.5*g vy_0 0]);
T = T(2,1);
t = 0:.001:T;
% preallocate array to make computation faster:
vy = NaN(size(t));
% Set initial values in array:
vy(1) = vy_0 -g*(t(1));
for n = 2:length(t)
delta_t = t(n)-t(n-1);
vy(n) = vy(n-1) - g*delta_t;
end
y = y0 + vy_0*t - 0.5*g*t.^2;
v = hypot(vx_0,vy);
subplot(2,1,1)
plot(t,y,'b')
hold on
plot([t(1) t(end)],[6 6],'r')
box off
axis tight
xlabel('time (s)')
ylabel('height (m)')
subplot(2,1,2)
plot(t,vx_0*ones(size(t)),'b')
hold on
plot(t,vy,'r')
plot(t,v,'k','linewidth',3)
legend('v_x','v_y','v','location','southwest')
box off
axis tight
xlabel('time (s)')
ylabel('velocity (m/s)')
firsttime = t(find(y>=6 & v<=16,1,'first'))
lasttime = t(find(y>=6 & v<=16,1,'last'))
subplot(2,1,1)
plot([firsttime firsttime],[min(y) max(y)],'k')
text(firsttime,5,' the first time!')
plot([lasttime lasttime],[min(y) max(y)],'k')
text(lasttime,2,' the {\it last} time!')
allconditionsmet = y>=6 & v<=16;
plot(t(allconditionsmet),y(allconditionsmet),'mo')
  2 Comments
Ken Cheema
Ken Cheema on 31 Mar 2015
Thank you so much! Really simplifies the task, plot was amazing.
Chad Greene
Chad Greene on 31 Mar 2015
There's a lot of new stuff there. Try to go through it line by line and understand what's going on. Let me know if you have questions--learning these concepts now will really pay off.

Sign in to comment.

Categories

Find more on Get Started with MATLAB 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!