# Covid-19: From model prediction to model predictive control

## Calibration of the age-structured deterministic model (2)

*Original code by Ryan S. McGee. Modified by T.W. Alleman in consultation with the BIOMATH research unit headed by prof. Ingmar Nopens.*

Copyright (c) 2020 by T.W. Alleman, BIOMATH, Ghent University. All Rights Reserved.

The original code by Ryan S. McGee implements an SEIRS infectious disease dynamics models with extensions to model the effect of population structure, social distancing, testing, contact tracing, and quarantining of detected cases. The model was implemented using two commonly used frameworks: 1) a deterministic framework represented by a set of ordinary differential equations and 2) a stochastic implementation of these models on dynamic networks. We modified the original implementation by Ryan McGee at its source to account for additional Covid-19 disease characteristics. The deterministic model was extended to model the effect of age-age group interactions, represented by an interaction matrix $N_c$. This was done in analogy to the recently published work of Plem et al (1). The modified implementation of Ryan McGee was then integrated with our previous work and allows to quickly perform Monte Carlo simulations, calibration of model parameters and the calculation of *optimal* government policies using a model predictive controller. A white paper and souce code of our previous work can be found on the Biomath website (2).

(1) https://www.thelancet.com/journals/lanpub/article/PIIS2468-2667(20)30073-6/fulltext

(2) https://biomath.ugent.be/covid-19-outbreak-modelling-and-control

#### Load required packages

```python
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import Image
from ipywidgets import interact,fixed,FloatSlider,IntSlider,ToggleButtons
import pandas as pd
import datetime
import scipy
import coronaHelper2 as cH
from scipy.integrate import odeint
import matplotlib.dates as mdates
import matplotlib
import scipy.stats as st
import networkx
import models
from gekko import GEKKO
```

#### Define the necessary clinical and testing parameters

```python
# -----------------------
# Define model parameters
# -----------------------

# Clinical parameters
zeta = 0 # re-susceptibility parameter (0 = permanent immunity)
dsm = 14 # length of disease for asymptotic (SM) infections
dm = 14 # length of disease for mildly symptomic infections
dhospital = 9.1 # average time from symptom onset to hospitalisation for H and C infections
dh = 21 # average recovery time for heavy infection
mc0 = 0.49 # mortality for critical cases when they receive care
ICU = 1900 # number of available ICU beds in Belgium

# Testing parameters
totalTests = 0
theta_S = 0 # no. of daily tests of susceptibles
theta_E = 0 # no. of daily tests of exposed
theta_SM = 0 # no. of daily tests of SM infected
theta_M = 0 # no. of daily tests of M infected
theta_R = 0 # no. of daily tests of recovered patients
phi_S = 0 # backtracking of susceptibles
phi_E = 0 # backtracking of exposed
phi_SM = 0 # backtracking of supermild cases
phi_R = 0 # backtracking of recovered cases
psi_FP = 0 # odds of a false positive test
psi_PP = 1 # odds of a correct test
dq = 14 # length of quarantine for false positives
```

## Calibration of disease transmission rate $\beta$

#### Load data

We calibrate to the number of ICU beds taken until March 20th (=index -20).

```python
columns = ['hospital','ICU','dead']
hospital = np.array([[58,97,163,252,361,496,634,837,1089,1380,1643,1859,2152,2652,3042,3717,4138,4524,4920,4995,5376,5552,5678,5735,5840,6012,5688]])
ICUvect= np.array([[5,24,33,53,79,100,130,164,238,290,322,381,474,605,690,789,867,927,1021,1088,1144,1205,1245,1261,1257,1260,1276]])
dead = np.array([[3,4,4,10,10,14,21,37,67,75,88,122,178,220,289,353,431,513,705,828,1011,1143,1283,1447,1632,2035,2240]])
index=pd.date_range('2020-03-13', freq='D', periods=ICUvect.size)
data = np.concatenate((hospital,ICUvect,dead),axis=0)
data = np.transpose(data)
data_belgie=pd.DataFrame(data,index=index, columns=columns)
```

#### Load and visualise the Belgian interaction matrix $N_c$

```python
# source: https://github.com/kieshaprem/covid19-agestructureSEIR-wuhan-social-distancing/tree/master/data
Nc_home = np.loadtxt("Belgium/BELhome.txt", dtype='f', delimiter='\t')
Nc_work = np.loadtxt("Belgium/BELwork.txt", dtype='f', delimiter='\t')
Nc_schools = np.loadtxt("Belgium/BELschools.txt", dtype='f', delimiter='\t')
Nc_others = np.loadtxt("Belgium/BELothers.txt", dtype='f', delimiter='\t')
Nc_all = np.loadtxt("Belgium/BELall.txt", dtype='f', delimiter='\t')
initN = np.loadtxt("Belgium/BELagedist.txt", dtype='f', delimiter='\t')

data = [Nc_all,Nc_work,Nc_schools,Nc_home,Nc_others]
titles = ['total','work','schools','home','others']
x = np.linspace(0,75,16)
y = np.linspace(0,75,16)
X, Y = np.meshgrid(x, y)

plt.figure()
fig, axes = plt.subplots(nrows=1, ncols=5)
fig.set_size_inches(24,3.6)

i = 0
for ax in axes.flat:
    im = ax.pcolor(x,y,data[i], vmin=0, vmax=2,cmap='Blues')
    if i == 0:
        ax.set_ylabel('age (years)')
    ax.set_title(titles[i])
    ax.set_xlabel('age (years)')
    i = i+1

fig.colorbar(im, ax=axes.ravel().tolist(),label='daily contacts (-)')
fig.savefig('BELinteractPlot.svg', dpi=100,bbox_inches='tight')
```

```python
Nc = Nc_all # Business-as-usual interaction
betaZonderIngrijpen=[]
```

#### Perform fit

```python
# -------------------------------
# Parameters of fitting algorithm
# -------------------------------
monteCarlo = True
n_samples = 50
maxiter=30
popsize=5
polish=True
disp = True
bounds=[(0.01,0.05),(1,60)]
idx=-20
print(index[idx])
idx = idx+1
data=np.transpose(ICUvect[:,0:idx])
method = 'findTime'
modelType = 'deterministic'
checkpoints=None
fitTo = np.array([8]) #positions in output of runSimulation that must be added together, here: CH

# -----------
# Perform fit
# -----------
estimate = cH.modelFit(bounds,data,fitTo,initN,Nc,zeta,dsm,dm,dhospital,dh,mc0,ICU,theta_S,theta_E,theta_SM,theta_M,theta_R,totalTests,psi_FP,psi_PP,dq,phi_S,phi_E,phi_SM,phi_R,monteCarlo,n_samples,method,modelType,checkpoints,disp,polish,maxiter,popsize)
betaZonderIngrijpen.append(estimate[0])
# print(estimate)
```

#### Visualise fit

```python
# -----------------------
# Fitted model parameters
# -----------------------
estimate = [0.03215578, 26.61816719]
beta = estimate[0]
extraTime = estimate[1]
simtime=data.size+int(extraTime)-1
method = 'none'

# inital condition
initN = initN
initE = np.ones(Nc.shape[0])
initSM = np.zeros(Nc.shape[0])
initM = np.zeros(Nc.shape[0])
initH = np.zeros(Nc.shape[0])
initC = np.zeros(Nc.shape[0])
initHH = np.zeros(Nc.shape[0])
initCH = np.zeros(Nc.shape[0])
initR = np.zeros(Nc.shape[0])
initF = np.zeros(Nc.shape[0])
initSQ = np.zeros(Nc.shape[0])
initEQ = np.zeros(Nc.shape[0])
initSMQ = np.zeros(Nc.shape[0])
initMQ = np.zeros(Nc.shape[0])
initRQ = np.zeros(Nc.shape[0])

# --------------
# Run simulation
# --------------
simout = cH.simModel(initN,beta,Nc,zeta,dsm,dm,dhospital,dh,mc0,ICU,theta_S,theta_E,theta_SM,theta_M,
                     theta_R,totalTests,psi_FP,psi_PP,dq,phi_S,phi_E,phi_SM,phi_R,initE, initSM, initM, initH, initC,
                    initHH,initCH,initR,initF,initSQ,initEQ,initSMQ,initMQ,initRQ,simtime,monteCarlo,
                     n_samples,method,modelType,checkpoints)

# -----------
# Plot result
# -----------
t=pd.date_range('2020-03-13', freq='D', periods=data.size)
tacc=pd.date_range('2020-03-13', freq='D', periods=data.size+int(extraTime))-datetime.timedelta(days=int(extraTime)-1)
fig=plt.figure(1)
plt.figure(figsize=(7,5),dpi=100)
plt.scatter(t,data_belgie.iloc[:idx,1],color="black",marker="v")
plt.scatter(t,data_belgie.iloc[:idx,2],color="black",marker="o")
plt.scatter(t,data_belgie.iloc[:idx,0],color="black",marker="s")

plt.plot(tacc,np.mean(simout['HH']+simout['CH'],axis=1),'--',color="green")
plt.fill_between(tacc,np.percentile(simout['HH']+simout['CH'],95,axis=1),
                 np.percentile(simout['HH']+simout['CH'],5,axis=1),color="green",alpha=0.2)

plt.plot(tacc,np.mean(simout['CH'],axis=1),'--',color="orange")
plt.fill_between(tacc,np.percentile(simout['CH'],95,axis=1),
                 np.percentile(simout['CH'],5,axis=1),color="orange",alpha=0.2)

plt.plot(tacc,np.mean(simout['F'],axis=1),'--',color="red")
plt.fill_between(tacc,np.percentile(simout['F'],95,axis=1),
                 np.percentile(simout['F'],5,axis=1),color="red",alpha=0.20)
plt.legend(('Hospital (model)','ICU (model)','Deaths (model)'),loc='upper left')

plt.xlim(pd.to_datetime(tacc[24]),pd.to_datetime(tacc[-1]))
plt.title('Belgium',{'fontsize':18})
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
plt.gca().xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%d-%m-%Y'))
plt.setp(plt.gca().xaxis.get_majorticklabels(),
         'rotation', 90)
plt.ylabel('number of patients')

plt.savefig('belgiumFit.svg',dpi=100,bbox_inches='tight')
```

## Calibrate social distancing parameter

By fitting our model to the number of ICU beds occupied during the initial days of the outbreak, we obtained a reliable estimate of $\beta$. This parameter is a disease characteristic and represents the odds of contrapting the disease when coming into contact with an infected person. All contacts in our model are random (assumption of homogeneous mixing) and the disease is assumed to have the same characteristics for all ages. The contact matrices allow us to simulate the effect of concrete policies such as schools closure, working at home, etc. However, when simulating the model with everyone being at home (no school, no work and no others), it is impossible to flatten the peak. This is caused by the fact that all contact matrices were obtained under a business-as-usual scenario, and do not account for the effect of general social distancing. To include the effect of social distancing, it is necessary to use one extra parameter, $\chi$, to model the effect of lower network connectivity. As the peak is flattening in Belgium, it is possible to calibrate $\chi$ using the ICU vs. time data from Belgium. To model the effect of Belgian government policy, we construct an artificial interaction matrix $N_{\text{c, total}}$ of the following form,

\begin{equation}
N_{\text{c, total}} = N_{\text{c, home}} + (1-x_s)*N_{\text{c, schools}} + \chi*(1-x_w)*N_{\text{c, work}} + \chi*(1-x_o)*N_{\text{c, others}}.
\end{equation}

In this equation, the total interaction matrix is a linear combination of the four individual contributions, $N_{\text{c, home}}$, $N_{\text{c, schools}}$, $N_{\text{c, work}}$ and $N_{\text{c, others}}$. In the above equation $x_s$ represents the reduction of contacts on schools. Since schools are closed, $x_s = 1$ and the contribution of $N_{\text{c, schools}}$ drops out of the equation. Note there is no social distancing parameter at home or in school. This is done on purpose because it is impossible to practice social distancing at home and it is highly unlikely that social distancing measures are effective at schools. $x_w$ represents the reduction of contacts at work and is obtained using the Google Covid 19 Community Mobility Report. From the report it can be seen that the fraction of people working at home gradually lowered from 0 to 52% during the week of Monday March 16th (week of announcement government measures). $x_o$ is the fractional reduction of all other contacts and is also estimated using the Google Covid 19 Community Mobility Report. The report shows an 85% decrease for places like restaurants, cafes, shopping centers, theme parks, museums, libraries, and movie theaters. A 53% reduction in grocery markets, food warehouses, farmers markets, specialty food shops, drug stores, and pharmacies. A 66% reduction for places like national parks, public beaches, marinas, dog parks, plazas, and public gardens. And finally a 76% reduction for places like public transport hubs such as subway, bus, and train stations. To obtain an exact number for $x_o$, one would need to know the contribution of each percentage to the total amount of interactions. However, since this data is unavailable we use the average value, $x_o = 0.70$.

Instead of considering step-wise changes of $\chi$, $x_w$ and $x_o$, we assume a two week transition period in which:

- The fraction of people working at home, $x_w$, changes linearly from 0.00 to 0.52 over the course of March 17th untill March 27th. The assumption that transitioning took approximately one to two weeks is backed up by the Google Covid 19 Community Report (see work related mobility stats below).
- The reduction in other contacts, $x_o$, changes from 0.00 to 0.35 on March 15th, and then changes to 0.70 on March 17th. This is done because all restaurants, cinemas, bars, etc. instantly closed on March 15th. On March 17th, all non essential shops were closed too. A more abrupt change in leasure activities is also backed up by the Google Covid 19 Community Report (see retail related mobility stats below).
- The hardest part of modeling the transition period is the gradual increase of public obedience for social distancing measures. It is relatively straightforward to assume that a mentality change in a population as whole is a gradual rather than a stepwise change. We model public obedience as follows: we discretise a two week transition period in 5 intervals, with 4 changes of the social distancing parameters $\chi$. We then use the available data to calibrate the gradual increase in social distancing. Calibration was difficult, so a brute-force approach, rather than a genetic algorithm, was used for the optimisation.

https://www.google.com/covid19/mobility/

#### Calibration with a genetic algorithm: DOES NOT WORK WELL

