Real step-size in do loop-fortran

  • Comp Sci
  • Thread starter Antonija
  • Start date
  • #1
18
0

Homework Statement



I'm trying to find out is it possible to make a real do loop in fortran. As I read online, it's possible, and there is written how to do it but when I copy/paste it into my fortran editor, it' doesn't work.
At the moment I'm writing the code for minimize a function of a single variable, and one of the things I need is the real step do loop.

Homework Equations




The Attempt at a Solution


This is how my part of code looks like:

real:: a,b
real::delta=0.0001
do i=a,b, delta
variablesvalues(1)=x+(i-1)*delta
call {some function}
answer=evaluate(......)
end do

and this is the code for which is written that it's example for real loop:
program xytab
implicit none
!constructs a table of z=x/y for values of x from 1 to 2 and
!y from 1 to 4 in steps of .5
real :: x, y, z
do x = 1,2
do y = 1,4,0.5
z = x/y
print *, x,y,z
end do
end do
end program xytab


in both examples I get the same error: Deleted feature: Step expression in DO loop at (1) must be integer.

I'm using Fortran 90.

Also, is it possible to have only step-size real, and other integer type?
 

Answers and Replies

  • #2
SteamKing
Staff Emeritus
Science Advisor
Homework Helper
12,796
1,668
There's a couple of things here.
1. If you're going to post source code, please use CODE tags to enclose the code. These allow for the preservation of spacing, etc. of the code as written by you.

2. In your variable declarations, you have declared a and b as real variables, but in the first DO-loop, the index variable i is assumed to be an INTEGER type (variables starting with I-N are automatically declared INTEGERs unless the program is otherwise instructed.), which is one reason why the compiler returns the error message.

3. In theory, real index variables for DO-loops may be programmed, but you should be careful using this approach. Floating point arithmetic always is subject to round-off error, so your real-valued DO-loops may not terminate properly when you expect them to.
 
  • #3
DrClaude
Mentor
7,388
3,648
3. In theory, real index variables for DO-loops may be programmed
In the Fortran 90 standard, do loops with real indices were declared obsolete, and eliminated in Fortran 95.

To the OP: loop with an integer index, and calculate the real equivalent inside the loop:
Fortran:
do i = 1, 1000000
   real_i = real(i) * 1e-5
   ...
end do
will loop from 1 to 10 in steps of 0.00001
 
  • #4
18
0
In the Fortran 90 standard, do loops with real indices were declared obsolete, and eliminated in Fortran 95.
Actually, at the moment I'm not sure if I have fortran 90 or fortran 95, because my linux crashed a month ago and then I had to build everything new... However, I tried it with do while loop and it worked....

It's like this:
Code:
real:: a, x, delta= 0.0001
do while(x<=a)
x=x+delta
call [function]
...
end do
 

Related Threads on Real step-size in do loop-fortran

  • Last Post
Replies
0
Views
3K
Replies
1
Views
1K
Replies
8
Views
1K
Replies
4
Views
628
Replies
1
Views
2K
Replies
11
Views
2K
Replies
0
Views
1K
Replies
5
Views
1K
Replies
3
Views
727
Top