Real step-size in do loop-fortran

  • Comp Sci
  • Thread starter Antonija
  • Start date
In summary, the code does not work with a real loop because the index variable i is assumed to be an integer, and the code for a real loop uses a while loop.
  • #1
Antonija
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 xytabin 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?
 
Physics news on Phys.org
  • #2
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
SteamKing said:
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
DrClaude said:
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
 

1. What is a do loop in FORTRAN?

A do loop in FORTRAN is a programming construct that allows a set of instructions to be repeated a certain number of times. It is commonly used for iterative calculations and looping through arrays or lists.

2. How do I specify the step-size in a do loop in FORTRAN?

To specify the step-size in a do loop in FORTRAN, you can use the "step" keyword followed by the desired step-size after the do loop statement. For example, "do i=1,10,2" would create a loop that starts at 1, increments by 2, and ends at 10.

3. What determines the real step-size in a do loop in FORTRAN?

The real step-size in a do loop in FORTRAN is determined by the data type of the loop variable. If the loop variable is an integer, the step-size will also be an integer. If the loop variable is a real number, the step-size can be a real number as well.

4. Can the step-size in a do loop be negative?

Yes, the step-size in a do loop can be negative. This allows for the loop to count backwards or decrement the loop variable instead of incrementing it. However, it is important to ensure that the loop condition will eventually be met to avoid an infinite loop.

5. How can I change the step-size in a do loop while the loop is running?

In a do loop in FORTRAN, the step-size cannot be changed while the loop is running. The step-size is determined and specified before the loop begins and remains constant throughout the loop. If you need to change the step-size, you will need to exit the loop and start a new one with the desired step-size.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Programming and Computer Science
Replies
4
Views
617
  • Engineering and Comp Sci Homework Help
Replies
12
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
941
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
Back
Top