1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Fortran90: DO loop for sequence of numbers

  1. Dec 9, 2014 #1
    1. The problem statement, all variables and given/known data
    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

    2. Relevant equations

    3. 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
    k = 1,2 !n = 88,84
    n = n-4
    CALL evalArea((-1)*sigma,sigma,area,n)
    PRINT*, 'n=',n, 'area=',area
    END DO
    k = 1,2 !n = 76,68
    n = n-8
    CALL evalArea((-1)*sigma,sigma,area,n)
    PRINT*, 'n=',n, 'area=',area
    END 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=',area

    All help appreciated.
  2. jcsd
  3. Dec 9, 2014 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    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.
  4. Dec 9, 2014 #3
    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.
  5. Dec 9, 2014 #4


    User Avatar

    Staff: Mentor

    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.
  6. Dec 9, 2014 #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: Dec 9, 2014
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted