-2

So I am trying to make a basic C++ program that calculates the mean, eccentric, and true anomalies as well as the radial distance and velocity of a planet. This is one of my first programs I have worked on, so I know it's not as concise as it could be. I'm just having a hard time getting it to run. I know some of the values might need to be converted to make the units match up, and I can do that later. I just want to know what is causing it not to run. Also in main, I have if statements for every planet; I didn't include the rest because it would just take up too much space.

Ultimately, the goal of the program is to print a table of times, the anomalies, r, and v given the planet name. If you need any clarification, please let me know. Thank you

Thanks in advance

const float G = 6.673E-11;
const float solar_m = 1.989E30;
const float mu = G*solar_m;

class Planet
{
  double a, ec, T;
public:
  void SetValues(float, float, float);
  float Calculations();
};

void Planet::SetValues(float m_a, float m_ec, float m_T)
{
  m_a = a;
  m_ec = ec;
  m_T = T;
}

float Planet::Calculations()
{
  //Set up values
  a = a*1.496E11; //AU to m
  T = T*365;   //Period converted to days

  float delta = 0.001;

  float M;
  float E;

  float E1;
  float E2, f_E2;
  float E3, f_E3;

  float f,r,v;

  for(int t; t<T; t++)
    {
      M= sqrt(mu/(a*a*a))*t;
      E1= M;

      E2 = (M-E1+ec*sin(E1))/(ec*cos(E1)-1);
      f_E2 = M-E2+ec*sin(E2);

      E3 = (M-E2+ec*sin(E2))/(ec*cos(E2)-1);
      f_E2 = M-E3+ec*sin(E3);

      if (f_E2<delta)
        E=E2;
      else if (f_E3<delta)
        E=E3;
      else
        cout<<"not enough iterations";

      f = 2*atan(sqrt((1+ec)/(1-ec))*tan(E/2));
      r = (a*(1-ec*ec))/(1+ec*cos(f));
      v = sqrt(mu*(2/r -1/a));

      cout<< t<<"     "<<M<<" "<<E<<" "<<f<<" "<<r<<" "<<v;
    }
};

int main()
{
  int i;
  float t;
  string pName;
  cout<<"What planet do you want to simulate?";
  cin>>pName;

  if (pName.compare("mercury") || pName.compare("Mercury"))
    {
      Planet me;
      me.SetValues(.387, .2056, .24);
      me.Calculations();
    };
Tunaki
  • 132,869
  • 46
  • 340
  • 423
peasqueeze
  • 121
  • 2
  • 10
  • What problems are you having? Compile errors? – tadman Jan 19 '15 at 21:13
  • 1
    *" I just want to know what is causing it not to run. "* That is pretty broad there. Can you describe ***how*** it fails? – abelenky Jan 19 '15 at 21:14
  • 1
    It's probably a good idea to lookup how to use a constructor instead of using a 'setValues' function. – Simon Jan 19 '15 at 21:16
  • Your code has some pretty bad syntax errors. This certainly won't compile. It is also an incomplete example so it's almost impossible to tell what is going on. Please read [*“How to create a Minimal, Complete, and Verifiable example”*](https://stackoverflow.com/help/mcve) and edit your question accordingly. Maybe you can also reduce the total amount of code down to the essential bits. – 5gon12eder Jan 19 '15 at 21:22
  • 1
    Your `setValues` function is incorrect. Presently, you are assigning the object's data vaues to the parameters. The syntax should be `data_field = parameter;` – Thomas Matthews Jan 19 '15 at 21:33
  • In your `Calculations` method, you assign `E1=M`; which means they have the same value. In the equation for `E2` you have `(M-E1`, which yields zero, since `M` and `E1` have the same value due to the previous assignment statement. – Thomas Matthews Jan 19 '15 at 21:36
  • Ok, so I made some changes and got the program to start, but I get "Run-Time Check Failure #3 - The variable 't' is being used without being initialized." I initialized t in the loop, so I am not sure what this means. Also I used the method for finding E seen here http://mmae.iit.edu/~mpeet/Classes/MMAE441/Spacecraft/441Lecture16.pdf. – peasqueeze Jan 20 '15 at 00:40

3 Answers3

0

You really haven't explained your problem, but I'll offer some critiques.

You mix and match your use of double and float. This introduces rounding errors. Pick one and stick with it.

You have some methods that end with }; (Bracket-Semicolon). Check the syntax: methods do not end with semi-colons. Also your if-statement ends with a semi-colon. Same problem.

Your main function, however, does not end.

abelenky
  • 63,815
  • 23
  • 109
  • 159
  • I cut off the rest of main function to save room. I made some adjustments, and now I get the error "Run-Time Check Failure #3 - The variable 't' is being used without being initialized." I'm not sure why this is because I initialized t in the loop and again in main(). – peasqueeze Jan 20 '15 at 00:42
0

Some of your calculations are bothering me.

  M= sqrt(mu/(a*a*a))*t;
  E1= M;

  E2 = (M-E1+ec*sin(E1))/(ec*cos(E1)-1);
  f_E2 = M-E2+ec*sin(E2);

In the equation for E2, M and E1 have the same value, by definition of the E1=M assignment, so this yields:
E2 = (0+ec*sin(E1))/(ec*cos(E1)-1);
or
E2 = (ec * sin(E1)) / (ec * cos(E1) - 1);

Is the " - 1" in the denominator correct? This is preventing factoring out ec.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • I can show you the method that I used. I got it here http://mmae.iit.edu/~mpeet/Classes/MMAE441/Spacecraft/441Lecture16.pdf – peasqueeze Jan 20 '15 at 00:29
  • That `-1` is probably correct, that does happen in ellipse-related functions. – SirGuy Feb 09 '18 at 16:47
0

Your algorithm does not work because SetValues is setting argument values not member values.

Try this:

void Planet::SetValues(float m_a, float m_ec, float m_T)
{
    a = m_a;
    ec = m_ec;
    T = m_T;
}