Please note that I am very new to python. I have never taken a programming course and I am self teaching when it comes to python, so excuse my verbiage or lack of proficient code.
I have am having trouble trying to vectorize the calculations with respect to the parameters. As you can see, some of my parameter values are arrays that take on multiple values. I looked into vectorizing my results, but again, being fairly new I am unsure how to do that. Any guidance on how I could vectorize the results would be helpful. If there is a more proficient way to do this, maybe 'looping over parameter values?', please share any tips. Thank you.
Below is my code followed by the error I currently get.
Code:
import scipy.integrate import matplotlib.pyplot as plt import numpy as np def ode(t,y): sigma1 = 1/10 sigma2 = 1/4 alpha = 1/10.4 gamma = 1/5 beta = 0.4 f = np.array[0.111, 0.111, 0.121, 0.121, 0.121, 0.121, 0.121, 0.121, 0.121, 0.121, 0.175, 0.175, 0.287, 0.287, 0.287, 0.287, 0.287] h = np.array[0.182, 0.055, 0.055, 0.055, 0.068, 0.068, 0.139, 0.139, 0.139, 0.139, 0.251, 0.251, 0.251, 0.512, 0.512, 0.512, 0.617] dh = np.array[0.002, 0, 0, 0, 0.002, 0.002, 0.009, 0.009, 0.009, 0.009, 0.036, 0.036, 0.036, 0.149, 0.149, 0.149, 0.328] d = np.array[0.001, 0, 0, 0, 0.001, 0.001, 0.004, 0.004, 0.004, 0.004, 0.014, 0.014, 0.014, 0.059, 0.059, 0.059, 0.129] N = np.array[60000,65000,65000,65000,65000,65000,65000,65000,65000,70000,70000,60000,60000,35000,350000,20000] return np.array([-(beta*(y[2]+y[3])/N)*y[0], (beta*(y[2]+y[3])/N)*y[0]-gamma*y[1], f*gamma*y[1]-sigma1*y[2], (1-f)*gamma*y[1]-sigma2*y[3], h*sigma1*y[2]-alpha*y[4], (1-h-d)*sigma1*y[2]+sigma2*y[3]+(1-dh)*alpha*y[4], d*sigma1*y[2]+dh*alpha*y[4]]) t0 = 0 t_bound = 100 y0 = np.array([480000,0,10000,10000,0,0,0]) sol = scipy.integrate.RK45(ode, t0, y0, t_bound) t = [] y = [] while sol.status == "running": t.append(sol.t) y.append(sol.y) sol.step() plt.plot(np.array(t), np.array(y)) plt.legend(("susceptible", "exposed","infectious_s", "infectious_a", "hospitalized", "recovered", "deaths")) plt.xlabel("time") plt.ylabel("cases") plt.show() Error:
f = np.array[0.111, 0.111, 0.121, 0.121, 0.121, 0.121, 0.121, 0.121, 0.121, 0.121, 0.175, 0.175, 0.287, 0.287, 0.287, 0.287, 0.287] TypeError: 'builtin_function_or_method' object is not subscriptable https://stackoverflow.com/questions/65557199/trying-to-vectorize-the-solution January 04, 2021 at 11:06AM
没有评论:
发表评论