MATLAB Answers

Having difficulty getting the program right

15 views (last 30 days)
Nathan Kim
Nathan Kim on 2 Aug 2020 at 16:03
Edited: Alan Stevens on 2 Aug 2020 at 16:53
Hello, I'm taking a minicourse on MATLAB and I was given an assignment that I am having difficulties completing. The assignment is to prompt the user for initial height (in meters), and vertical velocity (in m/s) and plot the height and vertical velocity as a function of time. I'm ending up with only a single dot. I assume it has something to do with my time variable, but given that I'm still early on in my education I don't know where to make the correction
The code I have is as follows:
g = -9.81;
h = input('Enter the initial height of the ball in meters: ');
v0 = input('Enter the initial velocity of the ball in meters squared: ');
t = 0;
h = (0.5)*(g)*t.^2+(v0)*(t)+(h);
v = (g)*(t)+(v0);
% Plot the height and veloctiy as a function of time
plot(t,h);
hold on;
plot(t,v,'r');
title('Position and Velocity of a Ball');
xlabel('Time t (seconds)');
ylabel('Height h (meters)');
grid on;

  0 Comments

Sign in to comment.

Answers (1)

Alan Stevens
Alan Stevens on 2 Aug 2020 at 16:19
Edited: Alan Stevens on 2 Aug 2020 at 16:53
Make h and v functions of time. For example;
h = @(t) (0.5)*(g)*t.^2+(v0)*(t)+h0;
Also, make sure you use a different symbol for the input value (i.e. h0 not just h)!
And, of course, you'll need to supply vaues of t.

  0 Comments

Sign in to comment.