```python
# -------------------------------
# Parameters of fitting algorithm
# -------------------------------
monteCarlo = True
n_samples = 500
maxiter=30
popsize=30
polish=True
disp = True
bounds=[(0.9,1),(0,1),(0,1),(0,1),(0,1)]
data=np.transpose(ICUvect[:,0:])
data = np.append(data,np.ones(3)*data[-1])
data = np.reshape(data,[data.size,1])
method = 'socialInteraction'
modelType = 'deterministic'
fitTo = np.array([8]) #positions in output of runSimulation that must be added together

# -----------
# Perform fit
# -----------
# chis = cH.modelFit(bounds,data,fitTo,initN,Nc,zeta,dsm,dm,dhospital,dh,mc0,ICU,theta_S,theta_E,theta_SM,theta_M,theta_R,psi_FP,psi_PP,dq,phi_S,phi_E,phi_SM,phi_R,monteCarlo,n_samples,method,modelType,checkpoints,disp,polish,maxiter,popsize)
# print(chis)
```

#### Rather use brute-force approach

```python
monteCarlo = True
n_samples = 5
data=np.transpose(ICUvect[:,0:])
data = np.append(data,np.ones(5)*data[-1])
data = np.reshape(data,[data.size,1])

x1 = np.linspace(0.05,1,4)
x2 = np.linspace(0.05,1,4)
x3 = np.linspace(0.05,1,4)
x4 = np.linspace(0.05,1,4)

# monte-carlo sampling
sigma = cH.sampleFromDistribution('corona_incubatie.csv',n_samples)
dcf = np.random.normal(18.5, 5.2, n_samples)
dcr = np.random.normal(22.0, 5.2, n_samples)
sm = np.random.normal(0.86, 0.04/1.96, n_samples)
m = (1-sm)*0.81
h = (1-sm)*0.14
c = (1-sm)*0.05
dhospital = np.random.normal(9.10, 0.50/1.96, n_samples)

# no monte-carlo sampling
# sigma = 5.2
# dcf = 18.5
# dcr = 22.0
# sm = 0.86
# m = (1-sm)*0.81
# h = (1-sm)*0.14
# c = (1-sm)*0.05
# dhospital = 9.1

stoArgs = None
method = 'socialInteraction'
SSE = np.zeros((x1.size,x2.size,x3.size,x4.size))
for i in range(x1.size):
    for j in range(x2.size):
        for k in range(x3.size):
            for l in range(x4.size):
                thetas=np.array([x1[i],x2[j],x3[k],x4[l]])
                checkpoints={
                             't':  [26,29,29+5,29+10,29+15],
                             'Nc': [Nc_all-Nc_schools,
                                     Nc_home + thetas[0]*(1-0.20)*Nc_work + thetas[0]*(1-0.70)*Nc_others,
                                     Nc_home + thetas[1]*(1-0.40)*Nc_work + thetas[1]*(1-0.70)*Nc_others,
                                     Nc_home + thetas[2]*(1-0.52)*Nc_work + thetas[2]*(1-0.70)*Nc_others,
                                     Nc_home + thetas[3]*(1-0.52)*Nc_work + thetas[3]*(1-0.70)*Nc_others]
                             }
                SSE[i,j,k,l] = cH.LSQ(thetas,data,fitTo,
                                    initN,sigma,Nc,zeta,sm,m,h,c,dsm,dm,dhospital,dh,
                                    dcf,dcr,mc0,ICU,theta_S,theta_E,theta_SM,theta_M,theta_R,
                                    psi_FP,psi_PP,dq,phi_S,phi_E,phi_SM,phi_R,monteCarlo,method,
                                    modelType,checkpoints,stoArgs)
print(np.where(SSE == SSE.min()))
sol=SSE.min()
print(sol)
```

#### Visualise fit

```python
# Load the fitted parameters
Nc=Nc_all
beta = estimate[0]
extraTime = estimate[1]
# inital condition
initN = initN
initE = np.ones(Nc.shape[0])
initSM = np.zeros(Nc.shape[0])
initM = np.zeros(Nc.shape[0])
initH = np.zeros(Nc.shape[0])
initC = np.zeros(Nc.shape[0])
initHH = np.zeros(Nc.shape[0])
initCH = np.zeros(Nc.shape[0])
initR = np.zeros(Nc.shape[0])
initF = np.zeros(Nc.shape[0])
initSQ = np.zeros(Nc.shape[0])
initEQ = np.zeros(Nc.shape[0])
initSMQ = np.zeros(Nc.shape[0])
initMQ = np.zeros(Nc.shape[0])
initRQ = np.zeros(Nc.shape[0])

# --------------
# Run simulation
# --------------
method='none'
simtime = 80
#chis=np.array([1,0.34,0.34,0.01]) #werkt goed
chis=np.array([1,0.68,0.34,0.02])

checkpoints={
    't':  [26,29,29+5,29+10,29+15],
    'Nc': [Nc_all-Nc_schools,
            Nc_home + chis[0]*(1-0.20)*Nc_work + chis[0]*(1-0.70)*Nc_others,
            Nc_home + chis[1]*(1-0.40)*Nc_work + chis[1]*(1-0.70)*Nc_others,
            Nc_home + chis[2]*(1-0.52)*Nc_work + chis[2]*(1-0.70)*Nc_others,
            Nc_home + chis[3]*(1-0.52)*Nc_work + chis[3]*(1-0.70)*Nc_others]
}

monteCarlo=True
n_samples=200
simout = cH.simModel(initN,beta,Nc,zeta,dsm,dm,dhospital,dh,mc0,ICU,theta_S,theta_E,theta_SM,theta_M,
                     theta_R,psi_FP,psi_PP,dq,phi_S,phi_E,phi_SM,phi_R,initE, initSM, initM, initH, initC,
                    initHH,initCH,initR,initF,initSQ,initEQ,initSMQ,initMQ,initRQ,simtime,monteCarlo,
                     n_samples,method,modelType,checkpoints)
```

```python
# -----------
# Plot result
# -----------

t=pd.date_range('2020-03-13', freq='D', periods=ICUvect.size)
tacc=pd.date_range('2020-03-13', freq='D', periods=simtime+1)-datetime.timedelta(days=int(extraTime)-1)

fig=plt.figure(1)
plt.figure(figsize=(6,4),dpi=100)
plt.scatter(t,data_belgie.iloc[:,1],color="black",marker="v")
plt.plot(tacc,np.mean(simout['CH'],axis=1),'--',color="orange")
plt.fill_between(tacc,np.percentile(simout['CH'],95,axis=1),
                 np.percentile(simout['CH'],5,axis=1),color="orange",alpha=0.2)
plt.legend(('ICU (model)','ICU (data)'),loc='upper left')
plt.xlim(pd.to_datetime(tacc[25]),pd.to_datetime(tacc[-20]))
plt.title('Belgium',{'fontsize':18})
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
#plt.gca().xaxis.set_minor_locator(mdates.DayLocator())
plt.gca().xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%d-%m-%Y'))
plt.setp(plt.gca().xaxis.get_majorticklabels(),
         'rotation', 90)
plt.ylabel('number of patients')

plt.savefig('belgiumICUfit.svg',dpi=100,bbox_inches='tight')
```

## Controller

#### Messing around

```python
modelType = 'deterministic'
method = 'none'
```

```python
monteCarlo = False
n_samples=1000
# initE = np.ones(16)*100
# initN = initN
period = 7
P = 8
N = 6
discrete=True
ICU = 1000
```

```python
# inital condition
initN = initN
initE = np.ones(Nc.shape[0])*np.mean(simout['E'],axis=1)[-1]/16
initSM = np.ones(Nc.shape[0])*np.mean(simout['SM'],axis=1)[-1]/16
initM = np.ones(Nc.shape[0])*np.mean(simout['M'],axis=1)[-1]/16
initH = np.ones(Nc.shape[0])*np.mean(simout['H'],axis=1)[-1]/16
initC = np.ones(Nc.shape[0])*np.mean(simout['C'],axis=1)[-1]/16
initHH = np.ones(Nc.shape[0])*np.mean(simout['HH'],axis=1)[-1]/16
initCH = np.ones(Nc.shape[0])*np.mean(simout['CH'],axis=1)[-1]/16
initR = np.ones(Nc.shape[0])*np.mean(simout['R'],axis=1)[-1]/16
initF = np.ones(Nc.shape[0])*np.mean(simout['F'],axis=1)[-1]/16
initSQ = np.ones(Nc.shape[0])*np.mean(simout['SQ'],axis=1)[-1]/16
initEQ = np.ones(Nc.shape[0])*np.mean(simout['EQ'],axis=1)[-1]/16
initSMQ = np.ones(Nc.shape[0])*np.mean(simout['SMQ'],axis=1)[-1]/16
initMQ = np.ones(Nc.shape[0])*np.mean(simout['MQ'],axis=1)[-1]/16
initRQ = np.ones(Nc.shape[0])*np.mean(simout['RQ'],axis=1)[-1]/16

polish=True
disp = True
maxiter = 80
popsize = 15

policy,thetas = cH.MPCoptimizeAge(initN,beta,zeta,dsm,dm,dhospital,dh,mc0,ICU,theta_S,theta_E,theta_SM,theta_M,theta_R,
                           psi_FP,psi_PP,dq,phi_S,phi_E,phi_SM,phi_R,initE, initSM, initM, initH, initC,initHH,initCH,initR,initF,initSQ,
                           initEQ,initSMQ,initMQ,initRQ,simtime,monteCarlo,n_samples,method,modelType,discrete,
                           period,P,N,disp,polish,maxiter,popsize)

print(policy,thetas)
```

    differential_evolution step 1: f(x)= 1.13248e+08
    differential_evolution step 2: f(x)= 1.07467e+08
    differential_evolution step 3: f(x)= 1.06177e+08
    differential_evolution step 4: f(x)= 1.04603e+08
    differential_evolution step 5: f(x)= 1.04603e+08
    differential_evolution step 6: f(x)= 1.04603e+08
    differential_evolution step 7: f(x)= 1.01492e+08
    differential_evolution step 8: f(x)= 1.01492e+08
    differential_evolution step 9: f(x)= 1.01492e+08
    differential_evolution step 10: f(x)= 1.01492e+08
    differential_evolution step 11: f(x)= 1.01492e+08
    differential_evolution step 12: f(x)= 1.01492e+08
    differential_evolution step 13: f(x)= 1.01492e+08
    differential_evolution step 14: f(x)= 1.01492e+08
    differential_evolution step 15: f(x)= 1.01492e+08
    differential_evolution step 16: f(x)= 1.01492e+08
    differential_evolution step 17: f(x)= 1.01492e+08
    differential_evolution step 18: f(x)= 1.01492e+08
    differential_evolution step 19: f(x)= 1.01492e+08
    differential_evolution step 20: f(x)= 1.01492e+08
    differential_evolution step 21: f(x)= 1.01492e+08
    differential_evolution step 22: f(x)= 1.01492e+08
    differential_evolution step 23: f(x)= 1.01492e+08
    differential_evolution step 24: f(x)= 1.01492e+08
    differential_evolution step 25: f(x)= 1.01492e+08
    differential_evolution step 26: f(x)= 1.01492e+08
    differential_evolution step 27: f(x)= 1.01492e+08
    differential_evolution step 28: f(x)= 1.01492e+08
    differential_evolution step 29: f(x)= 1.01492e+08
    differential_evolution step 30: f(x)= 1.01492e+08
    differential_evolution step 31: f(x)= 1.01492e+08
    differential_evolution step 32: f(x)= 1.01492e+08
    differential_evolution step 33: f(x)= 1.01492e+08
    differential_evolution step 34: f(x)= 1.01492e+08
    differential_evolution step 35: f(x)= 1.01492e+08
    differential_evolution step 36: f(x)= 1.01492e+08
    differential_evolution step 37: f(x)= 1.01492e+08
    differential_evolution step 38: f(x)= 1.01492e+08
    differential_evolution step 39: f(x)= 1.01492e+08
    differential_evolution step 40: f(x)= 1.01492e+08
    differential_evolution step 41: f(x)= 1.01492e+08
    differential_evolution step 42: f(x)= 1.01492e+08
    differential_evolution step 43: f(x)= 1.01492e+08
    differential_evolution step 44: f(x)= 1.01492e+08
    differential_evolution step 45: f(x)= 1.01492e+08
    differential_evolution step 46: f(x)= 1.01492e+08
    differential_evolution step 47: f(x)= 1.01492e+08
    differential_evolution step 48: f(x)= 1.01492e+08
    differential_evolution step 49: f(x)= 1.01492e+08
    differential_evolution step 50: f(x)= 1.01492e+08
    differential_evolution step 51: f(x)= 1.01492e+08
    differential_evolution step 52: f(x)= 1.01492e+08
    differential_evolution step 53: f(x)= 1.01492e+08
    differential_evolution step 54: f(x)= 1.01492e+08
    differential_evolution step 55: f(x)= 1.01492e+08
    differential_evolution step 56: f(x)= 1.01492e+08
    differential_evolution step 57: f(x)= 1.01492e+08
    differential_evolution step 58: f(x)= 1.01492e+08
    differential_evolution step 59: f(x)= 1.01492e+08
    differential_evolution step 60: f(x)= 1.01492e+08
    differential_evolution step 61: f(x)= 1.01492e+08
    differential_evolution step 62: f(x)= 1.01492e+08
    differential_evolution step 63: f(x)= 1.01492e+08
    differential_evolution step 64: f(x)= 1.01492e+08
    differential_evolution step 65: f(x)= 1.01492e+08
    differential_evolution step 66: f(x)= 1.01492e+08
    differential_evolution step 67: f(x)= 1.01492e+08
    differential_evolution step 68: f(x)= 1.01492e+08
    differential_evolution step 69: f(x)= 1.01492e+08
    differential_evolution step 70: f(x)= 1.01492e+08
    differential_evolution step 71: f(x)= 1.01492e+08
    differential_evolution step 72: f(x)= 1.01492e+08
    differential_evolution step 73: f(x)= 1.01492e+08
    differential_evolution step 74: f(x)= 1.01492e+08
    differential_evolution step 75: f(x)= 1.01492e+08
    differential_evolution step 76: f(x)= 1.01492e+08
    differential_evolution step 77: f(x)= 1.01492e+08
    differential_evolution step 78: f(x)= 1.01492e+08
    differential_evolution step 79: f(x)= 1.01492e+08
    differential_evolution step 80: f(x)= 1.01492e+08
    [array([[1.23628147e-01, 1.78123042e-01, 6.33852631e-02, 2.40222726e-04,
            4.74856012e-02, 5.79310536e-01, 7.67400980e-01, 2.47866437e-01,
            1.76166687e-02, 4.26855311e-02, 1.07179172e-02, 9.01975960e-04,
            4.43671452e-04, 4.41990356e-04, 1.79153998e-04, 2.40002279e-04],
           [3.21438849e-01, 2.62563795e-01, 1.26855940e-01, 4.07373998e-04,
            9.43132327e-04, 1.84386432e-01, 7.31033742e-01, 4.53542262e-01,
            2.25430548e-01, 2.71032210e-02, 4.06725972e-04, 1.28005799e-02,
            5.92323020e-04, 1.62515193e-04, 1.94128050e-04, 1.62922544e-04],
           [9.82739553e-02, 2.17336312e-01, 5.43046057e-01, 2.84637451e-01,
            6.58595338e-02, 1.83899570e-02, 1.78247556e-01, 7.21636355e-01,
            5.42377055e-01, 2.55687922e-01, 5.86664118e-03, 1.07582416e-02,
            1.51090939e-02, 6.27756165e-03, 2.11460268e-04, 2.00365335e-04],
           [1.72609817e-02, 3.11311483e-02, 3.31385612e-01, 3.51826578e-01,
            1.67248085e-01, 9.45873465e-03, 8.96520796e-04, 1.66829512e-01,
            5.86219728e-01, 5.18693864e-01, 1.22663170e-01, 9.07537178e-04,
            2.32339356e-04, 2.29201582e-03, 4.81198356e-03, 1.56020305e-05],
           [2.93130215e-05, 3.32831964e-02, 6.69632107e-02, 3.71938884e-01,
            3.95136476e-01, 8.80609602e-02, 3.32569443e-02, 5.78495581e-03,
            1.51798710e-01, 6.31122470e-01, 2.25210264e-01, 1.49599522e-01,
            3.85165215e-04, 2.46728625e-04, 3.14274977e-04, 3.27245041e-04],
           [1.44583464e-01, 8.53005610e-03, 2.05200328e-03, 1.68145373e-02,
            1.21877454e-01, 3.14140767e-01, 9.11019072e-02, 1.38088064e-02,
            1.09816184e-02, 1.86008856e-01, 2.35036090e-01, 1.24365082e-02,
            4.90260572e-05, 4.57986582e-09, 3.92736256e-05, 1.69382023e-04],
           [4.76666749e-01, 3.43951106e-01, 4.94102761e-02, 2.01532827e-03,
            5.86286094e-03, 1.77178308e-01, 4.18281674e-01, 1.96887791e-01,
            9.05118212e-02, 6.74974918e-03, 1.60845015e-02, 1.56044331e-03,
            9.64280665e-02, 2.98687629e-02, 3.70994749e-05, 4.93613421e-04],
           [3.21813077e-01, 4.31582123e-01, 3.26840729e-01, 6.99148029e-02,
            7.17580260e-04, 2.38150880e-02, 9.12689120e-02, 3.19216847e-01,
            4.46868613e-02, 2.79820971e-02, 2.10688752e-03, 8.59504682e-04,
            5.00863767e-04, 3.28257272e-04, 4.81265510e-04, 3.80656711e-05],
           [9.66812670e-02, 1.57454401e-01, 4.80237126e-01, 3.99776161e-01,
            6.42472729e-02, 3.82340290e-02, 3.52048948e-02, 2.21942738e-01,
            3.27372432e-01, 1.29650503e-01, 2.74465093e-03, 2.50873854e-04,
            3.65929533e-04, 3.50878167e-04, 4.44529986e-04, 5.64091606e-03],
           [6.25169929e-03, 9.43104457e-03, 1.84144154e-01, 5.27636111e-01,
            3.25493038e-01, 4.97560529e-03, 1.07280444e-02, 5.96939810e-02,
            2.28716999e-01, 3.74590844e-01, 4.77136336e-02, 1.53127988e-03,
            1.79711744e-04, 1.49414162e-04, 1.46155697e-04, 1.13540860e-02],
           [3.27426300e-04, 1.09338074e-03, 6.83921017e-03, 9.82870981e-02,
            2.96912044e-01, 4.18216586e-01, 1.98798738e-02, 4.18773573e-03,
            6.39938796e-03, 1.00601792e-01, 3.44941199e-01, 1.12297699e-01,
            3.39213982e-02, 6.34412514e-04, 5.58892672e-04, 1.93979926e-02],
           [2.61699617e-01, 5.46022579e-02, 1.12280184e-02, 8.12435672e-02,
            2.90715247e-01, 4.38923985e-01, 1.85949743e-01, 2.50790473e-02,
            2.01014057e-02, 6.38952553e-02, 1.86477810e-01, 2.90155679e-01,
            4.81343530e-02, 3.26033705e-03, 2.19524067e-04, 2.07679998e-02],
           [3.62832583e-02, 3.24136454e-05, 5.13966801e-03, 2.53723934e-04,
            7.66526966e-04, 4.70715240e-02, 2.78437734e-01, 8.51840228e-02,
            3.35655622e-02, 1.63364003e-03, 2.68656276e-02, 1.42286912e-01,
            4.68304366e-01, 9.02231634e-02, 4.75633182e-02, 8.75310157e-04],
           [1.16740419e-04, 5.23389492e-04, 1.20139084e-04, 9.80841141e-05,
            2.84305104e-04, 5.46645373e-04, 1.29284267e-03, 1.74372911e-03,
            2.10167444e-03, 1.12374709e-03, 2.83025380e-04, 1.10309955e-03,
            1.20017976e-01, 2.81207979e-01, 4.98366717e-04, 3.27269398e-02],
           [7.39834431e-05, 4.60610987e-04, 4.59903793e-04, 7.23214936e-04,
            2.17398410e-04, 7.22962781e-04, 7.04965729e-04, 1.26211869e-03,
            1.76432857e-03, 1.24505488e-03, 1.93409517e-03, 5.85968199e-04,
            2.93484749e-03, 1.86920479e-01, 1.07159436e-01, 1.14388123e-01],
           [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 4.20389539e-45,
            0.00000000e+00, 1.64030076e-04, 6.42228290e-04, 4.30420220e-24,
            3.26936456e-09, 1.57736169e-04, 5.45131084e-12, 1.70031047e-08,
            2.29537502e-04, 4.54660207e-02, 8.24055477e-11, 2.94448808e-02]],
          dtype=float32), array([[1.23628147e-01, 1.78123042e-01, 6.33852631e-02, 2.40222726e-04,
            4.74856012e-02, 5.79310536e-01, 7.67400980e-01, 2.47866437e-01,
            1.76166687e-02, 4.26855311e-02, 1.07179172e-02, 9.01975960e-04,
            4.43671452e-04, 4.41990356e-04, 1.79153998e-04, 2.40002279e-04],
           [3.21438849e-01, 2.62563795e-01, 1.26855940e-01, 4.07373998e-04,
            9.43132327e-04, 1.84386432e-01, 7.31033742e-01, 4.53542262e-01,
            2.25430548e-01, 2.71032210e-02, 4.06725972e-04, 1.28005799e-02,
            5.92323020e-04, 1.62515193e-04, 1.94128050e-04, 1.62922544e-04],
           [9.82739553e-02, 2.17336312e-01, 5.43046057e-01, 2.84637451e-01,
            6.58595338e-02, 1.83899570e-02, 1.78247556e-01, 7.21636355e-01,
            5.42377055e-01, 2.55687922e-01, 5.86664118e-03, 1.07582416e-02,
            1.51090939e-02, 6.27756165e-03, 2.11460268e-04, 2.00365335e-04],
           [1.72609817e-02, 3.11311483e-02, 3.31385612e-01, 3.51826578e-01,
            1.67248085e-01, 9.45873465e-03, 8.96520796e-04, 1.66829512e-01,
            5.86219728e-01, 5.18693864e-01, 1.22663170e-01, 9.07537178e-04,
            2.32339356e-04, 2.29201582e-03, 4.81198356e-03, 1.56020305e-05],
           [2.93130215e-05, 3.32831964e-02, 6.69632107e-02, 3.71938884e-01,
            3.95136476e-01, 8.80609602e-02, 3.32569443e-02, 5.78495581e-03,
            1.51798710e-01, 6.31122470e-01, 2.25210264e-01, 1.49599522e-01,
            3.85165215e-04, 2.46728625e-04, 3.14274977e-04, 3.27245041e-04],
           [1.44583464e-01, 8.53005610e-03, 2.05200328e-03, 1.68145373e-02,
            1.21877454e-01, 3.14140767e-01, 9.11019072e-02, 1.38088064e-02,
            1.09816184e-02, 1.86008856e-01, 2.35036090e-01, 1.24365082e-02,
            4.90260572e-05, 4.57986582e-09, 3.92736256e-05, 1.69382023e-04],
           [4.76666749e-01, 3.43951106e-01, 4.94102761e-02, 2.01532827e-03,
            5.86286094e-03, 1.77178308e-01, 4.18281674e-01, 1.96887791e-01,
            9.05118212e-02, 6.74974918e-03, 1.60845015e-02, 1.56044331e-03,
            9.64280665e-02, 2.98687629e-02, 3.70994749e-05, 4.93613421e-04],
           [3.21813077e-01, 4.31582123e-01, 3.26840729e-01, 6.99148029e-02,
            7.17580260e-04, 2.38150880e-02, 9.12689120e-02, 3.19216847e-01,
            4.46868613e-02, 2.79820971e-02, 2.10688752e-03, 8.59504682e-04,
            5.00863767e-04, 3.28257272e-04, 4.81265510e-04, 3.80656711e-05],
           [9.66812670e-02, 1.57454401e-01, 4.80237126e-01, 3.99776161e-01,
            6.42472729e-02, 3.82340290e-02, 3.52048948e-02, 2.21942738e-01,
            3.27372432e-01, 1.29650503e-01, 2.74465093e-03, 2.50873854e-04,
            3.65929533e-04, 3.50878167e-04, 4.44529986e-04, 5.64091606e-03],
           [6.25169929e-03, 9.43104457e-03, 1.84144154e-01, 5.27636111e-01,
            3.25493038e-01, 4.97560529e-03, 1.07280444e-02, 5.96939810e-02,
            2.28716999e-01, 3.74590844e-01, 4.77136336e-02, 1.53127988e-03,
            1.79711744e-04, 1.49414162e-04, 1.46155697e-04, 1.13540860e-02],
           [3.27426300e-04, 1.09338074e-03, 6.83921017e-03, 9.82870981e-02,
            2.96912044e-01, 4.18216586e-01, 1.98798738e-02, 4.18773573e-03,
            6.39938796e-03, 1.00601792e-01, 3.44941199e-01, 1.12297699e-01,
            3.39213982e-02, 6.34412514e-04, 5.58892672e-04, 1.93979926e-02],
           [2.61699617e-01, 5.46022579e-02, 1.12280184e-02, 8.12435672e-02,
            2.90715247e-01, 4.38923985e-01, 1.85949743e-01, 2.50790473e-02,
            2.01014057e-02, 6.38952553e-02, 1.86477810e-01, 2.90155679e-01,
            4.81343530e-02, 3.26033705e-03, 2.19524067e-04, 2.07679998e-02],
           [3.62832583e-02, 3.24136454e-05, 5.13966801e-03, 2.53723934e-04,
            7.66526966e-04, 4.70715240e-02, 2.78437734e-01, 8.51840228e-02,
            3.35655622e-02, 1.63364003e-03, 2.68656276e-02, 1.42286912e-01,
            4.68304366e-01, 9.02231634e-02, 4.75633182e-02, 8.75310157e-04],
           [1.16740419e-04, 5.23389492e-04, 1.20139084e-04, 9.80841141e-05,
            2.84305104e-04, 5.46645373e-04, 1.29284267e-03, 1.74372911e-03,
            2.10167444e-03, 1.12374709e-03, 2.83025380e-04, 1.10309955e-03,
            1.20017976e-01, 2.81207979e-01, 4.98366717e-04, 3.27269398e-02],
           [7.39834431e-05, 4.60610987e-04, 4.59903793e-04, 7.23214936e-04,
            2.17398410e-04, 7.22962781e-04, 7.04965729e-04, 1.26211869e-03,
            1.76432857e-03, 1.24505488e-03, 1.93409517e-03, 5.85968199e-04,
            2.93484749e-03, 1.86920479e-01, 1.07159436e-01, 1.14388123e-01],
           [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 4.20389539e-45,
            0.00000000e+00, 1.64030076e-04, 6.42228290e-04, 4.30420220e-24,
            3.26936456e-09, 1.57736169e-04, 5.45131084e-12, 1.70031047e-08,
            2.29537502e-04, 4.54660207e-02, 8.24055477e-11, 2.94448808e-02]],
          dtype=float32), array([[1.23628147e-01, 1.78123042e-01, 6.33852631e-02, 2.40222726e-04,
            4.74856012e-02, 5.79310536e-01, 7.67400980e-01, 2.47866437e-01,
            1.76166687e-02, 4.26855311e-02, 1.07179172e-02, 9.01975960e-04,
            4.43671452e-04, 4.41990356e-04, 1.79153998e-04, 2.40002279e-04],
           [3.21438849e-01, 2.62563795e-01, 1.26855940e-01, 4.07373998e-04,
            9.43132327e-04, 1.84386432e-01, 7.31033742e-01, 4.53542262e-01,
            2.25430548e-01, 2.71032210e-02, 4.06725972e-04, 1.28005799e-02,
            5.92323020e-04, 1.62515193e-04, 1.94128050e-04, 1.62922544e-04],
           [9.82739553e-02, 2.17336312e-01, 5.43046057e-01, 2.84637451e-01,
            6.58595338e-02, 1.83899570e-02, 1.78247556e-01, 7.21636355e-01,
            5.42377055e-01, 2.55687922e-01, 5.86664118e-03, 1.07582416e-02,
            1.51090939e-02, 6.27756165e-03, 2.11460268e-04, 2.00365335e-04],
           [1.72609817e-02, 3.11311483e-02, 3.31385612e-01, 3.51826578e-01,
            1.67248085e-01, 9.45873465e-03, 8.96520796e-04, 1.66829512e-01,
            5.86219728e-01, 5.18693864e-01, 1.22663170e-01, 9.07537178e-04,
            2.32339356e-04, 2.29201582e-03, 4.81198356e-03, 1.56020305e-05],
           [2.93130215e-05, 3.32831964e-02, 6.69632107e-02, 3.71938884e-01,
            3.95136476e-01, 8.80609602e-02, 3.32569443e-02, 5.78495581e-03,
            1.51798710e-01, 6.31122470e-01, 2.25210264e-01, 1.49599522e-01,
            3.85165215e-04, 2.46728625e-04, 3.14274977e-04, 3.27245041e-04],
           [1.44583464e-01, 8.53005610e-03, 2.05200328e-03, 1.68145373e-02,
            1.21877454e-01, 3.14140767e-01, 9.11019072e-02, 1.38088064e-02,
            1.09816184e-02, 1.86008856e-01, 2.35036090e-01, 1.24365082e-02,
            4.90260572e-05, 4.57986582e-09, 3.92736256e-05, 1.69382023e-04],
           [4.76666749e-01, 3.43951106e-01, 4.94102761e-02, 2.01532827e-03,
            5.86286094e-03, 1.77178308e-01, 4.18281674e-01, 1.96887791e-01,
            9.05118212e-02, 6.74974918e-03, 1.60845015e-02, 1.56044331e-03,
            9.64280665e-02, 2.98687629e-02, 3.70994749e-05, 4.93613421e-04],
           [3.21813077e-01, 4.31582123e-01, 3.26840729e-01, 6.99148029e-02,
            7.17580260e-04, 2.38150880e-02, 9.12689120e-02, 3.19216847e-01,
            4.46868613e-02, 2.79820971e-02, 2.10688752e-03, 8.59504682e-04,
            5.00863767e-04, 3.28257272e-04, 4.81265510e-04, 3.80656711e-05],
           [9.66812670e-02, 1.57454401e-01, 4.80237126e-01, 3.99776161e-01,
            6.42472729e-02, 3.82340290e-02, 3.52048948e-02, 2.21942738e-01,
            3.27372432e-01, 1.29650503e-01, 2.74465093e-03, 2.50873854e-04,
            3.65929533e-04, 3.50878167e-04, 4.44529986e-04, 5.64091606e-03],
           [6.25169929e-03, 9.43104457e-03, 1.84144154e-01, 5.27636111e-01,
            3.25493038e-01, 4.97560529e-03, 1.07280444e-02, 5.96939810e-02,
            2.28716999e-01, 3.74590844e-01, 4.77136336e-02, 1.53127988e-03,
            1.79711744e-04, 1.49414162e-04, 1.46155697e-04, 1.13540860e-02],
           [3.27426300e-04, 1.09338074e-03, 6.83921017e-03, 9.82870981e-02,
            2.96912044e-01, 4.18216586e-01, 1.98798738e-02, 4.18773573e-03,
            6.39938796e-03, 1.00601792e-01, 3.44941199e-01, 1.12297699e-01,
            3.39213982e-02, 6.34412514e-04, 5.58892672e-04, 1.93979926e-02],
           [2.61699617e-01, 5.46022579e-02, 1.12280184e-02, 8.12435672e-02,
            2.90715247e-01, 4.38923985e-01, 1.85949743e-01, 2.50790473e-02,
            2.01014057e-02, 6.38952553e-02, 1.86477810e-01, 2.90155679e-01,
            4.81343530e-02, 3.26033705e-03, 2.19524067e-04, 2.07679998e-02],
           [3.62832583e-02, 3.24136454e-05, 5.13966801e-03, 2.53723934e-04,
            7.66526966e-04, 4.70715240e-02, 2.78437734e-01, 8.51840228e-02,
            3.35655622e-02, 1.63364003e-03, 2.68656276e-02, 1.42286912e-01,
            4.68304366e-01, 9.02231634e-02, 4.75633182e-02, 8.75310157e-04],
           [1.16740419e-04, 5.23389492e-04, 1.20139084e-04, 9.80841141e-05,
            2.84305104e-04, 5.46645373e-04, 1.29284267e-03, 1.74372911e-03,
            2.10167444e-03, 1.12374709e-03, 2.83025380e-04, 1.10309955e-03,
            1.20017976e-01, 2.81207979e-01, 4.98366717e-04, 3.27269398e-02],
           [7.39834431e-05, 4.60610987e-04, 4.59903793e-04, 7.23214936e-04,
            2.17398410e-04, 7.22962781e-04, 7.04965729e-04, 1.26211869e-03,
            1.76432857e-03, 1.24505488e-03, 1.93409517e-03, 5.85968199e-04,
            2.93484749e-03, 1.86920479e-01, 1.07159436e-01, 1.14388123e-01],
           [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 4.20389539e-45,
            0.00000000e+00, 1.64030076e-04, 6.42228290e-04, 4.30420220e-24,
            3.26936456e-09, 1.57736169e-04, 5.45131084e-12, 1.70031047e-08,
            2.29537502e-04, 4.54660207e-02, 8.24055477e-11, 2.94448808e-02]],
          dtype=float32), array([[1.23628147e-01, 1.78123042e-01, 6.33852631e-02, 2.40222726e-04,
            4.74856012e-02, 5.79310536e-01, 7.67400980e-01, 2.47866437e-01,
            1.76166687e-02, 4.26855311e-02, 1.07179172e-02, 9.01975960e-04,
            4.43671452e-04, 4.41990356e-04, 1.79153998e-04, 2.40002279e-04],
           [3.21438849e-01, 2.62563795e-01, 1.26855940e-01, 4.07373998e-04,
            9.43132327e-04, 1.84386432e-01, 7.31033742e-01, 4.53542262e-01,
            2.25430548e-01, 2.71032210e-02, 4.06725972e-04, 1.28005799e-02,
            5.92323020e-04, 1.62515193e-04, 1.94128050e-04, 1.62922544e-04],
           [9.82739553e-02, 2.17336312e-01, 5.43046057e-01, 2.84637451e-01,
            6.58595338e-02, 1.83899570e-02, 1.78247556e-01, 7.21636355e-01,
            5.42377055e-01, 2.55687922e-01, 5.86664118e-03, 1.07582416e-02,
            1.51090939e-02, 6.27756165e-03, 2.11460268e-04, 2.00365335e-04],
           [1.72609817e-02, 3.11311483e-02, 3.31385612e-01, 3.51826578e-01,
            1.67248085e-01, 9.45873465e-03, 8.96520796e-04, 1.66829512e-01,
            5.86219728e-01, 5.18693864e-01, 1.22663170e-01, 9.07537178e-04,
            2.32339356e-04, 2.29201582e-03, 4.81198356e-03, 1.56020305e-05],
           [2.93130215e-05, 3.32831964e-02, 6.69632107e-02, 3.71938884e-01,
            3.95136476e-01, 8.80609602e-02, 3.32569443e-02, 5.78495581e-03,
            1.51798710e-01, 6.31122470e-01, 2.25210264e-01, 1.49599522e-01,
            3.85165215e-04, 2.46728625e-04, 3.14274977e-04, 3.27245041e-04],
           [1.44583464e-01, 8.53005610e-03, 2.05200328e-03, 1.68145373e-02,
            1.21877454e-01, 3.14140767e-01, 9.11019072e-02, 1.38088064e-02,
            1.09816184e-02, 1.86008856e-01, 2.35036090e-01, 1.24365082e-02,
            4.90260572e-05, 4.57986582e-09, 3.92736256e-05, 1.69382023e-04],
           [4.76666749e-01, 3.43951106e-01, 4.94102761e-02, 2.01532827e-03,
            5.86286094e-03, 1.77178308e-01, 4.18281674e-01, 1.96887791e-01,
            9.05118212e-02, 6.74974918e-03, 1.60845015e-02, 1.56044331e-03,
            9.64280665e-02, 2.98687629e-02, 3.70994749e-05, 4.93613421e-04],
           [3.21813077e-01, 4.31582123e-01, 3.26840729e-01, 6.99148029e-02,
            7.17580260e-04, 2.38150880e-02, 9.12689120e-02, 3.19216847e-01,
            4.46868613e-02, 2.79820971e-02, 2.10688752e-03, 8.59504682e-04,
            5.00863767e-04, 3.28257272e-04, 4.81265510e-04, 3.80656711e-05],
           [9.66812670e-02, 1.57454401e-01, 4.80237126e-01, 3.99776161e-01,
            6.42472729e-02, 3.82340290e-02, 3.52048948e-02, 2.21942738e-01,
            3.27372432e-01, 1.29650503e-01, 2.74465093e-03, 2.50873854e-04,
            3.65929533e-04, 3.50878167e-04, 4.44529986e-04, 5.64091606e-03],
           [6.25169929e-03, 9.43104457e-03, 1.84144154e-01, 5.27636111e-01,
            3.25493038e-01, 4.97560529e-03, 1.07280444e-02, 5.96939810e-02,
            2.28716999e-01, 3.74590844e-01, 4.77136336e-02, 1.53127988e-03,
            1.79711744e-04, 1.49414162e-04, 1.46155697e-04, 1.13540860e-02],
           [3.27426300e-04, 1.09338074e-03, 6.83921017e-03, 9.82870981e-02,
            2.96912044e-01, 4.18216586e-01, 1.98798738e-02, 4.18773573e-03,
            6.39938796e-03, 1.00601792e-01, 3.44941199e-01, 1.12297699e-01,
            3.39213982e-02, 6.34412514e-04, 5.58892672e-04, 1.93979926e-02],
           [2.61699617e-01, 5.46022579e-02, 1.12280184e-02, 8.12435672e-02,
            2.90715247e-01, 4.38923985e-01, 1.85949743e-01, 2.50790473e-02,
            2.01014057e-02, 6.38952553e-02, 1.86477810e-01, 2.90155679e-01,
            4.81343530e-02, 3.26033705e-03, 2.19524067e-04, 2.07679998e-02],
           [3.62832583e-02, 3.24136454e-05, 5.13966801e-03, 2.53723934e-04,
            7.66526966e-04, 4.70715240e-02, 2.78437734e-01, 8.51840228e-02,
            3.35655622e-02, 1.63364003e-03, 2.68656276e-02, 1.42286912e-01,
            4.68304366e-01, 9.02231634e-02, 4.75633182e-02, 8.75310157e-04],
           [1.16740419e-04, 5.23389492e-04, 1.20139084e-04, 9.80841141e-05,
            2.84305104e-04, 5.46645373e-04, 1.29284267e-03, 1.74372911e-03,
            2.10167444e-03, 1.12374709e-03, 2.83025380e-04, 1.10309955e-03,
            1.20017976e-01, 2.81207979e-01, 4.98366717e-04, 3.27269398e-02],
           [7.39834431e-05, 4.60610987e-04, 4.59903793e-04, 7.23214936e-04,
            2.17398410e-04, 7.22962781e-04, 7.04965729e-04, 1.26211869e-03,
            1.76432857e-03, 1.24505488e-03, 1.93409517e-03, 5.85968199e-04,
            2.93484749e-03, 1.86920479e-01, 1.07159436e-01, 1.14388123e-01],
           [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 4.20389539e-45,
            0.00000000e+00, 1.64030076e-04, 6.42228290e-04, 4.30420220e-24,
            3.26936456e-09, 1.57736169e-04, 5.45131084e-12, 1.70031047e-08,
            2.29537502e-04, 4.54660207e-02, 8.24055477e-11, 2.94448808e-02]],
          dtype=float32), array([[1.23628147e-01, 1.78123042e-01, 6.33852631e-02, 2.40222726e-04,
            4.74856012e-02, 5.79310536e-01, 7.67400980e-01, 2.47866437e-01,
            1.76166687e-02, 4.26855311e-02, 1.07179172e-02, 9.01975960e-04,
            4.43671452e-04, 4.41990356e-04, 1.79153998e-04, 2.40002279e-04],
           [3.21438849e-01, 2.62563795e-01, 1.26855940e-01, 4.07373998e-04,
            9.43132327e-04, 1.84386432e-01, 7.31033742e-01, 4.53542262e-01,
            2.25430548e-01, 2.71032210e-02, 4.06725972e-04, 1.28005799e-02,
            5.92323020e-04, 1.62515193e-04, 1.94128050e-04, 1.62922544e-04],
           [9.82739553e-02, 2.17336312e-01, 5.43046057e-01, 2.84637451e-01,
            6.58595338e-02, 1.83899570e-02, 1.78247556e-01, 7.21636355e-01,
            5.42377055e-01, 2.55687922e-01, 5.86664118e-03, 1.07582416e-02,
            1.51090939e-02, 6.27756165e-03, 2.11460268e-04, 2.00365335e-04],
           [1.72609817e-02, 3.11311483e-02, 3.31385612e-01, 3.51826578e-01,
            1.67248085e-01, 9.45873465e-03, 8.96520796e-04, 1.66829512e-01,
            5.86219728e-01, 5.18693864e-01, 1.22663170e-01, 9.07537178e-04,
            2.32339356e-04, 2.29201582e-03, 4.81198356e-03, 1.56020305e-05],
           [2.93130215e-05, 3.32831964e-02, 6.69632107e-02, 3.71938884e-01,
            3.95136476e-01, 8.80609602e-02, 3.32569443e-02, 5.78495581e-03,
            1.51798710e-01, 6.31122470e-01, 2.25210264e-01, 1.49599522e-01,
            3.85165215e-04, 2.46728625e-04, 3.14274977e-04, 3.27245041e-04],
           [1.44583464e-01, 8.53005610e-03, 2.05200328e-03, 1.68145373e-02,
            1.21877454e-01, 3.14140767e-01, 9.11019072e-02, 1.38088064e-02,
            1.09816184e-02, 1.86008856e-01, 2.35036090e-01, 1.24365082e-02,
            4.90260572e-05, 4.57986582e-09, 3.92736256e-05, 1.69382023e-04],
           [4.76666749e-01, 3.43951106e-01, 4.94102761e-02, 2.01532827e-03,
            5.86286094e-03, 1.77178308e-01, 4.18281674e-01, 1.96887791e-01,
            9.05118212e-02, 6.74974918e-03, 1.60845015e-02, 1.56044331e-03,
            9.64280665e-02, 2.98687629e-02, 3.70994749e-05, 4.93613421e-04],
           [3.21813077e-01, 4.31582123e-01, 3.26840729e-01, 6.99148029e-02,
            7.17580260e-04, 2.38150880e-02, 9.12689120e-02, 3.19216847e-01,
            4.46868613e-02, 2.79820971e-02, 2.10688752e-03, 8.59504682e-04,
            5.00863767e-04, 3.28257272e-04, 4.81265510e-04, 3.80656711e-05],
           [9.66812670e-02, 1.57454401e-01, 4.80237126e-01, 3.99776161e-01,
            6.42472729e-02, 3.82340290e-02, 3.52048948e-02, 2.21942738e-01,
            3.27372432e-01, 1.29650503e-01, 2.74465093e-03, 2.50873854e-04,
            3.65929533e-04, 3.50878167e-04, 4.44529986e-04, 5.64091606e-03],
           [6.25169929e-03, 9.43104457e-03, 1.84144154e-01, 5.27636111e-01,
            3.25493038e-01, 4.97560529e-03, 1.07280444e-02, 5.96939810e-02,
            2.28716999e-01, 3.74590844e-01, 4.77136336e-02, 1.53127988e-03,
            1.79711744e-04, 1.49414162e-04, 1.46155697e-04, 1.13540860e-02],
           [3.27426300e-04, 1.09338074e-03, 6.83921017e-03, 9.82870981e-02,
            2.96912044e-01, 4.18216586e-01, 1.98798738e-02, 4.18773573e-03,
            6.39938796e-03, 1.00601792e-01, 3.44941199e-01, 1.12297699e-01,
            3.39213982e-02, 6.34412514e-04, 5.58892672e-04, 1.93979926e-02],
           [2.61699617e-01, 5.46022579e-02, 1.12280184e-02, 8.12435672e-02,
            2.90715247e-01, 4.38923985e-01, 1.85949743e-01, 2.50790473e-02,
            2.01014057e-02, 6.38952553e-02, 1.86477810e-01, 2.90155679e-01,
            4.81343530e-02, 3.26033705e-03, 2.19524067e-04, 2.07679998e-02],
           [3.62832583e-02, 3.24136454e-05, 5.13966801e-03, 2.53723934e-04,
            7.66526966e-04, 4.70715240e-02, 2.78437734e-01, 8.51840228e-02,
            3.35655622e-02, 1.63364003e-03, 2.68656276e-02, 1.42286912e-01,
            4.68304366e-01, 9.02231634e-02, 4.75633182e-02, 8.75310157e-04],
           [1.16740419e-04, 5.23389492e-04, 1.20139084e-04, 9.80841141e-05,
            2.84305104e-04, 5.46645373e-04, 1.29284267e-03, 1.74372911e-03,
            2.10167444e-03, 1.12374709e-03, 2.83025380e-04, 1.10309955e-03,
            1.20017976e-01, 2.81207979e-01, 4.98366717e-04, 3.27269398e-02],
           [7.39834431e-05, 4.60610987e-04, 4.59903793e-04, 7.23214936e-04,
            2.17398410e-04, 7.22962781e-04, 7.04965729e-04, 1.26211869e-03,
            1.76432857e-03, 1.24505488e-03, 1.93409517e-03, 5.85968199e-04,
            2.93484749e-03, 1.86920479e-01, 1.07159436e-01, 1.14388123e-01],
           [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 4.20389539e-45,
            0.00000000e+00, 1.64030076e-04, 6.42228290e-04, 4.30420220e-24,
            3.26936456e-09, 1.57736169e-04, 5.45131084e-12, 1.70031047e-08,
            2.29537502e-04, 4.54660207e-02, 8.24055477e-11, 2.94448808e-02]],
          dtype=float32), array([[1.23628147e-01, 1.78123042e-01, 6.33852631e-02, 2.40222726e-04,
            4.74856012e-02, 5.79310536e-01, 7.67400980e-01, 2.47866437e-01,
            1.76166687e-02, 4.26855311e-02, 1.07179172e-02, 9.01975960e-04,
            4.43671452e-04, 4.41990356e-04, 1.79153998e-04, 2.40002279e-04],
           [3.21438849e-01, 2.62563795e-01, 1.26855940e-01, 4.07373998e-04,
            9.43132327e-04, 1.84386432e-01, 7.31033742e-01, 4.53542262e-01,
            2.25430548e-01, 2.71032210e-02, 4.06725972e-04, 1.28005799e-02,
            5.92323020e-04, 1.62515193e-04, 1.94128050e-04, 1.62922544e-04],
           [9.82739553e-02, 2.17336312e-01, 5.43046057e-01, 2.84637451e-01,
            6.58595338e-02, 1.83899570e-02, 1.78247556e-01, 7.21636355e-01,
            5.42377055e-01, 2.55687922e-01, 5.86664118e-03, 1.07582416e-02,
            1.51090939e-02, 6.27756165e-03, 2.11460268e-04, 2.00365335e-04],
           [1.72609817e-02, 3.11311483e-02, 3.31385612e-01, 3.51826578e-01,
            1.67248085e-01, 9.45873465e-03, 8.96520796e-04, 1.66829512e-01,
            5.86219728e-01, 5.18693864e-01, 1.22663170e-01, 9.07537178e-04,
            2.32339356e-04, 2.29201582e-03, 4.81198356e-03, 1.56020305e-05],
           [2.93130215e-05, 3.32831964e-02, 6.69632107e-02, 3.71938884e-01,
            3.95136476e-01, 8.80609602e-02, 3.32569443e-02, 5.78495581e-03,
            1.51798710e-01, 6.31122470e-01, 2.25210264e-01, 1.49599522e-01,
            3.85165215e-04, 2.46728625e-04, 3.14274977e-04, 3.27245041e-04],
           [1.44583464e-01, 8.53005610e-03, 2.05200328e-03, 1.68145373e-02,
            1.21877454e-01, 3.14140767e-01, 9.11019072e-02, 1.38088064e-02,
            1.09816184e-02, 1.86008856e-01, 2.35036090e-01, 1.24365082e-02,
            4.90260572e-05, 4.57986582e-09, 3.92736256e-05, 1.69382023e-04],
           [4.76666749e-01, 3.43951106e-01, 4.94102761e-02, 2.01532827e-03,
            5.86286094e-03, 1.77178308e-01, 4.18281674e-01, 1.96887791e-01,
            9.05118212e-02, 6.74974918e-03, 1.60845015e-02, 1.56044331e-03,
            9.64280665e-02, 2.98687629e-02, 3.70994749e-05, 4.93613421e-04],
           [3.21813077e-01, 4.31582123e-01, 3.26840729e-01, 6.99148029e-02,
            7.17580260e-04, 2.38150880e-02, 9.12689120e-02, 3.19216847e-01,
            4.46868613e-02, 2.79820971e-02, 2.10688752e-03, 8.59504682e-04,
            5.00863767e-04, 3.28257272e-04, 4.81265510e-04, 3.80656711e-05],
           [9.66812670e-02, 1.57454401e-01, 4.80237126e-01, 3.99776161e-01,
            6.42472729e-02, 3.82340290e-02, 3.52048948e-02, 2.21942738e-01,
            3.27372432e-01, 1.29650503e-01, 2.74465093e-03, 2.50873854e-04,
            3.65929533e-04, 3.50878167e-04, 4.44529986e-04, 5.64091606e-03],
           [6.25169929e-03, 9.43104457e-03, 1.84144154e-01, 5.27636111e-01,
            3.25493038e-01, 4.97560529e-03, 1.07280444e-02, 5.96939810e-02,
            2.28716999e-01, 3.74590844e-01, 4.77136336e-02, 1.53127988e-03,
            1.79711744e-04, 1.49414162e-04, 1.46155697e-04, 1.13540860e-02],
           [3.27426300e-04, 1.09338074e-03, 6.83921017e-03, 9.82870981e-02,
            2.96912044e-01, 4.18216586e-01, 1.98798738e-02, 4.18773573e-03,
            6.39938796e-03, 1.00601792e-01, 3.44941199e-01, 1.12297699e-01,
            3.39213982e-02, 6.34412514e-04, 5.58892672e-04, 1.93979926e-02],
           [2.61699617e-01, 5.46022579e-02, 1.12280184e-02, 8.12435672e-02,
            2.90715247e-01, 4.38923985e-01, 1.85949743e-01, 2.50790473e-02,
            2.01014057e-02, 6.38952553e-02, 1.86477810e-01, 2.90155679e-01,
            4.81343530e-02, 3.26033705e-03, 2.19524067e-04, 2.07679998e-02],
           [3.62832583e-02, 3.24136454e-05, 5.13966801e-03, 2.53723934e-04,
            7.66526966e-04, 4.70715240e-02, 2.78437734e-01, 8.51840228e-02,
            3.35655622e-02, 1.63364003e-03, 2.68656276e-02, 1.42286912e-01,
            4.68304366e-01, 9.02231634e-02, 4.75633182e-02, 8.75310157e-04],
           [1.16740419e-04, 5.23389492e-04, 1.20139084e-04, 9.80841141e-05,
            2.84305104e-04, 5.46645373e-04, 1.29284267e-03, 1.74372911e-03,
            2.10167444e-03, 1.12374709e-03, 2.83025380e-04, 1.10309955e-03,
            1.20017976e-01, 2.81207979e-01, 4.98366717e-04, 3.27269398e-02],
           [7.39834431e-05, 4.60610987e-04, 4.59903793e-04, 7.23214936e-04,
            2.17398410e-04, 7.22962781e-04, 7.04965729e-04, 1.26211869e-03,
            1.76432857e-03, 1.24505488e-03, 1.93409517e-03, 5.85968199e-04,
            2.93484749e-03, 1.86920479e-01, 1.07159436e-01, 1.14388123e-01],
           [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, 4.20389539e-45,
            0.00000000e+00, 1.64030076e-04, 6.42228290e-04, 4.30420220e-24,
            3.26936456e-09, 1.57736169e-04, 5.45131084e-12, 1.70031047e-08,
            2.29537502e-04, 4.54660207e-02, 8.24055477e-11, 2.94448808e-02]],
          dtype=float32)] [2.91328032 2.57734389 2.65895893 2.835841   2.91845363 2.74283754]

```python
checkpoints = cH.constructHorizonAge(policy,period)
policyVect = cH.constructHorizonPlot(thetas,period)
simtime = len(policy)*period
# --------------
# Run simulation
# --------------
monteCarlo=True
n_samples=200
Nc = Nc_home + 0.01*(1-0.52)*Nc_work + 0.01*(1-0.70)*Nc_others # Eerste checkpoint is maar na 7 dagen
simout = cH.simModel(initN,beta,Nc,zeta,dsm,dm,dhospital,dh,mc0,ICU,theta_S,theta_E,theta_SM,theta_M,
                     theta_R,psi_FP,psi_PP,dq,phi_S,phi_E,phi_SM,phi_R,initE, initSM, initM, initH, initC,
                    initHH,initCH,initR,initF,initSQ,initEQ,initSMQ,initMQ,initRQ,simtime,monteCarlo,
                     n_samples,method,modelType,checkpoints)
```

```python
# -----------
# Plot result
# -----------
t = simout['t']
I = simout['SM']+ simout['M'] + simout['H'] + simout['C'] + simout['HH'] + simout['CH']

plt.figure(1)
plt.plot(t,np.mean(simout['S'],axis=1),color="black")
plt.fill_between(t, np.percentile(simout['S'],90,axis=1), np.percentile(simout['S'],10,axis=1),color="black",alpha=0.2)
plt.plot(t,np.mean(simout['E'],axis=1),color="blue")
plt.fill_between(t, np.percentile(simout['E'],90,axis=1), np.percentile(simout['E'],10,axis=1),color="blue",alpha=0.2)
plt.plot(t,np.mean(I,axis=1),color="red")
plt.fill_between(t, np.percentile(I,90,axis=1), np.percentile(I,10,axis=1),color="red",alpha=0.2)
plt.plot(t,np.mean(simout['R'],axis=1),color="green")
plt.fill_between(t, np.percentile(simout['R'],90,axis=1), np.percentile(simout['R'],10,axis=1),color="green",alpha=0.2)
plt.legend(('susceptible','exposed','total infected','immune'))
plt.xlabel('days')
plt.ylabel('number of patients')

plt.figure(2)
plt.plot(t,np.mean(simout['CH'],axis=1),color="red")
plt.fill_between(t, np.percentile(simout['CH'],90,axis=1), np.percentile(simout['CH'],10,axis=1),color="red",alpha=0.2)
plt.plot(t,np.ones([t.size])*ICU,'--',color="red")
plt.legend(('critical patients','ICU capacity'))
plt.xlabel('days')
plt.ylabel('number of patients')
ax2 = plt.twinx()
plt.plot(t[:-1],policyVect[:-1],'--',color='black')
ax2.set_ylabel("Daily random social interactions")
```

