3076 Exam

Semester 1 2016


Please complete the following boxes.




Other Names:

Lecturer: S. Olver

June 2015

Time allowed: Two hours

**This paper is CONFIDENTIAL.**
**Only the course website may be accessed:**
**All external links have intentionally been removed: NO access to outside resources, or email, is permitted.**
**In addition, do NOT access any files on the machine, either inside or outside your user directory.**

This paper is worth 60% of the maximum mark for this course.

The questions are marked with an A, B or C. To earn a score of 65/100, complete all C questions. To earn a score of 85/100, complete all B and C questions. To earn a score of 100/100, complete all A, B and C questions.

If not all questions are completed, A questions will be weighted more than B questions, which will be weighted more than C questions.

All answers to questions must be entered in one or more cells immediatly below each question, and preceding the following question.

Default cells are provided below each question. If desired, additional cells can be added by clicking the + button in the toolbar.

The answers should be given as working Julia code. You are allowed to use all inbuilt Julia routines, as well as those in the included packages (ODE and PyPlot).

Pseudo-code, non-working answers and textual explanations will recieve partial credit. Please comment your code so that you may recieve partial credit for wrong or incomplete answers.

Any questions that do not ask for code can be answered in comments, or as a separate plain text cell. You can make a cell plain text by selecting NBConvert from the drop-down menu that says Code, after the ⟳ button. Try changing the following cell to NBConvert:

In [ ]:
This is a bit of plain text explaining the solution to the code, because I couldn't figure out how to answer the question in Julia.  I don't want it to be parsed as Julia as it will give an error message, and not wrap correctly.

It may be useful to restart the kernel between questions, by clicking the ⟳ button. Otherwise, you have issues arising from redefining variables as functions if you reuse the variable/function name.

Before we begin, test that the following 3 boxes of Julia commands are working correctly:

In [ ]:
In [ ]:
using PyPlot
In [ ]:
using ODE



Question 1 (C) Complete the following function that creates a new string consisting of the characters of a, followed by "hi", followed by the characters of b:

# INPUT: two strings a and b
# OUTPUT: a string
function histring(a::AbstractString,b::AbstractString)
 # TODO: concatenate a, "hi", and b
In [ ]:

Floating point arithmetic

Question 2 (C)

  1. What is the smallest normal Float64?
  2. What are its bits?
In [ ]:

Vectors and matrices

Question 3 (C) Complete the function

#INPUT: an Integer n
#OUTPUT: A Vector{Float64}
function primevector(n::Integer)
    #TODO: create a zero vector v of length n, set prime entries to 1, and return v

that returns a vector of length n whose prime entries are equal to 1, and non-prime entries are equal to zero.

Hint: isprime(x) returns whether a number is prime or not.

In [ ]:

Linear Algebra

Question 4 (C) Calculate the vector x that solves A*x=b, for

A=[1  2  3 ; 
       4  5  6 ; 
       7  8  10]
In [ ]:

Question 5 (B) For the matrix A above, use the svd of A to solve A*x=b, without using \.

In [ ]:

Condition numbers and Error Analysis

Question 6 (C) What is the worst case relative error of ${\rm fl}(x)$, where $x$ is a real number and ${\rm fl}$ is the function that a real number to the closes Float64?

In [ ]:

Question 7 (B)

  1. Calculate numerically the relative forward error in approximating a single step of Newton iteration for $x^2 - 2 = 0$: $$x_k - {x_k^2 -2 \over 2 x_k}$$ By its Float32 calculation $$x_k \ominus ((x_k \otimes x_k -2) \oslash (2x_k)),$$ where $x_k=3$. You can assume $x_k$ is given as a Float32 and treat Float64 calculations as exact.

  2. Why is the use of $2x_k$ instead of $2 \otimes x_k$ and $a-2$ instead of $a \ominus 2$ correct?

In [ ]:


Question 8 (C)

  1. Complete the function

    ## INPUT: a function f and an integer n
    ## OUTPUT: a Float64 approximating the integral of f using the lhrule rule
    function lhrule(f::Function,n::Integer)
     #TODO: return the lefthand rule to approximate

    that implements the lefthand rule, so that $$\int_0^1 f(x) dx \approx {1 \over n} \sum_{k=0}^{n-1} f(x_k)$$ where $x_k \triangleq kh$ for $h=1/n$.

  2. Show that the method approximates $\int_0^1 e^{x} dx$ for $n=10000$ with an error less than 1E-4.

In [ ]:

In [ ]:

Question 9 (B)

  1. Estimate the rate of convergence for lhrule approximating $\int_0^1 e^x dx$ by finding an $\alpha$ so that the error decays like $Cn^\alpha$.
  2. How does this rate of convergence compare to the right-hand rule and Trapezium rule?

Hint: it may help to do a loglog plot.

In [ ]:

Question 10 (A)

  1. Consider the function $$f(x)=x^2(1-x)^2exp(x).$$
    Give a formula for the asymptotic behaviour (not bound!) of the error in approximating by the Trapezoidal rule. That is, find a $C$ and $\alpha$ so that $$\int_0^1 f(x) dx- h \left({f(0) \over 2} + \sum_{k=1}^{n-1} f(x_k) + {f(1) \over 2}\right) \sim C n^{\alpha}.$$

  2. Demonstrate that your expression is correct using a plot to show that the relative error tends to zero. You can use any inbuilt routines to determine the exact integral.

Hint: You can use the following routine from lectures:

function trap(f,n)

In [ ]:

The Discrete Fourier Transform

Question 11 (B) Consider the even function $f(\theta) = \cos({\cos \theta})$.

  1. Calculate coefficients $c_k$ so that $$f(\theta) \approx \sum_{k=0}^5 c_k \cos k \theta.$$
  2. Show that the approximation has an error close to 2E-6 when evaluating at $\theta=0.6$.

Hint: Remember that you can use any inbuilt routines, or the trap function above.

In [ ]:

ODE and PDE Solving

Question 12 (C)

  1. Approximate numerically

    $$y'(t) = (1-y(t))y(t), y(0)=0.5$$
    for $0 \leq t \leq 1.$
  2. Plot the approximate $y$.

Hint: Remeber that you can use any inbuilt ODE solver.

In [ ]:

Question 13 (A)

  1. Approximate the solution the heat equation
$$u_t = u_x + u_{xx}$$

with Dirichlet boundary conditions $u(0) = u(1) = 0$, using Backward Euler in time and the initial condition $u_0(x)=x(1-x)exp(x)$. Plot the approximate solution at time $t = 1$. Use n=100, $\Delta x=1/n$ and $\Delta y$ sufficiently small so that the approximation is stable.

  1. Plot the approximate solution at time $t = 1$.

It may help to use the following functions from lectures

function D(h,n)
    for k=1:n
In [ ]: