MTH3007b Weekly Problems 1

Original Documents: Problem Sheet / GitHub Solutions

Vibes: Just quick definitions then the two methods outlined in the lecture - pretty simple!

Used Techniques:

  • Error/relation definitions.
  • Implementing explicit/implicit Euler methods.
  • Error calculations.

1.1. Definitions

Question

What is meant by the following:

  1. Local truncation error.
  2. Global truncation error.
  3. Order of an algorithm.
  4. Finite difference method.
  • Local Truncation Error: The difference between the numerical and analytical solution after a single timestep.
  • Global Truncation Error: The difference between the numerical and analytical solution after all timesteps.
  • Order of an Algorithm: How the global truncation error varies with the integration step; for order , then halving the step size reduces error by a factor of .
  • Finite Difference Method: Approximating a derivative by setting a value for , instead of integrating.

1.2. Definition Comparison

Question

What is the difference between an implicit and explicit relation?

  • An explicit relation has an isolated dependent variable.
  • An implicit relation does not have an isolated dependent variable.

1.3. Formula Implementation (Explicit Euler Method)

Question

Implement the explicit Euler method in Python to solve the ODE .

Use variables for the coefficients, setting them to , , , , and integrate with:

First, we should do all required imports.

Python
Output

Then we can write our ODE as a Python function, just to make things a bit neater…

Python
Output

Then actually implement the Explicit Euler method…

Python
Output

Then we can specify each of the variables for our specific ODE…

Python
Output

Which allows us to finally get the two solutions!

Python
Output

Resulting in two very different answers: approximately 0.043 and 6.2, respectively.


1.4. Formula Implementation (Implicit Euler Method)

Question

Implement the implicit Euler method in Python to solve the ODE from the previous question.

Using the same cached code from the previous question, we can jump straight into the new method function…

Python
Output

Which is quite a bit more mathematical than the last, given the implicit relation. This time, we can jump straight to the two solutions…

Python
Output

Resulting in two very similar answers, rounding to 0.43.


1.5. Error Calculation Against Analytical Solution

Question

For the same ODE from the previous two questions, compare the errors between the implicit and explicit Euler methods and plot each solution against time, along with the analytical solution:

We can now develop a function to find the analytical solution, directly from the question…

Python
Output

Hence, we may then create a function to compute the error across all solutions:

Python
Output

Now we need to compute the time values for our numerical solutions and the analytical solution:

Python
Output

We can now compute the errors for all methods:

Python
Output

Let’s now display the error stats…

Python
Output

Already, we can see a difference between the implicit and explicit methods, as expected. Let’s now plot everything to confirm, first importing the relevant plotting libraries:

Python
Output

Then creating our plotting functions:

Python
Output

Finally, we can now generate the plots:

Python
Output

These plots clearly demonstrate how the implicit Euler method is more stable with larger timesteps, while the explicit Euler method becomes inaccurate with the coarser discretization (). The implicit method’s superior stability is crucial for stiff ODEs like this one.