```python
monteCarlo = False
ICU=1000
P = 7
period = 7
x1 = np.linspace(1,3,3)
x2 = np.linspace(1,3,3)
x3 = np.linspace(1,3,3)
x4 = np.linspace(1,3,3)
x5 = np.linspace(1,3,3)
x6 = np.linspace(1,3,3)
SSE = np.zeros((x1.size,x2.size,x3.size,x4.size,x5.size,x6.size))
for i in range(x1.size):
    for j in range(x2.size):
        for k in range(x3.size):
            for l in range(x4.size):
                for m in range(x5.size):
                    for n in range(x6.size):
                        thetas=np.array([x1[i],x2[j],x3[k],x4[l],x5[m],x6[n]])
                        SSE[i,j,k,l,m,n] = cH.MPCcalcWeightsAge(thetas,initN,beta,zeta,dsm,dm,dhospital,dh,mc0,ICU,theta_S,theta_E,theta_SM,theta_M,theta_R,psi_FP,psi_PP,dq,phi_S,phi_E,phi_SM,phi_R,initE, initSM, initM, initH, initC,
                              initHH,initCH,initR,initF,initSQ,initEQ,initSMQ,initMQ,initRQ,simtime,monteCarlo,n_samples,method,modelType,discrete,period,P)
print(np.where(SSE == SSE.min()))
```

    (array([0]), array([0]), array([0]), array([0]), array([0]), array([2]))

```python
thetas0 = np.array([2,2,2,2,2,2])
Nappend = np.ones([N-thetas0.size])*thetas0[-1]
thetas0 = np.append(thetas0,Nappend)
thetas = scipy.optimize.fmin(cH.MPCcalcWeightsAge, thetas0, args=(initN,beta,zeta,dsm,dm,dhospital,dh,mc0,ICU,
                                                                  theta_S,theta_E,theta_SM,theta_M,theta_R,psi_FP,
                                                                  psi_PP,dq,phi_S,phi_E,phi_SM,phi_R,initE, initSM,
                                                                  initM,initH,initC,initHH,initCH,initR,initF,
                                                                  initSQ,initEQ,initSMQ,initMQ,initRQ,simtime,
                                                                  monteCarlo,n_samples,method,modelType,discrete,
                                                                  period,P), xtol=0.0001, ftol=0.0001, maxiter=10,
                                                                  maxfun=None, full_output=0, disp=1, retall=0,
                                                                  callback=None)

```

