Mastering Mathematics Smartly by Wee Wen Shih

A unique self-help website that provides comprehensive coverage of mathematics at A-level & beyond, written in a student-friendly style.

Computational Mathematics

This course deals with solving mathematical problems using numerical techniques or approximation methods as well as provides an introduction to the use of such techniques in a selected series of problems.

The main prerequisite is basic calculus of functions of one variable and some knowledge of the use of the computer.

The main objective of the course lies in achieving an understanding of the methods of solving mathematical problems numerically with the aid of a computing tool called MATLAB, which is a powerful technical computing software.

The following content will be covered:

1 - Basic MATLAB and Programming in MATLAB
2 - Solutions of non-linear equations
3 - Interpolation and Approximation
4 - Numerical integration
5 - Monte Carlo method and Simulations

For a start, interested students may wish to download this useful set of notes I found on the Internet.

Do feel free to visit the discussion forum at: forums.delphiforums.com/weews

Tutorial 0

Some good programming tips to begin with:
1. Know the format of instructions you are using.

2. Write the instructions one sentence at a time and make sure it compiles correctly.

3. Balance your parentheses.

In a typical programming language, we will come across the following types of programming statement:
1. simple,

2. decision,

3. repetitive,

4. file-processing,

5. function (a package of instructions).

Tutorial 1

Summary:
1. Quadratic formula: The roots of ax^2 + bx + c = 0 are x = \frac{-b \pm \sqrt{b^2-4ac}}{2a}.

2. Solution to a system of linear equations: \textbf{A}.\textbf{x} = \textbf{b} \Rightarrow \textbf{x} = \textbf{A}^{-1}.\textbf{b}.

3. Hero's formula: please read this link.

4. Syntax errors are errors associated with wrong instruction formats. These are easy to find because the computer will highlight them. An example of a syntax error is sin^2 x (for \text{sin}^2x) when it should have been (sin (x)).^2.

5. Logic errors are errors associated with incorrect (or unexpected) output values as a result of faulty reasoning. These are never easy to identify, because the instructions may be entered correctly in terms of format. An example of a logic error is A^(-1) (for \textbf{A}^{-1}) when it should have been inv(A).

Tutorial 2

Summary:
1. We will move on to write MATLAB instructions that allow the computer to make decisions (via if-then-else) and repeat certain actions (via for- and while-loops).

2. General form of if-statement:
if logical expression
  statements
else
  statements
end

if logical expression
   statements
elseif logical expression
   statements
else
   statements
end

3. General form of for-loop:
for variable = first : last
  statements
end

4. General form of while-loop:
while logical expression
  statements
end

5. One should make a plan of the logical steps with a flowchart before coding the program. Writing the steps (as comments) at the start of coding helps a programmer to develop the detailed instructions for the program later. For example, the steps and skeletal instructions for Q2 would look like this:

% Step 1: Ask user for celsius temperature.
C = input(...);

% Step 2: Convert celsius to fahrenheit via a formula.
F = (9/5)*C + 32;

% Step 3: Print both temperature values.
fprintf(...);

6. Formatting specifications include %d (for integers), %s (for strings) and %f, %g (for decimals).

Tutorial 3

Explanation to Q9
First, we plan the main steps to solve Q9:

% Step 1: Ask user for size (n) of square matrix.
n = input(...);

% Step 2: Declare matrix and assign zeros to all entries in it.
A = zeros(n, n); % Recall that we came across this function in tutorial 2.

% Step 3: Assign entry values depending on the conditions given in the problem.

% Step 4: Print matrix.

Next, we expand the details of step 3. We will require nested for loop (as suggested by the hint) and if-elseif statement:

for i = 1:n % Loop to go through each row.
    for j = 1:n % Loop to go through each column.
        if (i == j)
            A(i, j) = 4;
        elseif (abs(i - j) == 1)
            A(i, j) = 1;
        end
    end
end

Then, we elaborate the details of step 4. We will again use nested for loop:

for i = 1:n % Loop to go through each row.
    for j = 1:n % Loop to go through each column.
        fprintf('%2d', A(i, j)); % Print the column entries for a row.
    end
    fprintf('\n'); % Move the cursor to the next row.
end

Explanation to Q10
What conclusion can you draw about r_i?

We can tackle this with some mathematics.
Given that r_i = \frac{f_i}{f_{i-1}}, it can be rewritten as r_i = \frac{f_{i-1} + f_{i-2}}{f_{i-1}} and simplified to r_i = 1+\frac{f_{i-2}}{f_{i-1}} = 1+\frac{1}{r_{i-1}}.
Suppose the ratio converges to a limit l, then l satisfies the equation l = 1 + \frac{1}{l}, which simplifies to the quadratic equation l^2 - l - 1 = 0.
Solving the equation yields l = \frac{1+\sqrt{5}}{2} (i.e. the value of the golden ratio).

Tutorial 4

Summary:

1. It is useful to draw a flowchart to help us recall the steps of Newton's method.

