Fortran90: DO loop for sequence of numbers

In summary, the program finds the area under the Gaussian Distribution Curve between ±σ using Simpsons Rule. Modify the program to investigate the effect of the number of strips. Do this by using a DO loop in the main program for the following sequence of number of strips (n): n-2, n-4, n-6, n-8, n-12,n-16,n-24,n-32,n-48,n-64,n-96.
  • #1
SalfordPhysics
69
1

Homework Statement


A program finds the area under the Gaussian Distribution Curve between ±σ using Simpsons Rule.
Modify the program to investigate the effect of the number of strips.
Do this by using a DO loop in the main program for the
following sequence of number of strips (n); n-2, n-4, n-6, n-8, n-12,n-16,n-24,n-32,n-48,n-64,n-96

Homework Equations



The Attempt at a Solution


My program DOES work fine, but it has a long line of DO loops for bits of the sequence. I want to know if there is a way to massively shorten this part down.
(n) starts at 100 and there is first a print of that before implementing the sequence and printing on each iteration;

CALL evalArea((-1)*sigma,sigma,area,n) !n = 100
PRINT*, 'n=',n, 'area=',area
DO k = 1,4 !n = 98, 96, 94, 92
CALL evalArea((-1)*sigma,sigma,area,n)
n = n-2
PRINT*, 'n=',n, 'area=',area
END DO
DO
k = 1,2 !n = 88,84
n = n-4
CALL evalArea((-1)*sigma,sigma,area,n)
PRINT*, 'n=',n, 'area=',area
END DO
DO
k = 1,2 !n = 76,68
n = n-8
CALL evalArea((-1)*sigma,sigma,area,n)
PRINT*, 'n=',n, 'area=',area
END DO
DO
k = 1,2 ! n = 52, 36
n = n-16
CALL evalArea((-1)*sigma,sigma,area,n)
PRINT*, 'n=',n, 'area=',area
END DO
n = n-32 ! n = 4
CALL evalArea((-1)*sigma,sigma,area,n)
PRINT*, 'n=',n, 'area=',areaAll help appreciated.
 
Physics news on Phys.org
  • #2
It's not clear what 'number of strips' means in the context of using Simpson's Rule. Are you talking about the number of ordinates in the interval -σ ≤ x ≤ σ?

Why do you use all these different loops?

Is your Gaussian curve symmetric about x = 0? If it is, you can use this fact to calculate only half the area and multiply by 2.
 
  • #3
SteamKing said:
It's not clear what 'number of strips' means in the context of using Simpson's Rule. Are you talking about the number of ordinates in the interval -σ ≤ x ≤ σ?
Why do you use all these different loops?
Is your Gaussian curve symmetric about x = 0? If it is, you can use this fact to calculate only half the area and multiply by 2.

As I mentioned, I have the program running perfectly fine for a single integration with n=100, where n is the number of strips between -σ ≤ x ≤ σ. Yes it is symmetric about x = 0 but this is not important for my case.

I now need to implement a loop in the MAIN program that will repeat the whole process for a sequence of values n.
So first iteration is the original running of n=100.
Then is n = 98, n = 96, n = 64, n = 92 (n=n-2 part)
Then comes n = 88, n = 84 (n=n-4 part)

As you will see on the RHS of the code, I have put a !note as to which part each of the loops corresponds to. As before, I am looking to increase the efficiency and write this in a shorter, neater, more coherent manner. basically, I need some way to implement that sequence, which unfortunately is not geometric or arithmetic.
 
  • #4
A simple way would be to set up a 1D integer array that contains the values of m to calculate n - m, then loop over an index and take the value of n using the m's from the array.
 
  • #5
Thanks Dr! I simplified using IF-THEN-ELSE statements but your approach seems better. I will give it a go now.

It worked great! Thanks again.
 
Last edited:

1. What is a DO loop in Fortran90?

A DO loop in Fortran90 is a type of control structure that allows you to repeat a specific set of instructions for a certain number of times. It is commonly used for iterating over a sequence of numbers or performing a task a specific number of times.

2. How do I use a DO loop to iterate over a sequence of numbers in Fortran90?

To use a DO loop for a sequence of numbers in Fortran90, you first need to define the starting and ending values of the sequence and the increment or decrement value. Then, you can use the DO keyword followed by the index variable, an equals sign, and the start, end, and increment values. Finally, add the loop body between the DO and END DO statements.

3. Can I use a DO loop for a sequence of non-numeric values in Fortran90?

Yes, you can use a DO loop for a sequence of non-numeric values in Fortran90 by using the CHARACTER data type. You can define an array of characters and use a DO loop to iterate over the elements of the array.

4. How do I exit a DO loop in Fortran90?

You can exit a DO loop in Fortran90 using the EXIT or CYCLE statements. The EXIT statement will immediately terminate the loop, while the CYCLE statement will skip to the next iteration without executing the remaining code in the loop body.

5. Are there any limitations to using a DO loop in Fortran90?

Yes, there are a few limitations to using a DO loop in Fortran90. The index variable used in the DO loop must be an integer, and it cannot be modified within the loop. Additionally, the number of iterations must be known before the loop is executed, and the DO loop cannot be nested within another DO loop.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
5K
  • Programming and Computer Science
Replies
16
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Math Proof Training and Practice
2
Replies
61
Views
6K
Back
Top