```python
print(thetas)
```

    [2.        2.        2.        2.        2.05      2.025     2.0125
     2.00625   2.        2.003125  2.0015625 2.0015625]

```python
thetas=np.array([1,1,1,3,3,3])
Ncs=[]
for i in range(thetas.size):
    if thetas[i]<=1 and thetas[i]>=0:
        Ncs.append(Nc_all)
    elif thetas[i]<=2 and thetas[i]> 1:
        Ncs.append(Nc_home + Nc_schools + 0.01*(1-0.52)*Nc_work + 0.01*(1-0.70)*Nc_others)
    elif thetas[i]<=3 and thetas[i]> 2:
        Ncs.append(Nc_home + 0.01*(1-0.52)*Nc_work + 0.01*(1-0.70)*Nc_others)

```

```python
checkpoints = cH.constructHorizonAge(Ncs,period)
policyVect = cH.constructHorizonPlot(thetas,period)
simtime = len(Ncs)*period
# --------------
# Run simulation
# --------------
simout = cH.simModel(initN,beta,Nc,zeta,dsm,dm,dhospital,dh,mc0,ICU,theta_S,theta_E,theta_SM,theta_M,
                     theta_R,psi_FP,psi_PP,dq,phi_S,phi_E,phi_SM,phi_R,initE, initSM, initM, initH, initC,
                    initHH,initCH,initR,initF,initSQ,initEQ,initSMQ,initMQ,initRQ,simtime,monteCarlo,
                     n_samples,method,modelType,checkpoints)
```

```python
# -----------
# Plot result
# -----------
t = simout['t']
I = simout['SM']+ simout['M'] + simout['H'] + simout['C'] + simout['HH'] + simout['CH']

plt.figure(1)
plt.plot(t,np.mean(simout['S'],axis=1),color="black")
plt.fill_between(t, np.percentile(simout['S'],90,axis=1), np.percentile(simout['S'],10,axis=1),color="black",alpha=0.2)
plt.plot(t,np.mean(simout['E'],axis=1),color="blue")
plt.fill_between(t, np.percentile(simout['E'],90,axis=1), np.percentile(simout['E'],10,axis=1),color="blue",alpha=0.2)
plt.plot(t,np.mean(I,axis=1),color="red")
plt.fill_between(t, np.percentile(I,90,axis=1), np.percentile(I,10,axis=1),color="red",alpha=0.2)
plt.plot(t,np.mean(simout['R'],axis=1),color="green")
plt.fill_between(t, np.percentile(simout['R'],90,axis=1), np.percentile(simout['R'],10,axis=1),color="green",alpha=0.2)
plt.legend(('susceptible','exposed','total infected','immune'))
plt.xlabel('days')
plt.ylabel('number of patients')

plt.figure(2)
plt.plot(t,np.mean(simout['CH'],axis=1),color="red")
plt.fill_between(t, np.percentile(simout['CH'],90,axis=1), np.percentile(simout['CH'],10,axis=1),color="red",alpha=0.2)
plt.plot(t,np.ones([t.size])*ICU,'--',color="red")
plt.legend(('critical patients','ICU capacity'))
plt.xlabel('days')
plt.ylabel('number of patients')
ax2 = plt.twinx()
plt.plot(t[:-1],policyVect[:-1],'--',color='black')
ax2.set_ylabel("Daily random social interactions")
```

#### Find the right initial condition

Run Monte Carlo simulation until present day, then assume equal numbers in each age category.

```python
# inital condition
initN = initN
initE = np.ones(Nc.shape[0])*np.mean(simout['E'],axis=1)[-1]/16
initSM = np.ones(Nc.shape[0])*np.mean(simout['SM'],axis=1)[-1]/16
initM = np.ones(Nc.shape[0])*np.mean(simout['M'],axis=1)[-1]/16
initH = np.ones(Nc.shape[0])*np.mean(simout['H'],axis=1)[-1]/16
initC = np.ones(Nc.shape[0])*np.mean(simout['C'],axis=1)[-1]/16
initHH = np.ones(Nc.shape[0])*np.mean(simout['HH'],axis=1)[-1]/16
initCH = np.ones(Nc.shape[0])*np.mean(simout['CH'],axis=1)[-1]/16
initR = np.ones(Nc.shape[0])*np.mean(simout['R'],axis=1)[-1]/16
initF = np.ones(Nc.shape[0])*np.mean(simout['F'],axis=1)[-1]/16
initSQ = np.ones(Nc.shape[0])*np.mean(simout['SQ'],axis=1)[-1]/16
initEQ = np.ones(Nc.shape[0])*np.mean(simout['EQ'],axis=1)[-1]/16
initSMQ = np.ones(Nc.shape[0])*np.mean(simout['SMQ'],axis=1)[-1]/16
initMQ = np.ones(Nc.shape[0])*np.mean(simout['MQ'],axis=1)[-1]/16
initRQ = np.ones(Nc.shape[0])*np.mean(simout['RQ'],axis=1)[-1]/16
```

```python
# Define some parameters
monteCarlo = False
n_samples=50
period = 7
P = 8
N = 4
discrete=True
ICU = 1200

# Perform brute force optimisation of horizon
x1 = np.linspace(1,3,3)
x2 = np.linspace(1,3,3)
x3 = np.linspace(1,3,3)
x4 = np.linspace(1,3,3)
x5 = np.linspace(1,3,3)
x6 = np.linspace(1,3,3)
SSE = np.zeros((x1.size,x2.size,x3.size,x4.size,x5.size,x6.size))
for i in range(x1.size):
    for j in range(x2.size):
        for k in range(x3.size):
            for l in range(x4.size):
                for m in range(x5.size):
                    for n in range(x6.size):
                        thetas=np.array([x1[i],x2[j],x3[k],x4[l],x5[m],x6[n]])
                        SSE[i,j,k,l,m,n] = cH.MPCcalcWeightsAge(thetas,initN,beta,zeta,dsm,dm,dhospital,dh,mc0,ICU,theta_S,theta_E,theta_SM,theta_M,theta_R,psi_FP,psi_PP,dq,phi_S,phi_E,phi_SM,phi_R,initE, initSM, initM, initH, initC,
                              initHH,initCH,initR,initF,initSQ,initEQ,initSMQ,initMQ,initRQ,simtime,monteCarlo,n_samples,method,modelType,discrete,period,P)
print(np.where(SSE == SSE.min()))
```

    (array([1, 1, 1, 1, 1, 1, 1, 1, 1]), array([1, 1, 1, 1, 1, 1, 1, 1, 1]), array([1, 1, 1, 1, 1, 1, 1, 1, 1]), array([1, 1, 1, 1, 1, 1, 1, 1, 1]), array([0, 0, 0, 1, 1, 1, 2, 2, 2]), array([0, 1, 2, 0, 1, 2, 0, 1, 2]))

```python
thetas=np.array([1,1,2,1])
Ncs=[]
for i in range(thetas.size):
    if thetas[i]<=1 and thetas[i]>=0:
        Ncs.append(Nc_all)
    elif thetas[i]<=2 and thetas[i]> 1:
        Ncs.append(Nc_home + Nc_schools + 0.01*(1-0.52)*Nc_work + 0.01*(1-0.70)*Nc_others)
    elif thetas[i]<=3 and thetas[i]> 2:
        Ncs.append(Nc_home + 0.01*(1-0.52)*Nc_work + 0.01*(1-0.70)*Nc_others)
```

```python
checkpoints = cH.constructHorizonAge(Ncs,period)
Nc = Nc_home + Nc_schools + 0.01*(1-0.52)*Nc_work + 0.01*(1-0.70)*Nc_others # Eerste checkpoint is maar na 7 dagen
policyVect = cH.constructHorizonPlot(thetas,period)
simtime = len(Ncs)*period

monteCarlo=True
n_samples=600
# --------------
# Run simulation
# --------------
simout = cH.simModel(initN,beta,Nc,zeta,dsm,dm,dhospital,dh,mc0,ICU,theta_S,theta_E,theta_SM,theta_M,
                     theta_R,psi_FP,psi_PP,dq,phi_S,phi_E,phi_SM,phi_R,initE, initSM, initM, initH, initC,
                    initHH,initCH,initR,initF,initSQ,initEQ,initSMQ,initMQ,initRQ,simtime,monteCarlo,
                     n_samples,method,modelType,checkpoints)
```

```python
# -----------
# Plot result
# -----------
t = simout['t']
I = simout['SM']+ simout['M'] + simout['H'] + simout['C'] + simout['HH'] + simout['CH']

plt.figure(1)
plt.plot(t,np.mean(simout['S'],axis=1),color="black")
plt.fill_between(t, np.percentile(simout['S'],90,axis=1), np.percentile(simout['S'],10,axis=1),color="black",alpha=0.2)
plt.plot(t,np.mean(simout['E'],axis=1),color="blue")
plt.fill_between(t, np.percentile(simout['E'],90,axis=1), np.percentile(simout['E'],10,axis=1),color="blue",alpha=0.2)
plt.plot(t,np.mean(I,axis=1),color="red")
plt.fill_between(t, np.percentile(I,90,axis=1), np.percentile(I,10,axis=1),color="red",alpha=0.2)
plt.plot(t,np.mean(simout['R'],axis=1),color="green")
plt.fill_between(t, np.percentile(simout['R'],90,axis=1), np.percentile(simout['R'],10,axis=1),color="green",alpha=0.2)
plt.legend(('susceptible','exposed','total infected','immune'))
plt.xlabel('days')
plt.ylabel('number of patients')

plt.figure(2)
plt.plot(t,np.mean(simout['CH'],axis=1),color="red")
plt.fill_between(t, np.percentile(simout['CH'],90,axis=1), np.percentile(simout['CH'],10,axis=1),color="red",alpha=0.2)
plt.plot(t,np.ones([t.size])*ICU,'--',color="red")
plt.legend(('critical patients','ICU capacity'))
plt.xlabel('days')
plt.ylabel('number of patients')
ax2 = plt.twinx()
plt.plot(t[:-1],policyVect[:-1],'--',color='black')
ax2.set_ylabel("Daily random social interactions")
```

notebooks/scratch/[Experimental] Calibation of Age-Structured deterministic model (2).ipynb


```python
### Usar este ejemplo como base, para resolver el problema completo.
```

```python
from sympy import *
from sympy import init_printing; init_printing(use_latex = 'mathjax')
```

```python
#var('x l m hbar W c')
```

```python
x, l, m, hbar, W, c = symbols('x l m hbar W c')
```

```python
f_1 = x*(l - x)
f_2 = (x**2)*((l - x)**2)
f_3 = x*(l - x)*((l/2)-x)
f_4 = (x**2)*((l - x)**2)*((l/2)-x)
```

```python
FUNC = [f_1,f_2,f_3,f_4]

```

```python
n=4
c=[]
for i in range (n):
    c.append(Symbol('c_%d' %(i+1)))
```

```python
c = Matrix([var('c1')])
c = c.row_insert(1, Matrix([var('c2')]))
c = c.row_insert(2, Matrix([var('c3')]))
c = c.row_insert(3, Matrix([var('c4')]))
c
```

$$\left[\begin{matrix}c_{1}\\c_{2}\\c_{3}\\c_{4}\end{matrix}\right]$$

```python
n = 4
U = ones(n,n)
H = zeros(n,n)
s = zeros(n,n)
for i in range(n):
    for j in range(n):
        integrando = (-hbar**2/(2*m))* FUNC[i] *diff(FUNC[j], x, 2)
        H[i,j]= integrate(integrando, (x, 0, l))
        s[i,j] = integrate(FUNC[i]*FUNC[j], (x,0,l))

mult = H - s*W
```

```python
mult
```

$$\left[\begin{matrix}- \frac{W l^{5}}{30} + \frac{\hbar^{2} l^{3}}{6 m} & - \frac{W l^{7}}{140} + \frac{\hbar^{2} l^{5}}{30 m} & 0 & 0\\- \frac{W l^{7}}{140} + \frac{\hbar^{2} l^{5}}{30 m} & - \frac{W l^{9}}{630} + \frac{\hbar^{2} l^{7}}{105 m} & 0 & 0\\0 & 0 & - \frac{W l^{7}}{840} + \frac{\hbar^{2} l^{5}}{40 m} & - \frac{W l^{9}}{5040} + \frac{\hbar^{2} l^{7}}{280 m}\\0 & 0 & - \frac{W l^{9}}{5040} + \frac{\hbar^{2} l^{7}}{280 m} & - \frac{W l^{11}}{27720} + \frac{\hbar^{2} l^{9}}{1260 m}\end{matrix}\right]$$

```python
determinante = mult.det()
```

```python
determinante
```

$$\frac{W^{4} l^{32}}{147867793920000} - \frac{W^{3} \hbar^{2} l^{30}}{840157920000 m} + \frac{373 W^{2} \hbar^{4} l^{28}}{6161158080000 m^{2}} - \frac{W \hbar^{6} l^{26}}{1047816000 m^{3}} + \frac{\hbar^{8} l^{24}}{296352000 m^{4}}$$

```python
e = solve(determinante,W)
```

```python
e = Matrix([e])
e
```

$$\left[\begin{matrix}\frac{2 \hbar^{2}}{l^{2} m} \left(- \sqrt{133} + 14\right) & \frac{2 \hbar^{2}}{l^{2} m} \left(\sqrt{133} + 14\right) & \frac{2 \hbar^{2}}{l^{2} m} \left(- 9 \sqrt{5} + 30\right) & \frac{2 \hbar^{2}}{l^{2} m} \left(9 \sqrt{5} + 30\right)\end{matrix}\right]$$

```python
e = e*((l**2*m)/(hbar**2))
e
```

$$\left[\begin{matrix}- 2 \sqrt{133} + 28 & 2 \sqrt{133} + 28 & - 18 \sqrt{5} + 60 & 18 \sqrt{5} + 60\end{matrix}\right]$$

```python
#sorted(e_lista)
e = Matrix([sorted(e)])
e
```

$$\left[\begin{matrix}- 2 \sqrt{133} + 28 & - 18 \sqrt{5} + 60 & 2 \sqrt{133} + 28 & 18 \sqrt{5} + 60\end{matrix}\right]$$

