从高度列表加速 [英] Acceleration from altitude list

查看:57
本文介绍了从高度列表加速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个应该从.txt文件中获取火箭高度值列表的程序

 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



并计算某一时刻火箭的速度和加速度。到目前为止,我得到了高度和速度代码正常工作,但我得到的加速值是完全错误的。我的代码应显示

10秒加速度为43.76

20秒加速度为40.73

加速度为30秒是10.33

加速40秒是-9.33

加速50秒是-10.33

加速60秒是-9.79

加速70秒是-9.81

加速80秒是-9.79

加速90秒是-9.81

加速度在100秒是-9.79

加速在110秒是-9.81

加速在120秒是-9.79

加速在130秒是 - 9.81

加速140秒是-9.79

加速150秒是-3.57

加速160秒是28.47

170秒加速度为53.25

180秒加速度为71.01

190秒加速度为95.1

加速度为200秒是114.2

加速在210秒是18.6

加速在220秒是-9.81

加速在230秒是-9.79

加速在240秒是 - 9.81


相反它显示

10秒加速度为50.55

20秒加速度为92.795

加速30秒是118.325

加速40秒是118.825

加速50秒是108.995

加速度60秒是98.935

加速在70秒是89.135

加速在80秒是79.33500000000001

加速在90秒是69.535

加速100秒是59.735

加速110秒是49.935

加速120秒是40.135000000000005

加速130秒是30.335

加速140秒是20.535

加速150秒是13.855

Accelera 160秒时为26.305

170秒加速度为67.16499999999999

180秒加速度为129.29500000000002

加速度为190秒为212.35

加速200秒是317.0

加速210秒是383.4

加速220秒是387.79499999999996

加速度230秒是377.995

加速240秒是368.195


我的代码已附上

  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 加速度( 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);
返回 a;
}
public static void main( String [] args) throws IOException,FileNotFoundException {
BufferedReader a = new BufferedReader( new InputStreamReader( new FileInputStream( build / classes / Resources / altitude.txt)));
字符串 [] 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 + 秒是 + x [i ]);
}
for int i = 0; i< x.length; i ++){
if (i> 0&& i< 25){
System.out.println( 速度为 + i * 10 + 秒是 + velocity(i * 10 - < span class =code-digit> 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( 加速度为 + i * 10 + 秒是 +加速度(i * 10 - 10 ,i * 10 + 10,x [i + 1],x [i- 1 ]));
}
}
}
}



我试图调试代码,但我不确定是否读数组时出现问题或加速方法数学存在问题。谁能告诉我哪里出错了以及如何解决?

解决方案

速度计算是正确的,但你应该在相邻的对上调用它。然后将这些速度值保存在一个数组中,并通过对速度数组中相邻值进行完全相同的计算计算加速度。

速度:v =Δy/Δt

加速度:a =Δv/Δt



我不是火箭科学家;-)但我在JPL的工作1996年 1976 分析火星维京数据。



编辑:固定日期


< pre lang =text>'我试图调试代码,但我不确定读取数组是否有问题或加速方法数学有问题。





验证读取数组的代码很简单:打印数组x的内容。如果它与输入文本匹配,请继续。



你要做的是写一个单元测试 [ ^ ]为您的加速功能。



写一个单独的public static void Main(),用一些已知的输入(你通过手工计算验证)调用函数。如果输出等于你的预期值,你的功能(可能)是好的(至少对于那一个输入)。



如果它不匹配,检查你的数学。



我采取以下措施......



加速10秒是43.76 





......前三行是...



 60 
2927
10170





...意味着你期待以下是真实的:



  double  expected =  43  76 ; 
double a =加速度( 0 20 10170 60 );
// float / double的相等测试是dicy所以选择一个小的
// 容差值,表示足够接近。
断言 Math.abs(预期 - a)< 0 00000001 ;





提示使用断言:



http://docs.oracle.com/javase/specs/jls/se8/html/jls-14.html#jls-14.10 [ ^ ]


Quote:

我试过调试代码但是我不确定是否有读取数组的问题或加速方法数学的问题。

所以你在调试器上运行程序,你仍然不知道出了什么问题!

调试器没有魔力,它是一个工具,你必须学会​​使用它。



调试器允许你逐行执行代码并每次检查变量。

如果参数化,这允许你 SEE 函数的rs是你期望与否的结果,并且每行执行时结果都相同。

到现在为止,你应该知道什么是对错。



否则,你应该尝试更换

  double  m2 =(y2 -Y 3)/(X1-X3); 



with

  double  m2 =(y2-y3)/(x2-x3); 


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

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?

解决方案

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


'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:

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[^]


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

double m2 = (y2-y3)/(x1-x3);


with

double m2 = (y2-y3)/(x2-x3);


这篇关于从高度列表加速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