2. Before we apply Newton's method, it is important to form the function \text{f}(x).

    In Ex. 4.2 Q3, several possibilities of f are:

    (a) x-\sqrt[m]{3} (why is this function unsuitable?),

    (b) \text{ln}\:x - \frac{\text{ln}\:3}{m},

    (c) x^m-3.

    Do check, by running the program in the text, to see if the other two functions would produce the root after several iterations.

3. For a solution to be determined to an accuracy of n significant figures, we may need to define \varepsilon = 10^{-(n+1)}.

Comments on assignment 1

Question 1
(b) Value of the limit was not a constant.

(c) Wrong value of i, because limit was not a constant.
     Used a for-loop in the program.
     Logic error.

Question 2
(b) Did not use output to explain how the fare charges were computed.

(c) Did not give a complete explanation.

Note to TG2 & TG3: If I have penalised you in Q2(a), please bring along your script and see me to add one mark at the next lesson. Sorry for the inconvenience.

Tutorial 5

Preliminaries

1. This is the flowchart of Secant method:

2. Example 4.4: Suggested working to be shown when performing computations by hand.

    Given: x_0=1,\:x_1=2,\:\varepsilon=0.01, \text{f}(x)=x^3-x-1.
    Applying the Secant method, we have the following iterations.

    |x_1-x_0| = 1 > 0.01, \text{f}(x_0)=-1,\:\text{f}(x_1)=5, x_2=1.17.
    So there is a root within the interval [1,\:2], by IVT.

    |x_2-x_1| = 0.83 > 0.01, \text{f}(x_2)=-0.57, x_3=1.25.

    |x_3-x_2|=0.08>0.01, \text{f}(x_3)=-0.30, x_4=1.34.

    |x_4-x_3|=0.09>0.01, \text{f}(x_4)=0.07, x_5=1.32.

    |x_5-x_4|=0.02>0.01, \text{f}(x_5)=-0.02, x_6=1.32.

    |x_6-x_5|=0<0.01.
    Hence the root is 1.32 (correct to 2 decimal places).

3. Flowchart showing the Fixed Point Iteration method:

4. Example 4.5: Suggested working to be shown when performing computations by hand.

    Given: x_0=1, x_{n+1}=\frac{1}{2}\left(x_n + \frac{3}{x_n}\right).
    We calculate the first five iterations using six decimal places.

    x_1 = 2.

    x_2 = 1.75.

    x_3 = 1.732143.

    x_4 = 1.732051.

    x_5 = 1.732051.
    Hence the root is 1.732051 (correct to 6 decimal places).

5. Corollary 4.3 is an important result that determines whether an iteration method will converge or not. When the derivative of g at alpha is 1, no conclusion can be drawn.

Exercise 4.4

1. In Q4, we saw that iterative method (b) has |\text{g}'(0.5)| = 0.6065 and iterative method (c) has |\text{g}'(0.5)| = 0.1967. We then concluded that method (c) converges faster than method (b). The reason for this conclusion comes from the proof of Theorem 4.2 (2) on page 67 of text, which has the following result: |\alpha - x_n| \leq \lambda^n |\alpha - x_0|, where \lambda \equiv \max_{a \leq x \leq b} |\text{g}'(x)| < 1. Thus, the smaller the value of \lambda the faster the convergence.

2. In Q5(b), we consider |\text{g}'(x_0)| and attempt to show that it will be greater than 1, to explain why the scheme will not converge.

3. In Q6, the choice of c can be made based on the consideration of \left|\text{g}'(\sqrt{7})\right| < 1, from which we solve an inequality involving c.

Comments about order of convergence

1. We look at the error expression when we wish to analyse order of convergence.

2. On page 47 of text, we see the error expression of bisection method:
    |\alpha - c_n| \leq \frac{1}{2}(b_n - a_n),
    from which we say that the order of convergence is 1.

3. On page 54 of text, we see the expression (4.6) of Newton's method:
    \alpha - x_{n+1} = \left(\frac{-\text{f}\:''(\xi_n)}{2\text{f}\:'(x_n)}\right)(\alpha - x_n)^2,
    from which we say that the order of convergence is 2, since the error in x_{n+1} varies proportionally to the square of the error in x_n.

4. In Q4 of Exercise 4.3 on page 60 of text, we see the expression (4.11) of Secant method:
    \alpha - x_{n-1} \approx x_n - x_{n-1},
    from which we say that the order of convergence is 1. In actual fact, the order of convergence is precisely \frac{1+\sqrt{5}}{2} \approx 1.62 (the value of golden ratio). Read this link for more details.

I wish all muslim students a very happy Hari Raya Adil Fitri!

Tutorial 6

Preliminaries

1. In Q5 of Exercise 5.1, do plan your MATLAB program first before coding. A suggested outline is given below.

% (1) Initialise all required row matrices.

% (2) Compute coefficients of L0, L1, L2.

% (3) Compute coefficients of P(x).

% (4) Compute P(x) and f(x) for all data points of x.

% (5) Print P(x), f(x) and E(x).

% (6) Plot graph of E(x) against x.

2. For Q1(b), (c) and Q2, you may need to solve a system of equations to find the unknowns.

Preparation for Quiz 1

Here is a set of practice questions for you to work on. Jiayou!

PDF version of page content

Download here.