for (int i= 0; i<10; i++)
{
phi0 = phi0 * (L / L0);
X0 = R * Math.Sin(2 * phi0) * Math.Cos(phi0);
L0 = X0 * (1 + (Math.Pow(Math.Tan(phi0), 2)) / 10 - (Math.Pow(Math.Tan(phi0), 4)) / 72 + (Math.Pow(Math.Tan(phi0), 6)) / 208);
if ((Math.Abs(L0 - L)) < (0.003 * Math.Sqrt(L)))
break;
}
Usually after 2,3 iterations the condition is met, but I need a message to show the number of iterations after which the condition is met and also if its exceeds the total amount of 10.
If there is something to explain fell free to ask.
Thank you!
Move the declaration of
ioutside the loop and you can use it’s value afterwards. Remember that the value ofiwill be one less than the number of iterations used since you’re starting at 0 — in fact since you don’t useiin any of the expressions in the loop, you may want it to from from 1 to 10 instead of 0 to 9.Note you can also make a small optimization to make things faster. The compiler might do it for you, but you can make sure by storing the intermediate calculation of
tan(phi0)in a local varaiable.In fact, if this gets executed many times, you may want to carry it one step further and try computing the powers of
tan(phi0)in stages as well instead of callingPoweach time. I’m making the assumption here that multiplication is faster than calling thePowmethod, which seems reasonable, but you’ll want to test it to make sure. There’s also no reason to compute the error bound each time.