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

import java.io.*;

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:

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:

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)

0 60
10 2,927
20 10,170
30 21,486
40 33,835
50 45,251
60 55,634
70 65,038
80 73,461
90 80,905
100 87,368
110 92,852
120 97,355
130 100,879
140 103,422
150 104,986
160 106,193
170 110,247
180 119,626
190 136,106
200 162,096
210 199,506
220 238,776
230 277,065
240 314,375
250 350,704

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

## All 3 Replies

Where are you having problems? Reading the file or doing the formula.
The above are 2 different things.
First write a method that reads a file and returns a double array. I assume that all data in the file have 10 seconds difference.

``````public double [] read(File file) {

}``````

Search this forum for examples on how to read a file using BufferedReader or the Scanner class and post your code. Test it and print the array. Once you are done with that, you have your array and simply write a for loop that applies the formula described:

``````// Y'k = (Yk+1 - Yk-1) / 2h
result[k] = (array[k+1] - array[k-1]) / (2*h);``````

The above will be in a for loop

I am having problems in writing the formulas.

Can you post the scientific formula that you wan to use. Also indicate which of the elements of the formula are the distance and the time

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.