0

I wrote an orbit propagator (calculates position and velocity of a satellite for each time step) in matlab and it's pretty accurate (4.5 km in a 24-hour propagation). I had to rewrite it in C, I did it and it's working properly but my results are much less accurate (450 km for a 24-hour propagation), so I was wondering what could cause this loss of accuracy in C for the exact same calculations (I compared the scripts to be sure I didn't forget anything.) ?

All my variables are double or long double depending on the needs. There are no divisions by ints. The error grows linearly over time.

My main function contains a while loop that updates the values of my position and velocity arrays by calling the calculating function that takes pointers to the arrays as arguments.

la_
  • 51
  • 5
  • 7
    some code would be nice – Shlomi Agiv Jul 31 '14 at 12:30
  • 4
    Start by reading [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html). – Some programmer dude Jul 31 '14 at 12:30
  • Can you identify a particular calculation that causes the divergence in behaviour? – Oliver Charlesworth Jul 31 '14 at 12:33
  • @JoachimPileborg: It should not be necessary to read that in order to translate some Matlab code to C ;) – Oliver Charlesworth Jul 31 '14 at 12:34
  • @OliCharlesworth Probably not, but it might be to understand problems with floating point arithmetic. :) – Some programmer dude Jul 31 '14 at 12:35
  • 3
    **Don't** start by reading the reference provided by @JoachimPileborg. If you do need a primer on floating-point arithmetic start at http://en.wikipedia.org/wiki/Floating_point. Goldberg's paper is addressed to designers of f-p systems, not users. – High Performance Mark Jul 31 '14 at 12:35
  • Just out of curiosity, how big are your time steps? Do you get as big a divergence if you use coarser or finer steps? – John Bode Jul 31 '14 at 14:06
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Schorsch Jul 31 '14 at 18:27
  • 1
    Without code, best advice would be to dump all intermediate computations to files (both in matlab and C) and then compare them to find where divergence is occurring. – CitizenInsane Aug 01 '14 at 07:32

0 Answers0