AMATH Quiz 6
Scalar Differential Equations
Recognize differential equations and IVPs and rearrange them into a form that can be solved using forward Euler, backward Euler, and solve_ivp.
Differential Equation
An equation that involves derivatives of one or more dependent variables with respect to one or more independent variables
Initial Value Problem (IVP)
A differential equation plus the value of the unknown function at some particular time
General form
Forward Euler
IVP
Forward Euler Solver
def forward_euler(ode, IC, t): """ t = np.arange(0, T+dt/2, dt) V = np.zeros(t.size) Inputs: - ode = lambda or python function, of the form ode(t,V) t is independent variable, V is dependent variable - IC = the initial condition - t = times at which we want the solution. Extract dt from this. """ V = np.zeros(t.size) # Setup solution array V[0] = IC # Define the initial condition dt = t[1]-t[0] # Calculate dt # Use a for loop for Forward Euler for i in range(t.size - 1): V[i+1] = V[i] + dt * ode(t[i], V[i]) return VBackward Euler
IVP
solve_ivp
Format
sol = scipy.integrate.solve_ivp(ODE, [start, end], [IC])
sol.t
The t values at which the approximate solution is found.
sol.y[num]
The approximate solution at the corresponding t value.
Recognize that “solutions” found numerically are approximations to a discretization of the true solution of an IVP.
Recognize the explicit (Forward) and implicit (Backward) Euler Methods.
Use the forward and backward Euler methods to solve simple scalar IVPs.
Recognize implicit methods for solving IVPs.
Recognize that implicit methods are advantageous for stiff IVPs.
Recall the order of accuracy of the forward and backward Euler methods.
First-order accurate
Use solve_ivp to solve scalar (1-dimensional) IVPs.
Systems of Differential Equations
Use solve_ivp to solve systems of IVPs.
Steps
Define both ODEs
Put the ODEs into an ODE System
Package them in terms of one dependent-variable input
Use solve_ivp on the new ODE
Example
dxdt = lambda x, y: 2*x - x*y
dydt = lambda x, y: -0.5*y + 0.2*x*y
ode_system = lambda x, y: np.array([dxdt(x,y), dydt(x,y)])
ode = lambda t, z: ode_system(z[0], z[1])
t_span = np.linspace(0, 15, 1000)
sol = solve_ivp(ode, [0, 15], np.array([6, 2]), t_eval = t_span)Plot IVP solutions from systems of ODEs.
ax.plot(t, x) or ax.plot(t, y)
Interpret plots of IVP solutions to describe physical scenarios.
Convergence as x approaches 0
Forward Euler only works with stiff
Create and interpret phase portraits for 2D systems of IVPs.
Steps
Extract time values and z
Extract x and y from z
ax.plot(x, y)
Connecting phase portrait means a cycle
Ex:
dxdt = lambda x, y: 2*x - x*y
dydt = lambda x, y: -0.5*y + 0.2*x*y
ode_system = lambda x, y: np.array([dxdt(x,y), dydt(x,y)])
ode = lambda t, z: ode_system(z[0], z[1])
t_span = np.linspace(0, 15, 1000)
sol = solve_ivp(ode, [0, 15], np.array([6, 2]), t_eval = t_span)
t = sol.t
z = sol.y
x = z[0]
y = z[1]
fig, ax = plt.subplots()
ax.plot(x, y)Basic Python Functions
lambda
Ex:
f = lambda x: x**2 + 4
def
Format:
def function_name(parameters):
# Function body
return value # Optionalrange
ex:
range(start, stop, step)
step is number of steps
NumPy
array
Ex:
arr1 = np.array([1, 2, 3])
zeros
Ex:
z1 = np.zeros(5)
linspace
Format:
np.linspace(start, stop, number)
Number is the number of steps/samples
Ex:
arr = np.linspace(0, 10, 50)
arange
Format:
np.arange(start, stop, step)
Step is the size of the steps
Ex:
arr = np.arange(1, 10, 2)
Math functions (abs, sine, cosine, log, exp, etc.)
np.abs()
Absolute value
np.sin()
sine
np.cos()
cosine
np.log()
Natural logarithm
np.exp()
e to the x power
Scipy
optimize.fsolve
Format
fsolve(function, array)
It finds the root(s) of a function
Matplotlib
plot
fig, ax = plt.subplots()
ax.plot()
plt.show()
plt.savefig()