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.
The solution of this problem remained unsolved for about a century, and finally Euler found the answer as \(\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;
Make a
module
in a separate file, calledparam
which contains a parameter variable namedpi
and store the real value of \(\pi\) by usingacos(-1.0)
This value will be used to check our calculation.
Create a separate file which contains a
subroutine
namedcalc_basel
which takesthreshold
variable as an input argument, and calculatesa real solution \(\pi^2/6\), by using our
param
module. i.e.use param, only: pi real_soln = pi**2.0/6.0
a numerical solution by calculating
(3)¶\[\frac{1}{1^2} + \frac{1}{2^2} + \frac{1}{3^2} + \dots\]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
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)
After finish the loop, prints;
- total number of cycles
- numerical solution
- error.
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 / OutputAfter get a threshold value, calculate Basel problem by calling
calc_basel
subroutine, withthreshold
variable.call calc_basel(threshold)
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
Compile the code and run your program with
1.0E-7
threshold value. And save the results inresults.txt
file.Finally, you will have five files in your
homework/hw2
directory,main.F90
calc_basel.F90
param.F90
makefile
results.txt
Use the
git
frequently to save your work, and don’t forget to write your comments in your source codes.