# Solving a more difficult math equation in c programming

• 09-12-2011
karma_jesus45
Solving a more difficult math equation in c programming
The program is supposed to solve for U_T (terminal velocity of a particle falling in water)
Everything can be found except Re and U_T
Re is basically 100 < Re <10000. I TRIED to create a while loop for Re. I am more used to Matlab than C programing, but it needs to be done in C.
The while loop should stop when F_D + F_B = W
After it breaks I want it to display the U_T and Re used
The difficult part is that U_T is used in another equation. I am wondering if I need to take it out of the loop.

I was wondering if you guys can tell me if I am using the while loop correctly?
And if you can tell me how to make it print the U_T and Re values when it is done.

Code:

```// by Eric // Math program to solve for terminal velocity of particle falling into water #include<stdio.h> #include<math.h>   int main(int argc,char *argv[])         {                 int D;                        //diameter                 int rho_p;                //density of sand particle                 int rho_f;                //density of fluid                 int mu_f;                //dynamic viscosity of fluid                 int g;                        //gravity                 int F_D;                //Drag Force                 int C_D;                //Coefficent of Drag                 int U_T;                //Terminal Velocity solving for                 int F_B;                //Force of Bounacy                 int m                        //mass                 int Re                        //Reynolds Number                 int W                        //Weight                 int PI                        //pi                         D = 300*10^(-6);        // diameter of sand particle in units of meters                 rho_p = 2000;                // density of sand particle in units of kg/m^3                 rho_f = 998.2;                        // density of fluid (kg/m^3) at 68 F                 mu_f = 1.002*10^(-3);                                // Dynamic Viscosity of fluid(kg/m*s) at 68 F                 g = 9.81;                        // gravity (m/s^2)                        PI = 3.141592653589793;                                 //Re=((U_T·D·rho_f) / (mu_f));                 //U_T=((Re*mu_f)/(D*rho_f));                 //F_D=C_D·((PI·D*D) / (4))*0.5·rho_f·U_T*U_T;                 //F_B=rho_f·((PI*pow (D, 3)) / (6))·g;                 W=m·g;                 m=rho_p·((PI*pow (D, 3)) / (6));                 //C_D=((24) / (Re))*pow ((1+0.27·Re), 0.43)+0.47·(1-exp (-0.04*pow (Re, 0.38)));                                                 Re=100;                 while (Re <= 10000)                 {                         U_T=((Re*mu_f)/(D*rho_f));                         F_D=C_D·((PI·D*D) / (4))*0.5·rho_f·U_T*U_T;                         F_B=rho_f·((PI*pow (D, 3)) / (6))·g;                         C_D=((24) / (Re))*pow ((1+0.27·Re), 0.43)+0.47·(1-exp (-0.04*pow (Re, 0.38)));                         Re++;                         if (F_D+F_B == W)                                 break;                 }                 return 0;         }```
• 09-12-2011
stahta01
No idea what the dot should be; but, it does not likely exist in C.
Code:

`·`
WARNING the "^" is NOT the power symbol in C; it is a bit-wise operator.
Code:

`mu_f = 1.002*10^(-3);`
WARNING
3/4 in C results in 0 being the answer.
I suggest using double instead of int datatype.

Tim S.
• 09-12-2011
anduril462
3 things that jumped out at me within 5 seconds of reading your code:

1. int is for integers only. Anything that needs to use numbers with fractional components needs to use floating point types (double is preferred).
2. There is no "power of" operator in C, so 300 * 10^(-6) doesn't do what you think. Use the pow function in math.h. ^ is the bitwise "exclusive or" operator.
3. I don't know what that "floating dot" character is in C_D·((PI·D*D) , but it's not a valid operator in C.

What those three things tell me is that you don't really know C and haven't made much of an effort to learn it. You basically tried to wrap matlab code in a main function and threw in a couple header files. C is not Matlab. They are different languages with different purposes. Check out some online tutorials (here and elsewhere). Grab a book and start reading, doing the examples along the way.
• 09-12-2011
AndrewHunter
Here we go again...... Any guesses on how many pages this thread will be? I believe the last time we did this we reached 7.
• 09-12-2011
MK27
Quote:

Originally Posted by anduril462
What those three things tell me is that you don't really know C and haven't made much of an effort to learn it.

I'm going to third that opinion.

So you need to learn some C -- rules about numerical types, casting, math functions, etc. Forget your immediate project and instead write something very simple, gradually adding the elements that you think you will need until you understand how they work. When something does not turn out the way you expect, write as short a program as possible testing your premises. This will help you see where you have misunderstood or assumed the wrong thing.
• 09-12-2011
iMalc
Yes you are using a while loop correctly.
You're welcome.
• 09-12-2011
TheBigH
Code:

`D = 300*10^(-6);        // diameter of sand particle in units of meters`
C allows for convenient powers-of-ten notation:

Code:

`D = 300*1.0E-6;        // diameter of sand particle in units of meters`
will do what you want.
• 09-13-2011
grumpy
Quote:

Originally Posted by TheBigH
C allows for convenient powers-of-ten notation:

Code:

`D = 300*1.0E-6;        // diameter of sand particle in units of meters`
will do what you want.

Except that D is of type int in the OP, so that assignment will always set D to zero. Also 300*1.0E-6 is more simply expressed as 300.0E-6 or 3.0E-4.
• 09-13-2011
TheBigH
Quote:

Originally Posted by grumpy
Except that D is of type int in the OP, so that assignment will always set D to zero.

Sure, but it's already been pointed out that D should not be an int and I assumed that had already been fixed.
• 09-14-2011
GReaper
Quote:

Originally Posted by karma_jesus45
The program is supposed to solve for U_T (terminal velocity of a particle falling in water)
Everything can be found except Re and U_T
Re is basically 100 < Re <10000. I TRIED to create a while loop for Re. I am more used to Matlab than C programing, but it needs to be done in C.
The while loop should stop when F_D + F_B = W
After it breaks I want it to display the U_T and Re used
The difficult part is that U_T is used in another equation. I am wondering if I need to take it out of the loop.

I was wondering if you guys can tell me if I am using the while loop correctly?
And if you can tell me how to make it print the U_T and Re values when it is done.

What are you talking about?! According to wikipedia:

Attachment 10865

and

Attachment 10866