Fortran90: DO loop for sequence of numbers

Click For Summary

Discussion Overview

The discussion revolves around modifying a Fortran90 program to efficiently implement a DO loop for calculating the area under the Gaussian Distribution Curve using Simpson's Rule. Participants explore how to handle a specific sequence of numbers representing the number of strips in the integration process.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant describes their existing program that calculates the area under the Gaussian curve but expresses a desire to shorten the multiple DO loops used for different sequences of strips.
  • Another participant questions the meaning of 'number of strips' in the context of Simpson's Rule and suggests that the Gaussian curve's symmetry could simplify the calculations.
  • A later reply clarifies that the Gaussian curve is indeed symmetric, but the participant believes this is not relevant to their current implementation.
  • One suggestion involves creating a 1D integer array to hold the values for the number of strips, allowing for a more efficient loop structure.
  • Another participant mentions simplifying their code using IF-THEN-ELSE statements but acknowledges the proposed array method as potentially superior.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding the implementation of the DO loop and the meaning of the number of strips. There is no consensus on the best approach, as multiple suggestions and methods are discussed.

Contextual Notes

The discussion includes unresolved questions about the interpretation of the number of strips and the implications of the Gaussian curve's symmetry on the integration process. The proposed solutions vary in complexity and efficiency.

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 ·
Replies
4
Views
5K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 32 ·
2
Replies
32
Views
5K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
6K