WS06: Derivatives and Euler’s method#
These exercises are indented to give you practice at using the material on numerical approximation and are intended to reinforce the material that was covered in lectures.
Please attempt the worksheet before your tutorial. Support is available in your tutorial or in the Class Team.
Partial solutions are available to check your understanding. Please create Issues and Pull requests with your solutions.
Part a (pen and paper warm up)#
1. interpretation of derivatives: speed = rate of change of displacement#
Say you spent
Your walking speed is the rate of change of ______?.
Your walking speed
2. Compute the derivative by definition#
Definition of derivative of a function
If we know the general relation between
3. Approximation of derivatives#
If we do NOT know the general relation between
can you approximate
4. Differential equation (Euler’s method)#
If we do NOT know the general relation between
Use your idea for question 3 and the definition of derivative, given an arbitrary
, to approximate in this differential equation. What you get is just the Euler’s formula.let
, and , use Euler’s method to compute and .
Part b (code implementations and testing)#
5. Implement Euler’s method#
For the same problem in Q4, compute all these 100 function values:
import numpy as np
def Euler_method(t0, d0, dt, n):
"""
Input: initial value d0, time setp dt, and total computational steps n
Output: array d(n+1) with d[0] = d0, d[1] = d(0.01), d[2]=d(0.01) ... d[100] = d(1.00)
"""
d = np.zeros(n + 1)
d[0] = d0
t = np.zeros(n + 1)
for i in range(n + 1):
t[i] = t0 + i * dt
"""
Add a loop to compute d[1] to d[100] using Euler's method
"""
return t, d
6. Test your implementation#
Use the example in Q4 to test your implementation and compare with the exact solution:
# The right-hand side function of the differential equation
def f(t, d):
return t * t - 3.0 * t + d
# The exact solution of the differential equation
def d_exact(t):
return -t * t + t + 1.0
t0 = float(0.0)
d0 = float(1.0)
dt = float(0.01)
n = int(100)
t, d = Euler_method(t0, d0, dt, n)
de = np.zeros(n + 1)
for i in range(n + 1):
de[i] = d_exact(t[i])
import matplotlib.pyplot as plt
fig = plt.figure()
plt.plot(t, d, "-r", label="Euler method")
plt.plot(t, de, "-b", label="Exact solution")
plt.legend(loc="upper left")
plt.xlabel("t")
plt.ylabel("d")
plt.grid()
plt.show()
Part c: Extension#
Derivative is a very important concept non only in math but in all science. We interpret derivative as the rate of change of variable. For example, the derivative of displacement
7. Derivative as the tangent of a curve#
We know
Draw a curve such as
on the x-y plane, say , , so what is the meaning of
As
becomes smaller and smaller… goes to 0, you would get the geometrical meaning of , what’s that?
8. Euler’s method#
Given
Use the above geometrical meaning of the derivative in Q7 and think why Euler’s method can do this job.
Comparing with the rate-of-change interpretation: given your position or displacement
at a particular time , and your speed for all the time, how to compute your position for all the time.