Homework 2 (Due 6 pm, Friday, 05/11/2018)

Please submit your homework to your git repo by 6 pm, Friday, 05/11/2018.

Overview

In this homework, you are going to solve very popular mathematical problem called Basel Problem by writing a Fortran program.

The Basel problem asks for the summation of inverse square of all natural numbers, i.e.

(1)\[\sum^{\infty}_{n=1} = \frac{1}{n^2}\]

The solution of this problem remained unsolved for about a century, and finally Euler found the answer as \(\pi^2/6\).

(2)\[\frac{1}{1^2} + \frac{1}{2^2} + \frac{1}{3^2} + \dots = \frac{\pi^2}{6}\]

Note

If you want to see a proof of this answer in geometric view, please check a very nice video.

Direction

Make hw2 directory under homework directory in your git repository, and do the following;

  1. Make a module in a separate file, called param which contains a parameter variable named pi and store the real value of \(\pi\) by using acos(-1.0)

    This value will be used to check our calculation.

  2. Create a separate file which contains a subroutine named calc_basel which takes threshold variable as an input argument, and calculates

    1. a real solution \(\pi^2/6\), by using our param module. i.e.

      use param, only: pi
      real_soln = pi**2.0/6.0
      
    2. a numerical solution by calculating

      (3)\[\frac{1}{1^2} + \frac{1}{2^2} + \frac{1}{3^2} + \dots\]
      1. Since it is an infinite summation, we need an exit condition for do loop. The proper exit condition would be

        (4)\[\text{Error} > \text{Desired threshold}\]

        So the loop will stop if the error is smaller than threshold.

        Specifically, in a code the infinite summation could be expressed as,

        error = 10000.0   ! error should be very large for initial step!
        do while(error > threshold)
           numerical_soln = numerical_soln + ! something
           error = ABS(real_soln - numerical_soln) ! You need to calculate "error" every step
        end do
        
      2. In the loop, prints error every 1000 steps. For example,

        n = 1000    error = 1.0005001666648639E-003
        n = 2000    error = 5.0012502083029986E-004
        n = 3000    error = 3.3338889505518665E-004
        ...
        

        The intrinsic function MOD would be very helpful. (see article-MOD)

    3. After finish the loop, prints;

      1. total number of cycles
      2. numerical solution
      3. error.
  3. In a main driver, take value of threshold variable as an user input, like

    $ ./basel
      enter the desire threshold as a real number:
    

    Thus, you will need to use read statement. see Fortran Input / Output

    After get a threshold value, calculate Basel problem by calling calc_basel subroutine, with threshold variable.

    call calc_basel(threshold)
    
  4. Use this makefile to compile your program. (You may need to edit this makefile)

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    FC = gfortran
    EXE = basel
    FFLAGS = -fdefault-real-8 -fdefault-double-8 -Wall -Wextra -Wconversion
    OBJECTS = main.o param.o calc_basel.o
    
    .PHONY: clean
    
    $(EXE): $(OBJECTS)
    	$(FC) $(FFLAGS) $(OBJECTS) -o $(EXE)
    
    %.o : %.F90
    	$(FC) $(FFLAGS) -c $<
    
    main.o : param.o
    calc_basel.o : param.o
    
    clean:
    	rm -f $(OBJECTS) $(EXE) *.mod
    
  5. Compile the code and run your program with 1.0E-7 threshold value. And save the results in results.txt file.

  6. Finally, you will have five files in your homework/hw2 directory,

    1. main.F90
    2. calc_basel.F90
    3. param.F90
    4. makefile
    5. results.txt
  7. Use the git frequently to save your work, and don’t forget to write your comments in your source codes.