<< Chapter < Page | Chapter >> Page > |
When the test at the top of the while loop determines that the previously-computed value for height was negative,
This is not a complicated script in comparison with some of the scripts that were presented in earlier modules.
Given the output shown in Figure 2 , you should be able to estimate and plot the velocity of the arrow on the same graph bydividing the displacement during each time interval by the value of the time interval.
Estimating variable velocity with a computer model
Let's write a script that approximates the graphic procedure that I described above.
Copy the code from Listing 2 into an html file and open the file in your browser.
Listing 2 . Variable velocity exercise #2.
<!---------------- File JavaScript2.html ---------------------><html><body><script language="JavaScript1.3">document.write("Start Script<br/>");
//Declare and initialize constantsvar g = -32.2;//acceleration of gravity in ft/sec/sec
var v0 = 100;//initial velocity in ft/secvar h0 = 6;//initial height in feet
var tInc = 0.25;//calculation interval in seconds//Declare and initialize working variables
var t = .0001;//current time in secondsvar h = h0;//current height in feet
var v = 0;//current velocity in feet/secvar oldT = 0;//previous time in seconds
var delT = 0;//change in time in secondsvar oldH = h0;//previous height in feet
var delH = 0;//change in height in feet//The following variable is used to insert spaces in
// the output display.var sp = " "
//Compute and display various values for as long as the// height of the projectile is above the ground.
while(h>0){
//This is the general equation for the height of a// projectile under the influence of gravity in a
// vacuum.h = h0 + v0*t +0.5*g*t*t;
//Compute and save the time interval since the// previous estimate of height, etc.
delT = t-oldT;oldT = t;//Save the current time for the next iteration
//Estimate the velocity based on the change in// height since the previous estimate.
delH = h-oldH;v = delH/delT;//compute velocity
oldH = h;//Save the current height for the next iteration.//Display the information for this iteration
document.write("t = " + t.toFixed(2) + " seconds" + sp +
" h = " + h.toFixed(1) + " feet" + sp +" v = " + v.toFixed(2) + " feet/second" + sp +
"<br/>");
//Increment the time for the next iteration.t = t + tInc;
}//end while loopdocument.write("End Script");</script></body></html>
Screen output
The text shown in Figure 4 should appear in your browser window when the html file opens in your browser.
Figure 4 . Screen output for Listing #2. |
---|
Start Script
t = 0.00 seconds h = 6.0 feet v = 100.00 feet/secondt = 0.25 seconds h = 30.0 feet v = 95.97 feet/second
t = 0.50 seconds h = 52.0 feet v = 87.92 feet/secondt = 0.75 seconds h = 72.0 feet v = 79.87 feet/second
t = 1.00 seconds h = 89.9 feet v = 71.82 feet/secondt = 1.25 seconds h = 105.8 feet v = 63.77 feet/second
t = 1.50 seconds h = 119.8 feet v = 55.72 feet/secondt = 1.75 seconds h = 131.7 feet v = 47.67 feet/second
t = 2.00 seconds h = 141.6 feet v = 39.62 feet/secondt = 2.25 seconds h = 149.5 feet v = 31.57 feet/second
t = 2.50 seconds h = 155.4 feet v = 23.52 feet/secondt = 2.75 seconds h = 159.2 feet v = 15.47 feet/second
t = 3.00 seconds h = 161.1 feet v = 7.42 feet/secondt = 3.25 seconds h = 160.9 feet v = -0.63 feet/second
t = 3.50 seconds h = 158.8 feet v = -8.68 feet/secondt = 3.75 seconds h = 154.6 feet v = -16.73 feet/second
t = 4.00 seconds h = 148.4 feet v = -24.78 feet/secondt = 4.25 seconds h = 140.2 feet v = -32.83 feet/second
t = 4.50 seconds h = 130.0 feet v = -40.88 feet/secondt = 4.75 seconds h = 117.7 feet v = -48.93 feet/second
t = 5.00 seconds h = 103.5 feet v = -56.98 feet/secondt = 5.25 seconds h = 87.2 feet v = -65.03 feet/second
t = 5.50 seconds h = 69.0 feet v = -73.08 feet/secondt = 5.75 seconds h = 48.7 feet v = -81.13 feet/second
t = 6.00 seconds h = 26.4 feet v = -89.18 feet/secondt = 6.25 seconds h = 2.1 feet v = -97.23 feet/second
t = 6.50 seconds h = -24.2 feet v = -105.28 feet/secondEnd Script |
Notification Switch
Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?