Lab 1
T/F: Is python free to use?
Python is open-source software and free to use
T/F: We’re using Python 2
False: Python 2 has been officially retired. Most environments use python 3 now
T/F: Python can be run on both windows and macOS if installed correctly
True: python is cross–platform and can run on Windows, macOS, linux, etc
What is smallest increment that regular Python float can represent?
Approximately: 2.2 x 10^-16: this is the machine epsilon for floating point arithmetic in python, typically represented as np.finfo(float).eps in NumPy
What is the error in computer calculation caused by rounding at its smallest increment level called?
Round-off error: This is the error caused by approximating a number due to the finite precision of the representation\
T/F: 1.0 + 1e-17 in python returns 1.0
True: Adding a very small number (1e-17) to 1.0 doesn’t change 1/0 due to the precision limits of floating point arithmetic
Name two root finding algorithms
Newton Raphson Method
Bisection Method
Lab 2
T/F: A numpy array is the same as a Python list
False: NumPy arrays are different from Python lists; they are homogeneous and provide more functionality for numerical operations
T/F: you can store different data types within the same NumPy array
False: NumPy arrays are homogeneous; all elements must be of the same data type
How to write ln(2^4) in Python NumPy syntax?
import numpy as np
result = np.log(2**4)
How to write log(10^-3) in Python NumPy syntax?
import numpy as np
result = np.log10(10**-3)
What do the three arguments of np.arange() and np.linspace() each represent?
np.arange(start, stop, step): Start value, stop value (exclusive), step size
np.linspace(start, stop, num): start value, stop value (inclusive), number of samples
What is the first and last element of np.arange(11,2,-2)?
import numpy as np
arr = np.arange(11, 2, -2)
first = arr[0] # 11
last = arr[-1] # 3
What is the first and last element of np.linspace(2,15,20)?
import numpy as np
arr = np.linspace(2, 15, 20)
first = arr[0] # 2.0
last = arr[-1] # 15.0
Lab 3
Name 3 numerical differentiation methods
Forward difference
Backward difference
Central difference
Define Forward difference approximation, backward difference approximation, and central difference approximation
def forward_diff(f, x, h):
return (f(x + h) - f(x)) / h
def backward_diff(f, x, h):
return (f(x) - f(x - h)) / h
def central_diff(f, x, h):
return (f(x + h) - f(x - h)) / (2 * h)
State one difference between forward and backward difference approximations
Forward difference uses future point, backward difference uses past point
Forward differences uses f(x+h), while backward difference uses f(x-h)
Which point do you lose when you perform forward difference approximation with raw data?
Last point: because f(x+h) goes beyond the last point
Which point do you lose when you perform backward difference approximation with raw data?
First point: because f(x-h) goes before the first point
Which point do you lose when you perform central difference approximation with raw data?
First and last points: because it uses f(x+h) and f(x-h)
Lab 4
Name 4 numerical methods for solving ODEs
Euler’s method
Runge-kutta methods
Adams-Bashforth methods
Finite difference methods
What is the difference between numerical differentiation methods numerical methods for solving ODES?
Numerical differentiation approximates derivatives, numerical ODE methods solve differential equations: Differentiation focuses on derivatives, while ODE methods find the solution function
T/F: When numerically solving ODES, we are given the ODEs
True: The ODE must be known to solve it numerically
T/F: When numerically solving ODES, we are not given the initial value of the ODES
False: Initial values are typically provided
T/F: When numerically solving ODES, we are solving for the original function
True: The goal is to approximate the function that solves the ODE
Define forward Euler’s method and backward Euler’s method
def forward_euler(f, y0, t0, tf, h):
t = t0
y = y0
while t < tf:
y += h * f(t, y)
t += h
return y
def backward_euler(f, y0, t0, tf, h):
from scipy.optimize import fsolve
t = t0
y = y0
while t < tf:
y = fsolve(lambda y_next: y_next - y - h * f(t + h, y_next), y)
t += h
return y
The advantage of forward Euler is that it gives an explicit update equation, so it is easier to implement in practice. On the other hand, backward Euler requires solving an implicit equation, so it is more expensive, but in general it has greater stability properties.
T/F: Midpoint method is simply forward Euler’s method with step size cut in half.
False: The midpoint method uses a different approach to approximate the midpoint of the interval
Explicit vs implicit methods
Explicit method: next value is a function of known quantities
Implicit method: Next value involves solving an equation
T/F: Implicit methods are usually more computationally expensive than explicit methods
T/F: Implicit methods are usually more stable than explicit methods
True: Implicit methods tend to be more stable
For forward Euler’s method, backward Euler’s method, midpoint method, and RK4 method, which are implicit which are explicit?
Forward Euler’s method: explicit
Backward euler’s method: implicit
Midpoint method: Explicit
RK4 method: explicit
Lab 5
Know how to rewrite a higher oder ODE into a system of first order of ODES. State the four steps
1. Define new variables for each derivative
2. Rewrite the original ODE as a system of first order ODEs
3. Write the system in matrix form if possible
4. Solve the system using numerical methods
How many auxiliary variables are needed to rewrite a nth order ODE?
n-1 auxiliary variables: this converts the nth order ODE into nth first-order ODEs
T/F: We cannot use forward Euler’s method with a system of first order ODE
False: Forward Euler’s method can be used for systems of first-order ODEs
Understand the four key parameters of scipy.integrate.solve_ivp()
from scipy.integrate import solve_ivp
def example_ode(t, y):
return -y
result = solve_ivp(example_ode, [0, 10], [1], method='RK45')
Fun: function to evaluate the derivative, t_span: interval of integration y0: initial conditions, method: integration method to use
Lab 6
Define absolute error, relative error, sum of squared error, mean squared, and root mean squared error
true_value = 3.14
approx_value = 3.1
absolute_error = abs(true_value - approx_value)
relative_error = absolute_error / abs(true_value)
sse = (true_value - approx_value)**2
mse = sse / n
rmse = np.sqrt(mse)
What are the order of the forward difference method, backward difference method, and central difference method?
Forward difference: First order
Backward difference: First order
Central difference: Second order
Define global and local errors
Global error: Accumulated error over all steps
Local error: Error at a single step
What are the local and global error’s order of forward Euler’s method, backward Euler’s method, midpoint method, and RK4 method?
Forward difference: First order
Backward difference: First order
Central difference: Second order
RK4 method: Fourth order
For forward difference method, if I make the step size 5 times before, the error will be how many times before?
5 times: error is proportional to step size
For central difference method, the current error is 10, if I make the step size ⅕ before, the error now is?
0.4 times: central difference method has second-order accuracy, so error scales with the square of the step size
T/F We usually report the local error of the numerical methods
false : global error is usually more informative
What is the global error of a numerical method with nth order local error?
Order n-1: global error is typically one order less than local error
For RK4 method, the error now is 2, if I make step size 10 times before, what is the global error now?
0.00002: RK4 is fourth order, so error scales with h^4
Lab 7
Define phase portrait, trajectory, and slope field
Phase portrait: a graphical representation of trajectories in the phase plane
Trajectory: the patch that a system follows through the phase space
Slope field: a graphical representation showing the slope of a differential equation at various points