Click here to Skip to main content
15,898,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote a program that is supposed to take a list of rocket altitude values from a .txt file
60
2927
10170
21486
33835
45251
55634
65038
73461
80905
87368
92852
97355
100879
103422
104986
106193
110247
119626
136106
162096
199506
238776
277065
314375
350704

and calculate the velocity and acceleration of the rocket at a certain time. So far I got the altitude and velocity code working properly, but the values I get for acceleration are completely wrong. My code is supposed to display
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

But Instead it displays
Acceleration at 10 seconds is 50.55
Acceleration at 20 seconds is 92.795
Acceleration at 30 seconds is 118.325
Acceleration at 40 seconds is 118.825
Acceleration at 50 seconds is 108.995
Acceleration at 60 seconds is 98.935
Acceleration at 70 seconds is 89.135
Acceleration at 80 seconds is 79.33500000000001
Acceleration at 90 seconds is 69.535
Acceleration at 100 seconds is 59.735
Acceleration at 110 seconds is 49.935
Acceleration at 120 seconds is 40.135000000000005
Acceleration at 130 seconds is 30.335
Acceleration at 140 seconds is 20.535
Acceleration at 150 seconds is 13.855
Acceleration at 160 seconds is 26.305
Acceleration at 170 seconds is 67.16499999999999
Acceleration at 180 seconds is 129.29500000000002
Acceleration at 190 seconds is 212.35
Acceleration at 200 seconds is 317.0
Acceleration at 210 seconds is 383.4
Acceleration at 220 seconds is 387.79499999999996
Acceleration at 230 seconds is 377.995
Acceleration at 240 seconds is 368.195

My code is attached
Java
package RocketTrajectory;
import java.io.*;
 public class RocketTrajectory {
    public static double velocity (double x1, double x2, double y1, double y2){
        double x3 = x2-x1;
        double y3 = y2-y1;
        double v = y3/x3;
        return v;
    }
    public static double acceleration (double x1, double x2, double y1, double y2) {
        double x3 = (x1+x2)/2;
        double y3 = (y1+y2)/2;
        double m1 = (y3-y1)/(x3-x1);
        double m2 = (y2-y3)/(x1-x3);
        double m3 = m2-m1;
        double a = m3/(x2-x1);
        return a;
    }
    public static void main(String[] args) throws IOException, FileNotFoundException {
        BufferedReader a = new BufferedReader(new InputStreamReader(new FileInputStream("build/classes/Resources/altitude.txt")));
        String[] s = new String[26]; 
        double[] x = new double[26]; 
        for(int i=0; i<26; i++) {
            s[i] = a.readLine();
            x[i] = Double.parseDouble(s[i]); 
        }
        for(int i=0; i<x.length; i++){
            System.out.println("Altitude at " + i*10 + " seconds is " + x[i]);
        }
        for(int i=0; i<x.length; i++){
            if(i>0 && i<25) {
                System.out.println("Velocity at " + i*10 + " seconds is " + velocity(i*10-10, i*10+10, x[i-1], x[i+1]));
            }
        }
        for(int i=0; i<x.length; i++){
            if(i>0 && i<25){
                System.out.println("Acceleration at " + i*10 + " seconds is " + acceleration(i*10-10, i*10+10, x[i+1], x[i-1]));
            }
        }
    }
}

I've tried to debug the code but I'm not sure if there is a problem reading the array or a problem with the acceleration method mathematics. Could anyone tell me where I went wrong and how to fix it?
Posted
Comments
Garth J Lancaster 3-Nov-15 20:18pm    
I'm not a rocket scientist, but, if your altitude and velocity is working, it points to the acceleration method mathematics doesn't it ?

so, how was that function derived ?

The velocity calculation is correct, but you should be calling it on adjacent pairs. Then save those velocity values in an array and calculate acceleration by doing the exact same calculation on adjacent values in the velocity array.
Velocity: v = Δy / Δt
Acceleration: a = Δv / Δt

I'm not a "rocket scientist" ;-) but I did work at JPL in 1996 1976 analyzing Mars Viking data.

Edit: fixed date
 
Share this answer
 
v3
Comments
Patrice T 3-Nov-15 21:25pm    
"I'm not a "rocket scientist" Wink | ;-) but I did work at JPL in 1996 analyzing Mars Viking data."
As long as you were not in the Mars Climate Orbiter team :-)
Matt T Heffron 4-Nov-15 11:54am    
Nope! And that was a typo on the date. It should have been 1976
Patrice T 4-Nov-15 16:39pm    
Was simply kidding, anyway :)
've tried to debug the code but I'm not sure if there is a problem reading the array or a problem with the acceleration method mathematics.


Validating the code that reads the array is easy: print the contents of array x. If it matches the input text, move on.

What you want to do is write a unit test[^] for your acceleration function.

Write a separate public static void Main() that calls the function with some known inputs (that you've verified through hand calculation). If the output equals your expected value, you function is (likely) good (at least for that one input).

If it does not match, check your math.

I take the following ...

Acceleration at 10 seconds is 43.76


... where the first three lines are ...

60
2927
10170


... to mean you expect the following to be true:

Java
double expected = 43.76;
double a = acceleration(0, 20, 10170, 60);
// equality tests of float/double are dicy so pick a small 
// tolerance value that means "close enough".
assert Math.abs(expected - a) < 0.00000001;


Hint for using assert:

http://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.10[^]
 
Share this answer
 
v2
Quote:
I've tried to debug the code but I'm not sure if there is a problem reading the array or a problem with the acceleration method mathematics.
So you run the program on debugger and you still don't know what is wrong !
The debugger is no magic, it is a tool and you have to learn to use it.

The debugger allow you to execute your code line by line and to inspect variables every time.
This allow you to SEE if parameters of a function are what you expect or not, and the same for results as each line is executed.
By now, you should know what is right or wrong.

Otherwise, you should try to replace
Java
double m2 = (y2-y3)/(x1-x3);

with
Java
double m2 = (y2-y3)/(x2-x3);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900