def forward_model(m=None, sim_info=None): assert m is not None, "Please define parameters." m = np.exp(m) assert sim_info is not None, "Please define simulation parameters." Nt = sim_info['Nt'] h = sim_info['h'] sampling_rate = sim_info['sampling_rate'] u = np.zeros((5, Nt)) _, drugs, u[:, 0] = model_Rong_init() for i in range(1, Nt): x_old = u[:, i - 1] + h * f_Rong(u[:, i - 1], p=m, drugs=drugs) x_new = Newton(x_old, u[:, i - 1], p=m, drugs=drugs, h=h) k = 0 dev = np.abs(x_old - x_new) while dev[0] > tol and dev[1] > tol and dev[2] > tol: x_old = x_new x_new = Newton(x_old, u[:, i - 1], p=m, drugs=drugs, h=h) dev = np.abs(x_old - x_new) k += 1 u[:, i] = x_new if not (i % 10000): print('Solved at time ', str(i), ' after ', str(k), ' Newton iterations.') idx_obs = np.where(t % sampling_rate == 0)[0].astype(int)[:-1] return u[:, idx_obs], u[0, idx_obs] + u[1, idx_obs] + u[3, idx_obs], u[ 2, idx_obs] + u[4, idx_obs]
def Newton(x_old, u_old, p=para): J_old = Dg(h, Df_Rong(x_old, p=para)) g_old = g(u_old, x_old, h, f_Rong(x_old, p=para)) return x_old - np.dot(np.linalg.pinv(J_old), g_old)
return np.identity(5) - h * Df for idx, eps in enumerate(epsilon_RT_alpha_02): print('Solving for epsilon_RT = ', str(eps)) para, u[:, 0] = model_Rong_init() para[8] = eps @numba.jit(nopython=True) def Newton(x_old, u_old, p=para): J_old = Dg(h, Df_Rong(x_old, p=para)) g_old = g(u_old, x_old, h, f_Rong(x_old, p=para)) return x_old - np.dot(np.linalg.pinv(J_old), g_old) for i in range(1, Nt): guess = u[:, i - 1] + h * f_Rong(u[:, i - 1]) x_old = guess x_new = Newton(x_old, u[:, i - 1], p=para) k = 0 dev = np.abs(x_old - x_new) while dev[0] > tol and dev[1] > tol and dev[2] > tol: x_old = x_new x_new = Newton(x_old, u[:, i - 1], p=para) dev = np.abs(x_old - x_new) k += 1 u[:, i] = x_new if not (i % 10000): print('Solved at time ', str(i), ' after ', str(k), ' Newton iterations.')