New Jersey Institute of Technology ODE & LE Python Worksheet
Description
1. Find one example from your ChemE courses where you had to solve an ODE (initial value problem).
2. Find one example from your ChemE courses where you had to solve an ODE (boundary value problem).
3. Find one example from your ChemE courses where you had to solve an system of linear equations (not differential, linear algebraic equations)
Write this problems in detail, so that a clear connection between the math and the ChemE context is seen. Solve either IVP or BVP analytically (you better chose the problem which has a closed form solution) and using the corresponding SciPy functions. Plot the comparison for the solutions.
Unformatted Attachment Preview
# -*- coding: utf-8 -*”””
Created on Tue Dec 6 17:06:58 2022
@author: ggor
“””
import numpy as np
from scipy.integrate import solve_bvp
import matplotlib.pyplot as plt
def T_cf(x):
sq = np.sqrt(0.05)
return 200 + 20.4671*np.exp(sq*x) + 79.5329*np.exp(-sq*x)
def fun(x, y):
return np.vstack((y[1], 0.05*(y[0]-200)))
def bc(ya, yb):
return np.array([ya[0]-300, yb[0]-400])
x = np.linspace(0, 10, 11)
y = np.zeros((2, x.size))
res = solve_bvp(fun, bc, x, y)
x_plot = np.linspace(0, 10, 101)
y_cf = T_cf(x_plot)
y_plot = res.sol(x_plot)[0]
fig = plt.figure()
ax = fig.add_subplot()
plt.plot(x_plot, y_cf, linestyle=’–‘, linewidth=4, alpha=0.5,
color=’r’,
label=’Closed form’)
plt.plot(x_plot, y_plot, linestyle=’ ‘, color=’b’, marker=’.’,
label=’Numerical’)
plt.legend()
plt.show()
#!/usr/bin/env python3
# -*- coding: utf-8 -*”””
Created on Tue Dec 6 16:31:22 2022
@author: ggor
“””
import numpy as np
from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt
# y is 2-dimensional
def dydt(t, y):
dy = np.zeros_like(y)
dy[0] = 1.2*y[0] – 0.6*y[0]*y[1]
dy[1] = -0.8*y[1] + 0.3*y[0]*y[1]
return dy
t1 = 0
tf = 20
y0 = [2., 1.] # np.array([2., 1.])
tspan = np.linspace(t1, tf, num=100)
result = solve_ivp(dydt, (t1, tf), y0, t_eval=tspan)
t = result.t
y = result.y
fig = plt.figure()
ax = fig.add_subplot()
ax.plot(t, y[0,:], color=’r’, marker=’.’, linestyle=’-‘,
label=r’$y_1$’)
ax.plot(t, y[1,:], color=’b’, marker=’.’, linestyle=’–‘,
label=r’$y_2$’)
ax.legend()
ax.set_xlabel(r’$t$’)
ax.set_ylabel(r’$y_1, y_2$’)
plt.show()
Purchase answer to see full
attachment
Have a similar assignment? "Place an order for your assignment and have exceptional work written by our team of experts, guaranteeing you A results."