Jim Dimmette
Jim Dimmette

Reputation: 69

Matlab to C++ Translation

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.

Upvotes: 3

Views: 173

Answers (1)

Jim Dimmette
Jim Dimmette

Reputation: 69

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!

Upvotes: 3

Related Questions