Water discharge#
Introduction#
Wait a few seconds until Python interaction ready! is shown in the top.
After that follow the instructions in the Grasple Exercise.
On this webpage the initial-value problem
\[\begin{split}
\left\{\begin{array}{rcl}\dfrac{dQ}{dt} &=&p-qQ^{\beta}, \\\\Q(0)&=&0.\end{array}\right.
\end{split}\]
is considered.
This webpage solves this initial-value problem numerically, using numpy, and then uses pyplot to display the obtained approximated solution.
import micropip
await micropip.install("ipympl")
import matplotlib
if not hasattr(matplotlib.rcParams, '_get'):
matplotlib.rcParams._get = matplotlib.rcParams.get
%matplotlib widget
import sympy as sp
import numpy as np
import matplotlib.pyplot as plt
def Water_discharge(beta,p,q):
def ode(t,Q,beta,p,q):
rhs = p - q*Q**beta
return rhs
def solve_my_ivp(beta,p,q):
t0 = 0
A = np.sqrt(p/q)
B = p/A
tend = np.arctanh(0.99)/B*2
N = 1000
tout = np.linspace(t0,tend,N+1)
dt = np.mean(np.diff(tout))
Q0 = [0]
Qout = np.zeros_like(tout)
for n in np.arange(0,N):
k1 = dt*ode(tout[n],Qout[n],beta,p,q)
k2 = dt*ode(tout[n]+dt/2,Qout[n]+1/2*k1,beta,p,q)
k3 = dt*ode(tout[n]+dt/2,Qout[n]+1/2*k2,beta,p,q)
k4 = dt*ode(tout[n]+dt,Qout[n]+k3,beta,p,q)
Qout[n+1] = Qout[n]+1/6*(k1+2*k2+2*k3+k4)
return tout, Qout
Qs = (p/q)**(1/beta)
t,Q= solve_my_ivp(beta,p,q)
plt.close('all')
fig = plt.figure()
plt.title('$\\beta = '+str(beta)+', p='+str(p)+',q='+str(q)+'$')
plt.plot(t,Q,label='$Q$')
plt.plot(t,(Qs-0.01)*np.ones_like(t),label='$Q_s$-0.01',linestyle='dashed')
plt.plot(t,(Qs+0.01)*np.ones_like(t),label='$Q_s$+0.01',linestyle='dashed')
plt.xlim(0,np.max(t))
plt.xlabel('$t$')
plt.ylabel('$Q(t)$')
plt.legend()
plt.show()
fig.canvas.toolbar_visible = True
pass
INPUT CELL#
In the cell below you should put the values of the parameters you see in Grasple.
Please note: Each attempt in Grasple contains different values for the parameters.
After you run the cell the results of the calculations are shown with the set parameter values.
If you point your mouse below the figure, controls will appear that you can use to pan and zoom.
''' Set the given values in the following lines '''
beta = 1.4
p = 20
q = 8
''' The next line performs the calculations and shows the results. '''
Water_discharge(beta,p,q)