Sorey
Sorey

Reputation: 135

Repeating a step in a Fortran loop

I'm trying to write a Fortran 90 program to carry out Euler's method to solve ode's using an adaptive time step.

I have an if statement inside of a do while loop, in which I check that the error at each iteration of the code is less than a certain tolerance. However, if it is not less than certain tolerance, I must change a certain value (the step size) and carry out the calculation again to get a new error to compare with the tolerance.

It looks something like (and forgive me this is my first time using this website):

do while (some condition)
  (Get an approximation to the ODE with various subroutine calls)
  (Calculate the error)
if (error < tol)
  step = step/2
else
   step = 2*step
(Something that will return to the top of my do while loop)
end if
end do

Say for example, I had do while (i < 4), where i starts at 1, and my error was not less than my tolerance, I would have to repeat the calculation again for i=1 with a new step size.

I hope this makes sense to those of you who read this. If you need any clarification, let me know.

Upvotes: 2

Views: 1059

Answers (1)

Because there is no explicit counter in the do while loop unlike in a normal do i=1,... loop. you can just use cycle to start a new iteration. It will be the same as repeating the current iteration. But the condition will be evaluated again. If it shouldn't be evaluated, you would have to use go to or restructure your code.

Or another loop nested inside the main loop might be better, but that probably counts as the restructuring mentioned above. Depends what is the condition, how you change step and i and how the tolerance depends on the step and i.

Upvotes: 1

Related Questions