- #1
e_brock123
- 14
- 0
Homework Statement
I'm currently new to Matlab and have just started trying to solve a few problems with it. So I have to make a program that will display the results of height and velocity of a falling object, as well as plotting the overall function.
My main concern is I am using an array for t = [0:0.1:20] seconds were I would rather have time go on till h=0. For these equations:
h = (-0.5*g*t.^2 + u*t) + h0
v = u + g*t
So I was just wondering how I can make my script end time when the ball has reached the ground? I am guessing it will be some kind of if/for loop but still a bit unsure.
Homework Equations
The Attempt at a Solution
This is the script I have wrote so far, any help will be greatly appreciated.
% Script file: Position_velocity_ball.m
% constants
g = 9.81; % Acceleration due to gravity
t = 0:0.1:20;
% Get the initial height from the user
h0 = input('Enter the initial height (m):');
% Get the initial velocity from the user
u = input ('Enter the initial velocity (m/s):');
% Calculate the current height of the ball
h = (-0.5*g*t.^2 + u*t) + h0;
disp(['The height of the ball is ' num2str(h) 'm.']);
% Calculate the current velocity
v = u + g*t;
disp(['The velocity of the ball is ' num2str(v) 'm/s']);
% plot the result
plot(v,h);
xlabel('velocity');
ylabel('height');
title('Height versus Velocity');
grid on;