.. _homework2: 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. .. math:: \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 :math:`\pi^2/6`. .. math:: \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 :math:`\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 a. a real solution :math:`\pi^2/6`, by using our ``param`` module. i.e. .. code-block:: fortran use param, only: pi real_soln = pi**2.0/6.0 b. a numerical solution by calculating .. math:: \frac{1}{1^2} + \frac{1}{2^2} + \frac{1}{3^2} + \dots i. Since it is an infinite summation, we need an *exit condition* for `do` loop. The proper exit condition would be .. math:: \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, .. code-block:: fortran 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 ii. In the loop, prints ``error`` **every 1000 steps**. For example, .. code-block:: console 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 `_) c. After finish the loop, prints; i. `total number of cycles` ii. `numerical solution` iii. `error`. 3. In a main driver, take value of ``threshold`` variable as an user input, like .. code-block:: console $ ./basel enter the desire threshold as a real number: Thus, you will need to use ``read`` statement. see :ref:`ch02-fortran-io` After get a threshold value, calculate Basel problem by calling ``calc_basel`` subroutine, with ``threshold`` variable. .. code-block:: fortran call calc_basel(threshold) 4. Use this makefile to compile your program. (**You may need to edit this makefile**) .. literalinclude:: ./codes/makefile :language: make :linenos: 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, a. ``main.F90`` b. ``calc_basel.F90`` c. ``param.F90`` d. ``makefile`` e. ``results.txt`` 7. Use the ``git`` frequently to save your work, and don't forget to write your comments in your source codes.