%
% 程序作用:
% 計算並畫出一靜止小球在離地h0的地方以初速度v0做垂直運動在t時刻的位置及速度
%
%定義變量:
% h0 --初始高度(m)
% vo --初始速度(m/s)
% g --重力加速度(m/(s^2))
% t --時間(s)
% ht --t時刻小球的高度(m)
% vt --t時刻小球的速度(m/s)
%
%給定初始高度、速度及重力加速度
h0=500;
v0=0;
g=-9.81;
%時間的變化區間
t=0:1:10;
%計算速度
vt=g.*t;
%計算高度
ht=g.*(t.^2)/2+h0;
%繪圖
plot(t,vt,t,ht);
%輸出數據
fprintf('%f s the height is %f m\n',t,ht);
輸出:
2.000000 s the height is 3.000000 m
4.000000 s the height is 5.000000 m
6.000000 s the height is 7.000000 m
8.000000 s the height is 9.000000 m
10.000000 s the height is 500.000000 m
495.095000 s the height is 480.380000 m
455.855000 s the height is 421.520000 m
377.375000 s the height is 323.420000 m
259.655000 s the height is 186.080000 m