3

I've recently begun my journey into C++ and have very little knowledge of it other than the basics. I'm trying to translate a Matlab code of mine to C++ as a way to help me understand the differences between the two. The Matlab code takes a given input X(height), and calculates the density (rho), and speed of sound (acousticSpeed) for the input.

Here is the Matlab code.

function [rho, acousticSpeed] = atmos(X)

     %only valid to X = 11Km
       %Constants
       gamma=1.4; 
       R=287.05;
       g=9.81; 
       To=288.15;
       Po=101325;
       zo=50; 
       L=-0.0065;
       %Temperature Calculation

       T=To+(L*(X-zo));

       %Pressure Calculation

       P=Po*(T/To)^(-g/(L*R));

       %Density Calculation

        rho=P/(R*T);

        %Acoustic Speed

        acousticSpeed=sqrt(gamma*R*T)



         end

From what I've learned about C++, functions cannot return more than one value(or at least, its a very intensive process to make it so). This Matlab function returns two values, rho and acousticSpeed. For now, I have split this into 2 functions on C++ to calculate each individual output with the relevant equations.

For Rho I have

rho(double x){
double zo;
double To;
double Po;
double L;
double g;
double R;
double p;
zo = 50;
To = 288.15;
Po = 101325;
L = -0.0065;
g = -9.81;
R = 287.05;
double T = To + L*(x-zo);
double P = pow((Po*(T/To)), -(g*(L*R)));
p = P/(R*T);
return p; 
}

For Speed of Sound I have

soundspeed(double x){
double zo;
double To;
double L;
double R;
double as;
double gamma;
zo = 0;
To = 288.15;
L = -0.0065;
R = 287.05;
gamma = 1.4;
double T = To + L*(x-zo);
as = pow(gamma*R*T,0.5);
return as;
}

and my Main function is

int main()
{
cout << "Please enter a desired altitude in meters." << endl;
double x;
double A;
double B;
cin>> x;
A = soundspeed(x);
B = rho(x);
cout << "For Altitude: " << x << "  meters" << endl;
cout << "Speed of Sound:  " << A << " meters per second." << " Air Density: 
" << B;

return 0;
}

For an input of 500 meters, the Speed of Sound is 338 meters per second, and density is approximately 1.225. The speed of sound function returns the correct value, but the density function returns 0.

I have included iostream and math.h(for the pow() function).

What have I done wrong? Is there a cleaner way to translate this Matlab Function to C++? Are there any other tips for me as a beginner that you experienced folks can give? Thanks. Apologies for the length, I was unsure how else to include all the information.

  • 1
    First thing is that you are declaring your variables as old style C. Declare your variables when you need them at the scope you need. – Matthieu Brucher Dec 06 '18 at 14:24
  • math.h is also C header, the C++ one is cmath. Then what have you debugged? – Matthieu Brucher Dec 06 '18 at 14:26
  • 1
    functions can only return a single value, though it is most trivial to declare a structure containing more than one value, eg `struct two_values { int value1; int value2;};`, that you can return without much effort – 463035818_is_not_an_ai Dec 06 '18 at 14:27
  • In C, you needed to declare (C89?) variables first. Also if you are using ugly gotos. – Matthieu Brucher Dec 06 '18 at 14:30
  • 1
    not the problem: `pow` may seem like the universal function to calculate powers, though there are several cases where `pow` isnt the best (eg. for integers). Maybe `pow` will do the same, but still for square roots I'd rather use [std::sqrt](https://en.cppreference.com/w/cpp/numeric/math/sqrt) instead. – 463035818_is_not_an_ai Dec 06 '18 at 14:35
  • 1
    @MatthieuBrucher yes [it was the case in C89](https://stackoverflow.com/questions/288441/variable-declaration-placement-in-c). As a strange curiosity I found [this](https://www.dummies.com/programming/c/declaring-variables-in-c/) which has a tip regarding where to declare variables in c that I dont dare to quote here ;) – 463035818_is_not_an_ai Dec 06 '18 at 14:42
  • Related or not: the parameter `zo` is equal to `0` or `50` depending of the function, always 50 in matlab code. – Damien Dec 06 '18 at 14:43
  • 1
    You will avoid this kind of problems by defining a `struct` for all the parameters, setting theirs values, and using it as an input variable for the functions – Damien Dec 06 '18 at 14:48
  • @Damien zo is supposed to be 50 for all, that was an oversight. Thanks for pointing that out. I was not aware of structures so this might help. – Jim Dimmette Dec 06 '18 at 14:53
  • 1
    twimc, I dont agree with the close reason, the question has a mcve, it clearly states what is the input, output and expected output. Usually question about translating code, just provide code in one language and expect the other code as an answer, in this sense this question is a positive example imho – 463035818_is_not_an_ai Dec 06 '18 at 14:54

1 Answers1

3

The issue was with parenthesis and placement of values, particularly in the pressure calculation. The original was

double P = pow((Po*(T/To)), -(g*(L*R)));

However the correct equation is

double P = Po*pow((T/To), -(g/(L*R)));

A simple solution that I should have tried before posting, as I did not want to waste anyone's time.

Thank you all for your help!