```python
e = e*(hbar**2)/(l**2*m)
e
```

$$\left[\begin{matrix}\frac{\hbar^{2}}{l^{2} m} \left(- 2 \sqrt{133} + 28\right) & \frac{\hbar^{2}}{l^{2} m} \left(- 18 \sqrt{5} + 60\right) & \frac{\hbar^{2}}{l^{2} m} \left(2 \sqrt{133} + 28\right) & \frac{\hbar^{2}}{l^{2} m} \left(18 \sqrt{5} + 60\right)\end{matrix}\right]$$

```python
mult = H - s*e[0]
mult
```

$$\left[\begin{matrix}- \frac{\hbar^{2} l^{3}}{30 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{3}}{6 m} & - \frac{\hbar^{2} l^{5}}{140 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{5}}{30 m} & 0 & 0\\- \frac{\hbar^{2} l^{5}}{140 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{5}}{30 m} & - \frac{\hbar^{2} l^{7}}{630 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{7}}{105 m} & 0 & 0\\0 & 0 & - \frac{\hbar^{2} l^{5}}{840 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{5}}{40 m} & - \frac{\hbar^{2} l^{7}}{5040 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{7}}{280 m}\\0 & 0 & - \frac{\hbar^{2} l^{7}}{5040 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{7}}{280 m} & - \frac{\hbar^{2} l^{9}}{27720 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{9}}{1260 m}\end{matrix}\right]$$

```python
res1 = H - s*e[0]

res1

```

$$\left[\begin{matrix}- \frac{\hbar^{2} l^{3}}{30 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{3}}{6 m} & - \frac{\hbar^{2} l^{5}}{140 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{5}}{30 m} & 0 & 0\\- \frac{\hbar^{2} l^{5}}{140 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{5}}{30 m} & - \frac{\hbar^{2} l^{7}}{630 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{7}}{105 m} & 0 & 0\\0 & 0 & - \frac{\hbar^{2} l^{5}}{840 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{5}}{40 m} & - \frac{\hbar^{2} l^{7}}{5040 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{7}}{280 m}\\0 & 0 & - \frac{\hbar^{2} l^{7}}{5040 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{7}}{280 m} & - \frac{\hbar^{2} l^{9}}{27720 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{9}}{1260 m}\end{matrix}\right]$$

```python
res2 = H - s*e[1]

res2
```

$$\left[\begin{matrix}- \frac{\hbar^{2} l^{3}}{30 m} \left(- 18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{3}}{6 m} & - \frac{\hbar^{2} l^{5}}{140 m} \left(- 18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{5}}{30 m} & 0 & 0\\- \frac{\hbar^{2} l^{5}}{140 m} \left(- 18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{5}}{30 m} & - \frac{\hbar^{2} l^{7}}{630 m} \left(- 18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{7}}{105 m} & 0 & 0\\0 & 0 & - \frac{\hbar^{2} l^{5}}{840 m} \left(- 18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{5}}{40 m} & - \frac{\hbar^{2} l^{7}}{5040 m} \left(- 18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{7}}{280 m}\\0 & 0 & - \frac{\hbar^{2} l^{7}}{5040 m} \left(- 18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{7}}{280 m} & - \frac{\hbar^{2} l^{9}}{27720 m} \left(- 18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{9}}{1260 m}\end{matrix}\right]$$

```python
res3 = H - s*e[2]

res3
```

$$\left[\begin{matrix}- \frac{\hbar^{2} l^{3}}{30 m} \left(2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{3}}{6 m} & - \frac{\hbar^{2} l^{5}}{140 m} \left(2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{5}}{30 m} & 0 & 0\\- \frac{\hbar^{2} l^{5}}{140 m} \left(2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{5}}{30 m} & - \frac{\hbar^{2} l^{7}}{630 m} \left(2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{7}}{105 m} & 0 & 0\\0 & 0 & - \frac{\hbar^{2} l^{5}}{840 m} \left(2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{5}}{40 m} & - \frac{\hbar^{2} l^{7}}{5040 m} \left(2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{7}}{280 m}\\0 & 0 & - \frac{\hbar^{2} l^{7}}{5040 m} \left(2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{7}}{280 m} & - \frac{\hbar^{2} l^{9}}{27720 m} \left(2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{9}}{1260 m}\end{matrix}\right]$$

```python
res4 = H - s*e[3]

res4
```

$$\left[\begin{matrix}- \frac{\hbar^{2} l^{3}}{30 m} \left(18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{3}}{6 m} & - \frac{\hbar^{2} l^{5}}{140 m} \left(18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{5}}{30 m} & 0 & 0\\- \frac{\hbar^{2} l^{5}}{140 m} \left(18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{5}}{30 m} & - \frac{\hbar^{2} l^{7}}{630 m} \left(18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{7}}{105 m} & 0 & 0\\0 & 0 & - \frac{\hbar^{2} l^{5}}{840 m} \left(18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{5}}{40 m} & - \frac{\hbar^{2} l^{7}}{5040 m} \left(18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{7}}{280 m}\\0 & 0 & - \frac{\hbar^{2} l^{7}}{5040 m} \left(18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{7}}{280 m} & - \frac{\hbar^{2} l^{9}}{27720 m} \left(18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{9}}{1260 m}\end{matrix}\right]$$

```python
c = Matrix(c)
```

```python
c
```

$$\left[\begin{matrix}c_{1}\\c_{2}\\c_{3}\\c_{4}\end{matrix}\right]$$

```python
f1 = res1*c
f1
```

$$\left[\begin{matrix}c_{1} \left(- \frac{\hbar^{2} l^{3}}{30 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{3}}{6 m}\right) + c_{2} \left(- \frac{\hbar^{2} l^{5}}{140 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{5}}{30 m}\right)\\c_{1} \left(- \frac{\hbar^{2} l^{5}}{140 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{5}}{30 m}\right) + c_{2} \left(- \frac{\hbar^{2} l^{7}}{630 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{7}}{105 m}\right)\\c_{3} \left(- \frac{\hbar^{2} l^{5}}{840 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{5}}{40 m}\right) + c_{4} \left(- \frac{\hbar^{2} l^{7}}{5040 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{7}}{280 m}\right)\\c_{3} \left(- \frac{\hbar^{2} l^{7}}{5040 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{7}}{280 m}\right) + c_{4} \left(- \frac{\hbar^{2} l^{9}}{27720 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{9}}{1260 m}\right)\end{matrix}\right]$$

```python
f2 = res2*c
f2
```

$$\left[\begin{matrix}c_{1} \left(- \frac{\hbar^{2} l^{3}}{30 m} \left(- 18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{3}}{6 m}\right) + c_{2} \left(- \frac{\hbar^{2} l^{5}}{140 m} \left(- 18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{5}}{30 m}\right)\\c_{1} \left(- \frac{\hbar^{2} l^{5}}{140 m} \left(- 18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{5}}{30 m}\right) + c_{2} \left(- \frac{\hbar^{2} l^{7}}{630 m} \left(- 18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{7}}{105 m}\right)\\c_{3} \left(- \frac{\hbar^{2} l^{5}}{840 m} \left(- 18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{5}}{40 m}\right) + c_{4} \left(- \frac{\hbar^{2} l^{7}}{5040 m} \left(- 18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{7}}{280 m}\right)\\c_{3} \left(- \frac{\hbar^{2} l^{7}}{5040 m} \left(- 18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{7}}{280 m}\right) + c_{4} \left(- \frac{\hbar^{2} l^{9}}{27720 m} \left(- 18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{9}}{1260 m}\right)\end{matrix}\right]$$

```python
f3 = res3*c
f3
```

$$\left[\begin{matrix}c_{1} \left(- \frac{\hbar^{2} l^{3}}{30 m} \left(2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{3}}{6 m}\right) + c_{2} \left(- \frac{\hbar^{2} l^{5}}{140 m} \left(2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{5}}{30 m}\right)\\c_{1} \left(- \frac{\hbar^{2} l^{5}}{140 m} \left(2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{5}}{30 m}\right) + c_{2} \left(- \frac{\hbar^{2} l^{7}}{630 m} \left(2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{7}}{105 m}\right)\\c_{3} \left(- \frac{\hbar^{2} l^{5}}{840 m} \left(2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{5}}{40 m}\right) + c_{4} \left(- \frac{\hbar^{2} l^{7}}{5040 m} \left(2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{7}}{280 m}\right)\\c_{3} \left(- \frac{\hbar^{2} l^{7}}{5040 m} \left(2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{7}}{280 m}\right) + c_{4} \left(- \frac{\hbar^{2} l^{9}}{27720 m} \left(2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{9}}{1260 m}\right)\end{matrix}\right]$$

```python
f4 = res4*c
f4
```

$$\left[\begin{matrix}c_{1} \left(- \frac{\hbar^{2} l^{3}}{30 m} \left(18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{3}}{6 m}\right) + c_{2} \left(- \frac{\hbar^{2} l^{5}}{140 m} \left(18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{5}}{30 m}\right)\\c_{1} \left(- \frac{\hbar^{2} l^{5}}{140 m} \left(18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{5}}{30 m}\right) + c_{2} \left(- \frac{\hbar^{2} l^{7}}{630 m} \left(18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{7}}{105 m}\right)\\c_{3} \left(- \frac{\hbar^{2} l^{5}}{840 m} \left(18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{5}}{40 m}\right) + c_{4} \left(- \frac{\hbar^{2} l^{7}}{5040 m} \left(18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{7}}{280 m}\right)\\c_{3} \left(- \frac{\hbar^{2} l^{7}}{5040 m} \left(18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{7}}{280 m}\right) + c_{4} \left(- \frac{\hbar^{2} l^{9}}{27720 m} \left(18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{9}}{1260 m}\right)\end{matrix}\right]$$

```python
r1 = solve(f1, c)
r2 = solve(f2, c)
r3 = solve(f3, c)
r4 = solve(f4, c)
r1[c[0]]
```

$$\frac{c_{2} l^{2}}{3} + \frac{\sqrt{133} c_{2}}{21} l^{2}$$

```python
r = [r1, r2, r3, r4]
r
```

$$\left [ \left \{ c_{1} : \frac{c_{2} l^{2}}{3} + \frac{\sqrt{133} c_{2}}{21} l^{2}, \quad c_{3} : 0, \quad c_{4} : 0\right \}, \quad \left \{ c_{1} : 0, \quad c_{2} : 0, \quad c_{3} : \frac{c_{4} l^{2}}{33} + \frac{\sqrt{5} c_{4}}{11} l^{2}\right \}, \quad \left \{ c_{1} : - \frac{\sqrt{133} c_{2}}{21} l^{2} + \frac{c_{2} l^{2}}{3}, \quad c_{3} : 0, \quad c_{4} : 0\right \}, \quad \left \{ c_{1} : 0, \quad c_{2} : 0, \quad c_{3} : - \frac{\sqrt{5} c_{4}}{11} l^{2} + \frac{c_{4} l^{2}}{33}\right \}\right ]$$

```python
EC = Matrix([f1])
EC = EC.col_insert(1,f2)
EC = EC.col_insert(2,f3)
EC = EC.col_insert(3,f4)
EC
```

$$\left[\begin{matrix}c_{1} \left(- \frac{\hbar^{2} l^{3}}{30 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{3}}{6 m}\right) + c_{2} \left(- \frac{\hbar^{2} l^{5}}{140 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{5}}{30 m}\right) & c_{1} \left(- \frac{\hbar^{2} l^{3}}{30 m} \left(- 18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{3}}{6 m}\right) + c_{2} \left(- \frac{\hbar^{2} l^{5}}{140 m} \left(- 18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{5}}{30 m}\right) & c_{1} \left(- \frac{\hbar^{2} l^{3}}{30 m} \left(2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{3}}{6 m}\right) + c_{2} \left(- \frac{\hbar^{2} l^{5}}{140 m} \left(2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{5}}{30 m}\right) & c_{1} \left(- \frac{\hbar^{2} l^{3}}{30 m} \left(18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{3}}{6 m}\right) + c_{2} \left(- \frac{\hbar^{2} l^{5}}{140 m} \left(18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{5}}{30 m}\right)\\c_{1} \left(- \frac{\hbar^{2} l^{5}}{140 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{5}}{30 m}\right) + c_{2} \left(- \frac{\hbar^{2} l^{7}}{630 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{7}}{105 m}\right) & c_{1} \left(- \frac{\hbar^{2} l^{5}}{140 m} \left(- 18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{5}}{30 m}\right) + c_{2} \left(- \frac{\hbar^{2} l^{7}}{630 m} \left(- 18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{7}}{105 m}\right) & c_{1} \left(- \frac{\hbar^{2} l^{5}}{140 m} \left(2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{5}}{30 m}\right) + c_{2} \left(- \frac{\hbar^{2} l^{7}}{630 m} \left(2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{7}}{105 m}\right) & c_{1} \left(- \frac{\hbar^{2} l^{5}}{140 m} \left(18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{5}}{30 m}\right) + c_{2} \left(- \frac{\hbar^{2} l^{7}}{630 m} \left(18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{7}}{105 m}\right)\\c_{3} \left(- \frac{\hbar^{2} l^{5}}{840 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{5}}{40 m}\right) + c_{4} \left(- \frac{\hbar^{2} l^{7}}{5040 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{7}}{280 m}\right) & c_{3} \left(- \frac{\hbar^{2} l^{5}}{840 m} \left(- 18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{5}}{40 m}\right) + c_{4} \left(- \frac{\hbar^{2} l^{7}}{5040 m} \left(- 18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{7}}{280 m}\right) & c_{3} \left(- \frac{\hbar^{2} l^{5}}{840 m} \left(2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{5}}{40 m}\right) + c_{4} \left(- \frac{\hbar^{2} l^{7}}{5040 m} \left(2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{7}}{280 m}\right) & c_{3} \left(- \frac{\hbar^{2} l^{5}}{840 m} \left(18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{5}}{40 m}\right) + c_{4} \left(- \frac{\hbar^{2} l^{7}}{5040 m} \left(18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{7}}{280 m}\right)\\c_{3} \left(- \frac{\hbar^{2} l^{7}}{5040 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{7}}{280 m}\right) + c_{4} \left(- \frac{\hbar^{2} l^{9}}{27720 m} \left(- 2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{9}}{1260 m}\right) & c_{3} \left(- \frac{\hbar^{2} l^{7}}{5040 m} \left(- 18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{7}}{280 m}\right) + c_{4} \left(- \frac{\hbar^{2} l^{9}}{27720 m} \left(- 18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{9}}{1260 m}\right) & c_{3} \left(- \frac{\hbar^{2} l^{7}}{5040 m} \left(2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{7}}{280 m}\right) + c_{4} \left(- \frac{\hbar^{2} l^{9}}{27720 m} \left(2 \sqrt{133} + 28\right) + \frac{\hbar^{2} l^{9}}{1260 m}\right) & c_{3} \left(- \frac{\hbar^{2} l^{7}}{5040 m} \left(18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{7}}{280 m}\right) + c_{4} \left(- \frac{\hbar^{2} l^{9}}{27720 m} \left(18 \sqrt{5} + 60\right) + \frac{\hbar^{2} l^{9}}{1260 m}\right)\end{matrix}\right]$$

