I almost feel bad taking up space on this blog with this stuff, but I would really like this c++ program to be used for something besides a lame grade. If anyone needs to use the Newton-Raphson method to determine the friction factor in the von Karman fluids equation, using a Reynolds number of your choice, this is for you.

#include

#include

#include

using namespace std;

//Function prototypes

double f1(double reynolds, double friction);

double f2(double friction);

int main ()

{

//Open the file that will receive data:

ofstream output;

output.open("lab5.txt");

//Define variables

double reynolds, friction, error, residual, derivative;

int (i);

//Prompt user for Reynolds number, and friction factor guess

cout << "Please enter the Reynolds Number (Re) to be used in the von Karman equation:" << endl;

cin >> reynolds;

cout << "Please enter your initial guess for the friction factor (f):" << endl;

cin >> friction;

//Run the von Karman equation to test:

//cout << f1(reynolds,friction) << "," << f2(friction);

//Use the Newton-Raphson Method to determine the roots of the von Karman equation

//Use a for loop to do multiple iterations

for (i=1; i<21; i++)

{ double frictionnew;

//Assign functions to equations inside the loop

residual=f1(reynolds,friction);

derivative=f2(friction);

//Newton-Raphson

frictionnew=friction-residual/derivative;

//Percent relative error

error=abs((frictionnew-friction)/(frictionnew));

//Reassign friction

friction=frictionnew;

//Print data to screen

cout << "For i=" << i << ", " << "Re=" << reynolds << ", " << "f(f)=" << residual << ", " << "f'(f)=" << derivative << ", " << "f=" << friction << ", " << "e=" << error << endl;

//Print data to file Lab5.txt

output << "lets see if this works" << i << reynolds << residual << derivative << friction << error << "lets see if this works";

//Stipulate iteration on error value

if(error < .00001)

{ break;

}

//Stop iteration if derivative gets too close to zero

if(abs(derivative) < .001)

{break;

cout << "That guess did not work. Please try again." << endl;

}

//Inform user if guess was inefficient

if(i==20&&error > .0001)

{cout << "The estimated friction factor value is not sufficient to estimate a root. Please try again." << endl;

}

}

//Tell user CLEARLY what the solution is

cout << endl;

cout << "The best estimated value for the friction factor is:" << friction << endl;

output.close();

return 0;

}

//Define functions prototyped above

double f1(double reynolds, double friction)

{

double solution1;

solution1=4*log10(reynolds*pow(friction,.5))-0.4-1/(pow(friction,.5));

return solution1;

}

double f2(double friction)

{double solution2;

solution2=2/(friction*log(10))+1/(2*pow(friction,1.5));

return solution2;

}

//Holy cow, that is a loooooong program.

## 2 comments:

C++ makes me sick.

Do you have to use C++?

jay

Somehow this makes me happy...

Maybe it's because now you know what my life is like when I'm at school.

Post a Comment