% falling ball demo script -- fall.m % Author: F. P. Schloerb % computes solution of falling object under force of gravity % x(t) = x(0) - 1/2 g t^2 % % define an array of times for calculation % array t spans times from 0 to 10 seconds in steps of 1s t = 0:1:10; % get the initial height from the user: h = input('Enter initial height (m): '); % compute the array of positions corresponding to array of times, t % note use of .* operator for element-by-element multiplication of t arrays x = h - 0.5 * 9.8 * t.*t; % plot the result - with labels for axes plot(t,x); xlabel('time (s)'); ylabel('position (m)'); title('Falling Ball'); % display results in a table that looks nice fprintf('Here are results:\n') fprintf(' t x\n') fprintf(' --- -------\n') for i=1:11 fprintf('%5.1f %10.2f\n',t(i),x(i)) end