I just started programming some 1 days ago, I have the program complete except for the following problem-- iteration. I consider myself pretty engineering savy when it comes to Excel and have created a spreadsheet to calculate this already, but I would like to create a simple program to do it as well.

Just as a reference my Excel formula (modified for my C++ names instead of cells)is:

This code uses iteration and circular references within Excel to get a value to 1-E6 accuracy or 1000 iterations.

For those unfamiliar, the equation deals with friction loss through a pipe and outputs a friction factor to then be used in a pressure drop calculation. All of the required values are already either inputted or calculated. The problem is the same factor on both sides of the equation (f), see below.

The original equation is:

Code:

1/sqrt(f) = -2 * log10((e/3.7D) + ((2.51/Re*sqrt(f))))

Since my experience in C++ is lacking I was looking for someone to "show me the way," I am not used to doing math the way C++ handles it. I currently have most of the pieces defined, but I know I do not have this setup correctly.

I think I would limit the program to 500 iterations and/or 1-E6 accuracy. If I can provide anymore detail I will.

Regards,
McJ

09-02-2011

tabstop

All your t variables aren't needed. When you put your Excel formula into C++ you need to remember that x^2 is represented by pow(x,2) (and that if the power is in the denominator, it's really a negative power) and that common log is represented by log10 rather than log, so you would have

Code:

x = pow(-2*log10(rrough/3.7 + 2.51/(rnum*sqrt(x+1e-300))), -2);

inside a big loop.

09-02-2011

mcjeff

Quote:

Originally Posted by tabstop

All your t variables aren't needed. When you put your Excel formula into C++ you need to remember that x^2 is represented by pow(x,2) (and that if the power is in the denominator, it's really a negative power) and that common log is represented by log10 rather than log, so you would have

Code:

x = pow(-2*log10(rrough/3.7 + 2.51/(rnum*sqrt(x+1e-300))), -2);

inside a big loop.

Interesting, it looks like I have done a bunch of defining variables when that was un-needed. Just another thing to put down for the ol'learning curve.

Now when you mention a big-loop, I am assuming I will be defining iteration and tolerances and have the loop run until one of those two conditions are met. I will do some searching on loops to learn about them.

If you want to, I suppose. What you have written will never end, so you maybe want to come up with a way to make it stop.

09-02-2011

rags_to_riches

Code:

} while (tpfc = tpfc2);

= is for assignment, == is for comparison

09-02-2011

iMalc

What would be the point in looping while they are equal anyway? Don't you want to loop until two things are equal, i.e. while the two things are not equal?

Or more usefully, loop until the two things are close enough to equal, within a certain tolerance.

09-06-2011

mcjeff

Indeed you are all correct.

Quote:

iMalc
What would be the point in looping while they are equal anyway? Don't you want to loop until two things are equal, i.e. while the two things are not equal?

Or more usefully, loop until the two things are close enough to equal, within a certain tolerance

I would prefer a tolerance of 1-E6. But am still unsure as to how to make that happen. The goal of the formula is to use iteration to solve for the factor (f) until they both are more or less equal.

Quote:

If you want to, I suppose. What you have written will never end, so you maybe want to come up with a way to make it stop.

The program "solves" and will end just fine currently (whether or not it is supposed to I am not sure). However, the end result is incorrect. No matter the other input values it outputs -1.

09-06-2011

tabstop

You probably want to go while fabs(old - new) < 1e-6 then. Currently your loop will only stop when you value is zero. Probably what is happening is you assign tpfc2 a value of zero, compute with it to get a value of -1, then re-assign zero back to the old value while stopping the loop.

09-06-2011

mcjeff

1 Attachment(s)

Quote:

Originally Posted by tabstop

You probably want to go while fabs(old - new) < 1e-6 then. Currently your loop will only stop when you value is zero. Probably what is happening is you assign tpfc2 a value of zero, compute with it to get a value of -1, then re-assign zero back to the old value while stopping the loop.