This is my assignment:
The field of calculus is largely concerned with the concepts of the derivative and the integral of functions. Derivatives and integrals are closely related because they "undo" or reverse one another. If the derivative of a function is taken, a new function is obtained which, if integrated, produces the original function again, to within a constant. If the order is reversed, we get the same result.
The integral of a function can be described graphically as the area under the function curve. The derivative of a function at some specific point is the rate of change of the function at that point. This is illustrated in the following diagram where the derivative of the function at point Yk is represented by the slope of the line that runs tangent to the function at that point.
If we calculated the derivatives at many points along the function, we could graph them as a new function which represents the derivative of the function above. We could then calculate the derivative at many points on our derivative function, which would represent the second derivative of our original function at those points. If we plotted those, we would have a graph of the second derivative of our original function.
If the derivative of a function is difficult or impossible to find mathematically, it can be estimated graphically, and thus numerically, by finding the slope of the secant line between data points, as illustrated in the figure below.
The first derivative of the function at Xk, which is denoted Y'k, is estimated by computing the slope of the secant line connecting points Yk-1 and Yk+1:
Y'k = (Yk+1 - Yk-1) / 2h
It is very important to notice that as the data points get closer together, the estimate for the first derivative becomes increasingly accurate because as h approaches zero, the slope of the secant line connecting the data points approaches the slope of the tangent line at Yk. When modeling such functions computationally, then, greater accuracy is achieved by selecting smaller steps along the X axis-- the smaller the steps, the greater the accuracy.
To compute the second derivative Y'k, which is the rate of change of the first derivative at Xk, we use this method again. In the following figure, we draw secant lines between the adjacent points and compute the slopes of these two lines. Then, we compute the difference in the slopes of the two lines and divide by the distance between their midpoints. The result is the rate of change of the slope, which is also an estimate of the second derivative of the function at Xk.
Consequently, the formula for estimating the second derivative is:
Y'k = (((Yk+1 - Yk) / h) - ((Yk - Yk-1) / h)) / h
= (Yk+1 - 2Yk + Yk-1) / h^2
Such a formula is known as a "central difference" technique. Note that we cannot compute derivatives for the first and last points of a data set using this technique; to do so, we could utilize the so-called "forward" or "backward" difference techniques.
Caution should be exercized when using techniques of numerical differentiation since noisy, or otherwise inaccurate, functions can lead to large inaccuracies in the estimates of the derivatives. This is illustrated in the two figures below where the derivative of the function at point b in the respective figures would be quite different.
Numerical differentiation should only be used, therefore, when a data set represents a function quite accurately, and even then the results should be used with care.
Preliminary Rocket Exercise
The model program requires that you be able to read some data from a file on disk. Use Notepad, or another text editor, to create a list of the altitude values from the table below (do not enter the time values and do not include the commas in the altitude values). Be sure to enter each number on a separate line and then hit the Enter key. Save the file as altitude.txt.
Reading a file on disk requires a little bit of set-up. You will need the
statement at the head of your program. You will also find that a "throws" clause is required in the method declaration of the method in which the reading code is found, like this:
public static void main(String argv) throws IOException, FileNotFoundException
Basically, you will need to set up a BufferedReader at the start of the main method called, say, "b" like this:
BufferedReader b = new BufferedReader(new InputStreamReader(new FileInputStream("altitude.txt")));
Note especially that whenever files on disk are read with a BufferedReader, the lines of data are in fact strings. In order to do calculations with them in the rocket application below, you will need to cast (convert) them from type String to type double, after they are initially read from "altitude.txt."
Let's suppose, in your program, that you have already declared a string array, at the beginning of the main method, called "stringArray" with a length of 26 to hold the 26 values in altitude.txt. You will need to use "stringArray" to hold the lines of input from the text file. In a loop, load the string array using your BufferedReader, b, with the lines of input from "altitude.txt" with this syntax:
stringArray = b.readLine( );
and then, in another loop, use a double array that has already been declared at the beginning of the main method (called, let's say, "doubleArray"), with the same size as the string array, to convert the members of the string array to double using syntax like the following:
doubleArray = Double.parseDouble(stringArray);
Then, in another loop, plot the altitude data in "doubleArray," as a function of time, using the "Turtle" or some "SmartTurtle" class that you have developed. Remember that the points actually occur at 10-second intervals. You should obtain a plot similar to the "Altitude of Rocket" plot below (look at that graph in order to choose appropriately scaled axes for your graph.
(Note that this exercise program will not run as an applet since Java applets cannot read or write data files on disk-- a security measure, of course. You should write this program to run as an application only.)
Application: Rocket Trajectory
Imagine the first stage of a rocket burning for about 35 seconds, accelerating the rocket to a velocity of 1250 m/s. Then, the rocket coasts for almost 2 minutes before reaching the lower levels of the ionosphere at about 100 km. Gravity has slowed the rocket's ascent to about 100 m/s at this point, but then the second stage of the rocket ignites and accelerates the rocket into space.
The plot below shows the altitude measurements of the rocket as a function of time. It contains data points at 10-second intervals along the altitude curve.
A program is needed to model the velocity and acceleration of the rocket. It is important to note that the first derivative (the instantaneous rate of change) of the altitude vs. time function represents the velocity of the rocket, and the derivative (the instantaneous rate of change) of the velocity function will represent the acceleration of the rocket! Write a program that will read the altitude data from a file on disk and estimate the corresponding velocity and acceleration information using numerical differentiation. Again, use the Turtle Class that you learned about in chapter 1 from the text Java Au Naturel to create plots of the velocity and acceleration curves.
The following table contains the altitude data for your program:
Time (sec.) Altitude (meters)
Your output should look like the the graphs below. Notice in the acceleration curve that, after the first burn (at about 35 seconds), the acceleration of the rocket is -9.8 m/sec/sec for a period of time, which is the deceleration of the rocket due to gravity. The speed of the rocket, therefore, is decreasing during this interval.
Instead of doing all that and making the program really complicated, all I want to do is write a program that reads the input file, calculates first derivative, calculates the second derivative, and prints the following output.
The output should look like:
Altitude at 0 seconds is 60.0
Altitude at 10 seconds is 2927.0
Altitude at 20 seconds is 10170.0
Altitude at 30 seconds is 21486.0
Altitude at 40 seconds is 33835.0
Altitude at 50 seconds is 45251.0
Altitude at 60 seconds is 55634.0
Altitude at 70 seconds is 65038.0
Altitude at 80 seconds is 73461.0
Altitude at 90 seconds is 80905.0
Altitude at 100 seconds is 87368.0
Altitude at 110 seconds is 92852.0
Altitude at 120 seconds is 97355.0
Altitude at 130 seconds is 100879.0
Altitude at 140 seconds is 103422.0
Altitude at 150 seconds is 104986.0
Altitude at 160 seconds is 106193.0
Altitude at 170 seconds is 110247.0
Altitude at 180 seconds is 119626.0
Altitude at 190 seconds is 136106.0
Altitude at 200 seconds is 162096.0
Altitude at 210 seconds is 199506.0
Altitude at 220 seconds is 238776.0
Altitude at 230 seconds is 277065.0
Altitude at 240 seconds is 314375.0
Altitude at 250 seconds is 350704.0
Velocity at 10 seconds is 505.5
Velocity at 20 seconds is 927.95
Velocity at 30 seconds is 1183.25
Velocity at 40 seconds is 1188.25
Velocity at 50 seconds is 1089.95
Velocity at 60 seconds is 989.35
Velocity at 70 seconds is 891.35
Velocity at 80 seconds is 793.35
Velocity at 90 seconds is 695.35
Velocity at 100 seconds is 597.35
Velocity at 110 seconds is 499.35
Velocity at 120 seconds is 401.35
Velocity at 130 seconds is 303.35
Velocity at 140 seconds is 205.35
Velocity at 150 seconds is 138.55
Velocity at 160 seconds is 263.05
Velocity at 170 seconds is 671.65
Velocity at 180 seconds is 1292.95
Velocity at 190 seconds is 2123.5
Velocity at 200 seconds is 3170.0
Velocity at 210 seconds is 3834.0
Velocity at 220 seconds is 3877.95
Velocity at 230 seconds is 3779.95
Velocity at 240 seconds is 3681.95
Acceleration at 10 seconds is 43.76
Acceleration at 20 seconds is 40.73
Acceleration at 30 seconds is 10.33
Acceleration at 40 seconds is -9.33
Acceleration at 50 seconds is -10.33
Acceleration at 60 seconds is -9.79
Acceleration at 70 seconds is -9.81
Acceleration at 80 seconds is -9.79
Acceleration at 90 seconds is -9.81
Acceleration at 100 seconds is -9.79
Acceleration at 110 seconds is -9.81
Acceleration at 120 seconds is -9.79
Acceleration at 130 seconds is -9.81
Acceleration at 140 seconds is -9.79
Acceleration at 150 seconds is -3.57
Acceleration at 160 seconds is 28.47
Acceleration at 170 seconds is 53.25
Acceleration at 180 seconds is 71.01
Acceleration at 190 seconds is 95.1
Acceleration at 200 seconds is 114.2
Acceleration at 210 seconds is 18.6
Acceleration at 220 seconds is -9.81
Acceleration at 230 seconds is -9.79
Acceleration at 240 seconds is -9.81
PLEASE HELP :(