Please tell me why if I replace the line printf("%lf %lf\n", u1, u2); , then the cycle ends under the condition specified in the if, and if this line is not fixed, then the cycle continues indefinitely:

 double originalF1(double); double originalF2(double); double f1(double, double, double); double f2(double, double, double); int main() { int a = 1, b = 3; double u1 = 2, u2 = exp(1.0), h = (double)(ba)/10, x = a; do { u1 = u1 + h*f1(x, u1, u2); u2 = u2 + h*f2(x, u1, u2); printf("%lf %lf\n", u1, u2); if (x >= b && fabs(u1 - originalF1(x)) <= 0.0001 && fabs(u2 - originalF2(x)) <= 0.0001) break; else if (x >= b) { x = a; h /= 2; u1 = 2; u2 = exp(1.0); } x += h; } while (true); return 0; } double originalF1(double x) { return 2 * x; } double originalF2(double x) { return exp(x); } double f1(double x, double u1, double u2) { return u1 / x - u2 / exp(x) + 1; } double f2(double x, double u1, double u2) { return u1 / (2 * x) + u2 - 1; } 
  • one
    and the compiler does not swear at %lf %lf ? - Alexey Shimansky
  • @ Alexey Shimansky, I'm sorry, I’ve been here recently, a personal question - why didn’t you issue this message as an answer (the first version of your replica)? I just want to understand how to act in this case myself - ale
  • @ale because it is not an answer, but a clarification on the issue. If you want to clarify something with the author of the question, you need to write in the comments, if you know the answer to the question - the answer, which is logical ........ and what is the first version? - Alexey Shimansky
  • one
    @JohnVein It seems to me you have a memory overflow. and not sickly ... and in case of mistakes you do not do anything and everything hangs - Alexey Shimansky
  • one
    I will insert my guess, it may be stupid, but suddenly ... the instruction printf ("% lf% lf \ n", u1, u2); - it is possible to implicitly change u1 and u2 (for example, rounding), and when calculating exp () large arguments, this can change the result (glitches at the overflow boundary) and accordingly violate the convergence of the calculated expression (no decrease in error occurs during the next iteration) - ale

1 answer 1

The fact is that printf requires a lot of resources relative to the rest of the code. You have over a million iterations. Accordingly, the code runs many times longer.

Alternatively, you can insert any condition before printf so that only certain iterations are printed.

 int counter = 0; do { u1 = u1 + h*f1(x, u1, u2); u2 = u2 + h*f2(x, u1, u2); counter++; if (counter%100000 == 0)printf("%lf %lf %i\n", u1, u2, counter);