<< Chapter < Page Chapter >> Page >

Please prepare a piece of graph paper to plot a curve defined by several points. Interpret the grid lines such that you can plot values ranging from 0 to 10000feet on the vertical axis and you can plot values ranging from 0 to 30 seconds on the horizontal axis.

JavaScript code

Copy the code from Listing 3 into an html file and open it in your browser.

Listing 3 . A plotting exercise.
<!-- File JavaScript03.html --<<html<<body<<script language="JavaScript1.3"<//Function to compute free-fall distance for a given time // interval in seconds.function distance(time){ var g = -32.174; //acceleration of gravity in feet/sec^2var d = 0.5 * g * time * time;//(feet/sec^2)*sec^2 = feet return new Number(d.toFixed(0));}//end function //Compute the height of the rock every two seconds from// release until the rock hits the ground. var t = 0;//secondswhile(t<= 30){ d = distance(t);//distance traveled in feeth = 10000 + d;//height in feet//Don't allow the height to go negative (underground) if(h<0){ h = 0;}//end if//Display time and height at current time. document.write("time = " + t +" height = " + h + "<br/<"); t = t + 2;}//end while document.write("<br/>The End")</script<</body<</html<

The screen output

When you open the html file in your browser, the text shown in Figure 7 should appear in your browser window.

Figure 7 . Screen output for Listing #3.
time = 0 height = 10000 time = 2 height = 9936time = 4 height = 9743 time = 6 height = 9421time = 8 height = 8970 time = 10 height = 8391time = 12 height = 7683 time = 14 height = 6847time = 16 height = 5882 time = 18 height = 4788time = 20 height = 3565 time = 22 height = 2214time = 24 height = 734 time = 26 height = 0time = 28 height = 0 time = 30 height = 0The End

Plot the data

Plot the 16 points shown in Figure 7 . Your horizontal axis should be the time axis and your vertical axis should be the height axis.

If we have both done everything correctly, the shape of your curve should be an upside-down parabola with its high point at a value of 10000. (If the wordparabola is new to you, don't worry about it. It's not important for the purpose of this module.)

The graph

Figure 8 shows an image of the graph that describes the trajectory of the falling rock. Asusual, please ignore the lower-case letters scattered throughout the image. They were placed there when the image was used for a different purpose earlier.

Figure 8 - The trajectory of the falling rock.

Missing image

Change in height increases with time

Getting back to the height versus time values shown in Figure 7 and plotted in Figure 8 , the value for height should decrease for each successive time value.

Also, the change in height should increase for each successive time interval until the height goes to zero. The height goes to zero when the rock has landed on theground somewhere between 24 and 26 seconds. It continues at zero after that.

(Note that I put code into the script to prevent the value of height from going negative.)

The increase in the change in the height value during each successive time interval is the result of the time being squared in the equation given earlier . It is also the result of the acceleration of gravity that causes the downward speed of the rock to increaseas the rock falls.

Analysis of the code

Listing 3 calls a function copied from the previous exercise inside a while loop to compute the distance traveled and the resulting height for a series of time values ranging from 0 to 30 seconds intwo-second intervals.

Those values are displayed as shown in Figure 7 .

Understanding the code in Listing 2 and Listing 3 is important. However, it is more important at this point that you understand the handling of units asshown in the comments in the code.

Run the scripts

I encourage you to run the scripts that I have presented in this lesson to confirm that you get the same results. Copy the code for each script into atext file with an extension of html. Then open that file in your browser. Experiment with the code, making changes, and observing the results of your changes. Makecertain that you can explain why your changes behave as they do.

Miscellaneous

This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: GAME 2302-0350 Units and Dimensional Analysis
  • File: Game0350.htm
  • Published: 10/12/12
  • Revised: 02/01/16
Disclaimers:

Financial : Although the Connexions site makes it possible for you to download a PDFfile for this module at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should be aware thatsome of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.comshowing me as the author. I neither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, pleasebe aware that it is a copy of a module that is freely available on cnx.org and that it was made and published without my prior knowledge.

Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.

-end-

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Game 2302 - mathematical applications for game development. OpenStax CNX. Jan 09, 2016 Download for free at https://legacy.cnx.org/content/col11450/1.33
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Game 2302 - mathematical applications for game development' conversation and receive update notifications?

Ask