سؤال

I am using Fortran to iteratively solve a system of equations but do not want to know the result after every iteration. My actual system is much larger and requires around 100,000 iterations for the solution but please consider this simple example which carries out 100 iterations.

PROGRAM ITER

DATA X1,X2,X3/.0,.0,.0/



DO 20 K = 1,100

    X1 = 10+3*X2 - X3
    X2 = (4 - 2*X1 - X3)/5.
    X3 = (-13 + X1 - X2)/(-2.)


    WRITE (*,10)K,X1,X2,X3


    10 FORMAT (' ',I3,3(F8.1))
    20 CONTINUE
END PROGRAM ITER

As it it, the program reports the result after every iteration but, I would like to have the values of X1, X2 and X3 reported back after every 10 iterations.

I have tried adding a nested IF statement into the DO loop but I get an error from the compiler. I am wondering if it possible to print the values of X1, X2 and X3 when K (the iteration step) is a multiple of 10. Therefore is it possible to divide K by 10 and then whenever the result is an integer, the values of X1,X2 and X3 are output?

I hope that is clear. Thanks for your help.

هل كانت مفيدة؟

المحلول

if (mod(k,10)==0) WRITE...

Just use the mod function(the remainder or modulo) in the if condition to execute the write only when k is divisible by 10.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top