```python
C = Matrix(c)
C
```

$$\left[\begin{matrix}c_{1}\\c_{2}\\c_{3}\\c_{4}\end{matrix}\right]$$

```python
CRcol = C[:, :]
for a in range(1, len(C)+1):
    C_val = solve(EC.col(a-1), C)
    for b in range(1, len(C)+1):
        if sympify('c'+str(b)) in C_val:
            CRcol[b-1] = C_val[sympify('c'+str(b))]
        else:
            CRcol[b-1] = sympify('c'+str(b))
    if a == 1:
        CR = Matrix([CRcol])
    else:
        CR = CR.col_insert(a-1, CRcol)
simplify(CR)
```

$$\left[\begin{matrix}\frac{c_{2} l^{2}}{21} \left(7 + \sqrt{133}\right) & 0 & \frac{c_{2} l^{2}}{21} \left(- \sqrt{133} + 7\right) & 0\\c_{2} & 0 & c_{2} & 0\\0 & \frac{c_{4} l^{2}}{33} \left(1 + 3 \sqrt{5}\right) & 0 & \frac{c_{4} l^{2}}{33} \left(- 3 \sqrt{5} + 1\right)\\0 & c_{4} & 0 & c_{4}\end{matrix}\right]$$

```python
CR.T
```

$$\left[\begin{matrix}\frac{c_{2} l^{2}}{3} + \frac{\sqrt{133} c_{2}}{21} l^{2} & c_{2} & 0 & 0\\0 & 0 & \frac{c_{4} l^{2}}{33} + \frac{\sqrt{5} c_{4}}{11} l^{2} & c_{4}\\- \frac{\sqrt{133} c_{2}}{21} l^{2} + \frac{c_{2} l^{2}}{3} & c_{2} & 0 & 0\\0 & 0 & - \frac{\sqrt{5} c_{4}}{11} l^{2} + \frac{c_{4} l^{2}}{33} & c_{4}\end{matrix}\right]$$

```python
FUNC = Matrix(FUNC)
FUNC
```

$$\left[\begin{matrix}x \left(l - x\right)\\x^{2} \left(l - x\right)^{2}\\x \left(\frac{l}{2} - x\right) \left(l - x\right)\\x^{2} \left(\frac{l}{2} - x\right) \left(l - x\right)^{2}\end{matrix}\right]$$

```python
PHIS = CR.T*FUNC
PHIS
```

$$\left[\begin{matrix}c_{2} x^{2} \left(l - x\right)^{2} + x \left(l - x\right) \left(\frac{c_{2} l^{2}}{3} + \frac{\sqrt{133} c_{2}}{21} l^{2}\right)\\c_{4} x^{2} \left(\frac{l}{2} - x\right) \left(l - x\right)^{2} + x \left(\frac{l}{2} - x\right) \left(l - x\right) \left(\frac{c_{4} l^{2}}{33} + \frac{\sqrt{5} c_{4}}{11} l^{2}\right)\\c_{2} x^{2} \left(l - x\right)^{2} + x \left(l - x\right) \left(- \frac{\sqrt{133} c_{2}}{21} l^{2} + \frac{c_{2} l^{2}}{3}\right)\\c_{4} x^{2} \left(\frac{l}{2} - x\right) \left(l - x\right)^{2} + x \left(\frac{l}{2} - x\right) \left(l - x\right) \left(- \frac{\sqrt{5} c_{4}}{11} l^{2} + \frac{c_{4} l^{2}}{33}\right)\end{matrix}\right]$$

```python
for i in range (0,4):
    PHIS[i] = PHIS[i]**2
PHIS
```

$$\left[\begin{matrix}\left(c_{2} x^{2} \left(l - x\right)^{2} + x \left(l - x\right) \left(\frac{c_{2} l^{2}}{3} + \frac{\sqrt{133} c_{2}}{21} l^{2}\right)\right)^{2}\\\left(c_{4} x^{2} \left(\frac{l}{2} - x\right) \left(l - x\right)^{2} + x \left(\frac{l}{2} - x\right) \left(l - x\right) \left(\frac{c_{4} l^{2}}{33} + \frac{\sqrt{5} c_{4}}{11} l^{2}\right)\right)^{2}\\\left(c_{2} x^{2} \left(l - x\right)^{2} + x \left(l - x\right) \left(- \frac{\sqrt{133} c_{2}}{21} l^{2} + \frac{c_{2} l^{2}}{3}\right)\right)^{2}\\\left(c_{4} x^{2} \left(\frac{l}{2} - x\right) \left(l - x\right)^{2} + x \left(\frac{l}{2} - x\right) \left(l - x\right) \left(- \frac{\sqrt{5} c_{4}}{11} l^{2} + \frac{c_{4} l^{2}}{33}\right)\right)^{2}\end{matrix}\right]$$

```python
#integrate de phis con respecto a x de 0 a l y el resultado; al termino 1, le resto 1 y eso lo resuelvo para c2 y así con
PHISI = integrate(PHIS, (x, 0, l))
PHISI
```

$$\left[\begin{matrix}- \frac{7 c_{2}^{2}}{18} l^{9} + l^{7} \left(- \frac{2 \sqrt{133}}{147} c_{2}^{2} l^{2} + \frac{16 c_{2}^{2}}{21} l^{2}\right) + l^{6} \left(- \frac{c_{2}^{2} l^{3}}{3} + \frac{\sqrt{133} c_{2}^{2}}{21} l^{3}\right) + l^{5} \left(- \frac{16 \sqrt{133}}{315} c_{2}^{2} l^{4} - \frac{37 c_{2}^{2}}{315} l^{4}\right) + l^{4} \left(- \frac{5 c_{2}^{2}}{126} l^{5} + \frac{\sqrt{133} c_{2}^{2}}{126} l^{5}\right) + l^{3} \left(\frac{2 \sqrt{133}}{189} c_{2}^{2} l^{6} + \frac{26 c_{2}^{2}}{189} l^{6}\right)\\- \frac{9 c_{4}^{2}}{22} l^{11} + l^{9} \left(- \frac{2 \sqrt{5}}{99} c_{4}^{2} l^{2} + \frac{1345 c_{4}^{2}}{1188} l^{2}\right) + l^{8} \left(- \frac{355 c_{4}^{2}}{264} l^{3} + \frac{\sqrt{5} c_{4}^{2}}{11} l^{3}\right) + l^{7} \left(- \frac{821 \sqrt{5}}{5082} c_{4}^{2} l^{4} + \frac{6712 c_{4}^{2}}{7623} l^{4}\right) + l^{6} \left(- \frac{445 c_{4}^{2}}{1452} l^{5} + \frac{205 \sqrt{5}}{1452} c_{4}^{2} l^{5}\right) + l^{5} \left(- \frac{109 \sqrt{5}}{1815} c_{4}^{2} l^{6} + \frac{245 c_{4}^{2}}{4356} l^{6}\right) + l^{4} \left(- \frac{35 c_{4}^{2}}{2904} l^{7} + \frac{9 \sqrt{5}}{968} c_{4}^{2} l^{7}\right) + l^{3} \left(\frac{\sqrt{5} c_{4}^{2}}{2178} l^{8} + \frac{23 c_{4}^{2}}{6534} l^{8}\right)\\- \frac{7 c_{2}^{2}}{18} l^{9} + l^{7} \left(\frac{2 \sqrt{133}}{147} c_{2}^{2} l^{2} + \frac{16 c_{2}^{2}}{21} l^{2}\right) + l^{6} \left(- \frac{\sqrt{133} c_{2}^{2}}{21} l^{3} - \frac{c_{2}^{2} l^{3}}{3}\right) + l^{5} \left(- \frac{37 c_{2}^{2}}{315} l^{4} + \frac{16 \sqrt{133}}{315} c_{2}^{2} l^{4}\right) + l^{4} \left(- \frac{\sqrt{133} c_{2}^{2}}{126} l^{5} - \frac{5 c_{2}^{2}}{126} l^{5}\right) + l^{3} \left(- \frac{2 \sqrt{133}}{189} c_{2}^{2} l^{6} + \frac{26 c_{2}^{2}}{189} l^{6}\right)\\- \frac{9 c_{4}^{2}}{22} l^{11} + l^{9} \left(\frac{2 \sqrt{5}}{99} c_{4}^{2} l^{2} + \frac{1345 c_{4}^{2}}{1188} l^{2}\right) + l^{8} \left(- \frac{355 c_{4}^{2}}{264} l^{3} - \frac{\sqrt{5} c_{4}^{2}}{11} l^{3}\right) + l^{7} \left(\frac{821 \sqrt{5}}{5082} c_{4}^{2} l^{4} + \frac{6712 c_{4}^{2}}{7623} l^{4}\right) + l^{6} \left(- \frac{205 \sqrt{5}}{1452} c_{4}^{2} l^{5} - \frac{445 c_{4}^{2}}{1452} l^{5}\right) + l^{5} \left(\frac{245 c_{4}^{2}}{4356} l^{6} + \frac{109 \sqrt{5}}{1815} c_{4}^{2} l^{6}\right) + l^{4} \left(- \frac{9 \sqrt{5}}{968} c_{4}^{2} l^{7} - \frac{35 c_{4}^{2}}{2904} l^{7}\right) + l^{3} \left(- \frac{\sqrt{5} c_{4}^{2}}{2178} l^{8} + \frac{23 c_{4}^{2}}{6534} l^{8}\right)\end{matrix}\right]$$

```python
c2 = PHISI[0]-1
c2
```

$$- \frac{7 c_{2}^{2}}{18} l^{9} + l^{7} \left(- \frac{2 \sqrt{133}}{147} c_{2}^{2} l^{2} + \frac{16 c_{2}^{2}}{21} l^{2}\right) + l^{6} \left(- \frac{c_{2}^{2} l^{3}}{3} + \frac{\sqrt{133} c_{2}^{2}}{21} l^{3}\right) + l^{5} \left(- \frac{16 \sqrt{133}}{315} c_{2}^{2} l^{4} - \frac{37 c_{2}^{2}}{315} l^{4}\right) + l^{4} \left(- \frac{5 c_{2}^{2}}{126} l^{5} + \frac{\sqrt{133} c_{2}^{2}}{126} l^{5}\right) + l^{3} \left(\frac{2 \sqrt{133}}{189} c_{2}^{2} l^{6} + \frac{26 c_{2}^{2}}{189} l^{6}\right) - 1$$

```python
solve(c2, C[1])
```

$$\left [ - \frac{21 \sqrt{30} \sqrt{\frac{1}{l^{9}}}}{\sqrt{23 \sqrt{133} + 266}}, \quad \frac{21 \sqrt{30} \sqrt{\frac{1}{l^{9}}}}{\sqrt{23 \sqrt{133} + 266}}\right ]$$

```python
C[1] = solve(c2, C[1])
C[1]
```

$$- \frac{21 \sqrt{30} \sqrt{\frac{1}{l^{9}}}}{\sqrt{23 \sqrt{133} + 266}}$$

```python
c4 = PHISI[3]-1
c4
```

$$- \frac{9 c_{4}^{2}}{22} l^{11} + l^{9} \left(\frac{2 \sqrt{5}}{99} c_{4}^{2} l^{2} + \frac{1345 c_{4}^{2}}{1188} l^{2}\right) + l^{8} \left(- \frac{355 c_{4}^{2}}{264} l^{3} - \frac{\sqrt{5} c_{4}^{2}}{11} l^{3}\right) + l^{7} \left(\frac{821 \sqrt{5}}{5082} c_{4}^{2} l^{4} + \frac{6712 c_{4}^{2}}{7623} l^{4}\right) + l^{6} \left(- \frac{205 \sqrt{5}}{1452} c_{4}^{2} l^{5} - \frac{445 c_{4}^{2}}{1452} l^{5}\right) + l^{5} \left(\frac{245 c_{4}^{2}}{4356} l^{6} + \frac{109 \sqrt{5}}{1815} c_{4}^{2} l^{6}\right) + l^{4} \left(- \frac{9 \sqrt{5}}{968} c_{4}^{2} l^{7} - \frac{35 c_{4}^{2}}{2904} l^{7}\right) + l^{3} \left(- \frac{\sqrt{5} c_{4}^{2}}{2178} l^{8} + \frac{23 c_{4}^{2}}{6534} l^{8}\right) - 1$$

```python
solve(c4,C[3])
```

$$\left [ - \frac{66 \sqrt{70} \sqrt{\frac{1}{l^{11}}}}{\sqrt{- 13 \sqrt{5} + 30}}, \quad \frac{66 \sqrt{70} \sqrt{\frac{1}{l^{11}}}}{\sqrt{- 13 \sqrt{5} + 30}}\right ]$$

```python

```

```python
CRcol = C[:, :]
for a in range(1, len(C)+1):
    C_val = solve(EC.col(a-1), C)
    for b in range(1, len(C)+1):
        if sympify('c'+str(b)) in C_val:
            CRcol[b-1] = C_val[sympify('c'+str(b))]
        else:
            CRcol[b-1] = sympify('c'+str(b))
    if a == 1:
        CR = Matrix([CRcol])
    else:
        CR = CR.col_insert(a-1, CRcol)
simplify(CR)
```

$$\left[\begin{matrix}c_{1} & c_{1} & c_{1} & c_{1}\\c_{2} & c_{2} & c_{2} & c_{2}\\c_{3} & c_{3} & c_{3} & c_{3}\\c_{4} & c_{4} & c_{4} & c_{4}\end{matrix}\right]$$

```python

```

Ejemplo variational principle Zulema Cruz.ipynb