fromparestoimportParestoimportnumpyasnpimportmatplotlib.pyplotaspltimportosplt.ion()# Define parametersca0=2k=1tfinal=5nts=100tout=np.linspace(0,tfinal,nts)# Initialize the modelpe=Paresto()pe.transcription='shooting'pe.x=['ca']pe.p=['k']pe.d=['m_ca']pe.tout=tout# Define the ODE functionpe.ode=lambdat,y,p:[-p.k*y.ca]# Define the least squares functionpe.lsq=lambdat,y,p:[y.ca-y.m_ca]# Initialize the Paresto objectpe.initialize()# Initialize state and parametersx0=ca0y_ac,z_ac=pe.simulate({'k':k,'ca':x0})# Initialize parameters and boundspe.ic.k=kpe.ic.ca=ca0pe.lb_ub()# lb and ub are copies of ic (all equal), so parameters are fixed# Optimize the parametersest,y,p=pe.optimize(y_ac)conf=pe.confidence(est,0.95)table=np.column_stack((tout,y['ca'].reshape(nts),est.dca_dk.reshape(nts),est.dca_dca0.reshape(nts)))print('Estimated parameters')print(f' ca_final = {table[-1,1]:.6g}')print(f' dca_dk_final = {table[-1,2]:.6g}')print(f' dca_dca0_final = {table[-1,3]:.6g}')# Save the data to a .dat filenp.savetxt('Sfirstorder.dat',table,fmt='%.6e')# Conditional plottingifnotos.getenv('OMIT_PLOTS')=='true':plt.plot(tout,y['ca'].reshape(nts),label='y.ca')plt.plot(tout,est.dca_dk.reshape(nts),label='est.dca_dk')plt.plot(tout,est.dca_dca0.reshape(nts),label='est.dca_dca0')plt.show()