Comp Sci Fortran90: DO loop for sequence of numbers

AI Thread Summary
The discussion focuses on optimizing a Fortran90 program that calculates the area under a Gaussian Distribution Curve using Simpson's Rule. The user seeks to replace multiple DO loops with a more efficient method for iterating through a specific sequence of strip values (n). Suggestions include using a 1D integer array to store the strip values and looping through this array to simplify the code. The user confirms that the proposed solution worked effectively, enhancing the program's efficiency. The conversation highlights the importance of code optimization in numerical methods.
SalfordPhysics
Messages
68
Reaction score
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
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.
 
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.
 
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.
 
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:

Similar threads

Replies
4
Views
5K
Replies
7
Views
2K
Replies
1
Views
2K
Replies
7
Views
3K
Replies
3
Views
1K
Replies
10
Views
2K
Back
Top