Like Tree1Likes
  • 1 Post By grumpy

Solving a more difficult math equation in c programming

This is a discussion on Solving a more difficult math equation in c programming within the C Programming forums, part of the General Programming Boards category; The program is supposed to solve for U_T (terminal velocity of a particle falling in water) Everything can be found ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    1

    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_TDrho_f) / (mu_f));
    		//U_T=((Re*mu_f)/(D*rho_f));
    		//F_D=C_D((PID*D) / (4))*0.5rho_fU_T*U_T;
    		//F_B=rho_f((PI*pow (D, 3)) / (6))g;
    		W=mg;
    		m=rho_p((PI*pow (D, 3)) / (6));
    		//C_D=((24) / (Re))*pow ((1+0.27Re), 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((PID*D) / (4))*0.5rho_fU_T*U_T;
    			F_B=rho_f((PI*pow (D, 3)) / (6))g;
    			C_D=((24) / (Re))*pow ((1+0.27Re), 0.43)+0.47(1-exp (-0.04*pow (Re, 0.38)));
    			Re++;
    			if (F_D+F_B == W)
    				break;
    		}
    		return 0;
    	}

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,372
    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.
    Last edited by stahta01; 09-12-2011 at 11:45 AM.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,324
    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((PID*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.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    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.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by anduril462 View Post
    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.
    Last edited by MK27; 09-12-2011 at 01:41 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,269
    Yes you are using a while loop correctly.
    You're welcome.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    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.
    Code:
    while(!asleep) {
       sheep++;
    }

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    5,883
    Quote Originally Posted by TheBigH View Post
    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.
    AndrewHunter likes this.
    Right 98% of the time, and don't care about the other 3%.

  9. #9
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Quote Originally Posted by grumpy View Post
    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.
    Code:
    while(!asleep) {
       sheep++;
    }

  10. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    1,602
    Quote Originally Posted by karma_jesus45 View Post
    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:

    Name:  Equation.jpg
Views: 1040
Size:  18.1 KB

    and

    Name:  Equation2.jpg
Views: 749
Size:  23.1 KB
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to do a math equation in c Programming
    By jabber5050 in forum C Programming
    Replies: 6
    Last Post: 09-08-2011, 01:08 PM
  2. quadratic equation solving
    By narendrav in forum C Programming
    Replies: 6
    Last Post: 05-18-2011, 08:49 AM
  3. Rather difficult maths equation
    By Zewu in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-21-2004, 08:31 AM
  4. Equation solving
    By Sang-drax in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 11-24-2002, 01:13 PM
  5. equation solving
    By ajlott_coder in forum C Programming
    Replies: 6
    Last Post: 01-26-2002, 01:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21