%23%20%2F%2F%2F%20script%0A%23%20requires-python%20%3D%20%22%3E%3D3.14%22%0A%23%20dependencies%20%3D%20%5B%0A%23%20%20%20%20%20%22marimo%3E%3D0.21.1%22%2C%0A%23%20%5D%0A%23%20%2F%2F%2F%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.23.16%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0Aasync%20with%20app.setup(hide_code%3DTrue)%3A%0A%20%20%20%20%23%20Setup%20cell%20for%20marimo%20notebook%2C%20can%20be%20ignored%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20sys%0A%0A%20%20%20%20%23%20Import%20packages%20if%20running%20on%20marimo%20playground%0A%20%20%20%20if%20sys.executable%20%3D%3D%20%22%2Fhome%2Fpyodide%2Fthis.program%22%3A%0A%20%20%20%20%20%20%20%20import%20micropip%0A%0A%20%20%20%20%20%20%20%20await%20micropip.install(%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B%22pint_pandas%3C%3D0.7%22%2C%20%22typing_extensions%3E%3D4.15.0%22%2C%20%22poincare%3E%3D1.3.0%22%2C%20%22matplotlib%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20verbose%3DFalse%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Fitting%20models%20to%20data%0A%20%20%20%20Poincare%20can%20fit%20model%20parameters%20(and%20initial%20conditions)%20to%20experimental%20or%20expected%20results.%20The%20tools%20live%20in%20%60poincare.analysis%60%3A%0A%0A%20%20%20%20-%20%60make_target%60%20builds%20the%20data%20to%20fit%20in%20the%20same%20format%20as%20simulation%20results.%0A%20%20%20%20-%20%60fit_result%60%20fits%20one%20time%20course.%0A%20%20%20%20-%20%60fit_steady_state%60%20fits%20steady%20states%20measured%20at%20different%20parameter%20values.%0A%20%20%20%20-%20%60Fitter%60%20fits%20several%20experiments%20at%20once%2C%20with%20shared%20parameters.%0A%0A%20%20%20%20All%20of%20them%20use%20%5B%60scipy.optimize.least_squares%60%5D(https%3A%2F%2Fdocs.scipy.org%2Fdoc%2Fscipy%2Freference%2Fgenerated%2Fscipy.optimize.least_squares.html)%20and%20return%20the%20best%20values%20together%20with%20their%20covariance.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Fitting%20a%20time%20course%0A%20%20%20%20First%20we%20define%20a%20model%20to%20fit%2C%20a%20harmonic%20oscillator%20with%20unknown%20frequency%20%60omega%60%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%0A%20%20%20%20from%20poincare%20import%20(%0A%20%20%20%20%20%20%20%20Derivative%2C%0A%20%20%20%20%20%20%20%20Parameter%2C%0A%20%20%20%20%20%20%20%20Simulator%2C%0A%20%20%20%20%20%20%20%20System%2C%0A%20%20%20%20%20%20%20%20Variable%2C%0A%20%20%20%20%20%20%20%20assign%2C%0A%20%20%20%20%20%20%20%20initial%2C%0A%20%20%20%20%20%20%20%20solvers%2C%0A%20%20%20%20)%0A%0A%20%20%20%20class%20Oscillator(System)%3A%0A%20%20%20%20%20%20%20%20x%3A%20Variable%20%3D%20initial(default%3D1)%0A%20%20%20%20%20%20%20%20vx%3A%20Derivative%20%3D%20x.derive(initial%3D0)%0A%0A%20%20%20%20%20%20%20%20omega%3A%20Parameter%20%3D%20assign(default%3D1)%0A%0A%20%20%20%20%20%20%20%20spring%20%3D%20vx.derive()%20%3C%3C%20-(omega**2)%20*%20x%0A%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20Derivative%2C%0A%20%20%20%20%20%20%20%20Oscillator%2C%0A%20%20%20%20%20%20%20%20Parameter%2C%0A%20%20%20%20%20%20%20%20Simulator%2C%0A%20%20%20%20%20%20%20%20System%2C%0A%20%20%20%20%20%20%20%20Variable%2C%0A%20%20%20%20%20%20%20%20assign%2C%0A%20%20%20%20%20%20%20%20initial%2C%0A%20%20%20%20%20%20%20%20np%2C%0A%20%20%20%20%20%20%20%20plt%2C%0A%20%20%20%20%20%20%20%20solvers%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20fit%20runs%20the%20model%20many%20times%2C%20changing%20the%20parameters%20slightly%20to%20find%20in%20which%20direction%20to%20move%20them.%20For%20that%20to%20work%2C%20the%20simulation%20error%20must%20be%20much%20smaller%20than%20those%20changes%2C%20so%20we%20create%20the%20%60Simulator%60%20with%20a%20solver%20with%20tight%20tolerances%20(see%20the%20%60ToleranceWarning%60%20section%20below)%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Oscillator%2C%20Simulator%2C%20solvers)%3A%0A%20%20%20%20sim%20%3D%20Simulator(Oscillator).with_solver(solvers.LSODA(rtol%3D1e-10%2C%20atol%3D1e-12))%0A%20%20%20%20return%20(sim%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20As%20data%20we%20will%20use%20the%20known%20solution%20for%20the%20harmonic%20oscillator%2C%20%24x(t)%20%3D%20%5Ccos(%5Comega%20t)%24%20with%20%24%5Comega%20%3D%202%24%2C%20plus%20some%20noise%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20plt)%3A%0A%20%20%20%20t%20%3D%20np.linspace(0%2C%2010%2C%2050)%20%20%23%20Measurement%20times%0A%20%20%20%20omega_real%20%3D%202%20%20%23%20%22Actual%22%20omega%0A%20%20%20%20rng%20%3D%20np.random.default_rng(0)%0A%20%20%20%20x_data%20%3D%20np.cos(omega_real%20*%20t)%20%2B%20rng.normal(0%2C%200.05%2C%20t.size)%0A%0A%20%20%20%20plt.plot(t%2C%20x_data%2C%20%22.%22%2C%20label%3D%22x%22)%0A%20%20%20%20plt.xlabel(%22time%22)%0A%20%20%20%20plt.legend()%0A%20%20%20%20return%20omega_real%2C%20rng%2C%20t%2C%20x_data%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20target%20has%20to%20be%20an%20xarray%20%60Dataset%60%20in%20the%20same%20format%20as%20the%20simulation%20results.%20The%20%60make_target%60%20function%20builds%20it%20from%20a%20dictionary%20%60%7Bvariable%3A%20values%7D%60%20and%20the%20times%20at%20which%20the%20values%20were%20measured%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Oscillator%2C%20t%2C%20x_data)%3A%0A%20%20%20%20from%20poincare.analysis%20import%20fit_result%2C%20make_target%0A%0A%20%20%20%20target%20%3D%20make_target(%7BOscillator.x%3A%20x_data%7D%2C%20save_at%3Dt)%0A%20%20%20%20target%0A%20%20%20%20return%20fit_result%2C%20make_target%2C%20target%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20We%20pass%20the%20simulator%20and%20the%20target%20to%20%60fit_result%60%2C%20together%20with%20a%20dictionary%20%60p0%60%20telling%20it%20which%20parameters%20to%20fit.%20For%20each%20parameter%20we%20can%20give%3A%0A%0A%20%20%20%20-%20an%20initial%20guess%2C%0A%20%20%20%20-%20%60None%60%2C%20to%20start%20from%20the%20model's%20default%20value%2C%0A%20%20%20%20-%20a%20tuple%20%60(initial%20guess%2C%20lower%20bound%2C%20upper%20bound)%60%20to%20add%20bounds%20(the%20guess%20can%20be%20%60None%60).%0A%0A%20%20%20%20The%20model%20is%20simulated%20at%20the%20times%20of%20the%20target%2C%20so%20there%20is%20no%20need%20to%20set%20them%20on%20the%20simulator.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Oscillator%2C%20fit_result%2C%20omega_real%2C%20sim%2C%20target)%3A%0A%20%20%20%20fit%20%3D%20fit_result(sim%2C%20target%2C%20p0%3D%7BOscillator.omega%3A%20omega_real%20*%201.1%7D)%0A%20%20%20%20fit%0A%20%20%20%20return%20(fit%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20result%20is%20a%20dictionary%20%60%7Bparameter%3A%20best%20value%7D%60%2C%20which%20can%20be%20passed%20directly%20to%20%60with_values%60%20to%20simulate%20the%20fitted%20model%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(fit%2C%20np%2C%20plt%2C%20sim%2C%20t%2C%20x_data)%3A%0A%20%20%20%20fitted%20%3D%20sim.with_values(fit).solve(save_at%3Dnp.linspace(0%2C%2010%2C%20500))%0A%20%20%20%20plt.plot(t%2C%20x_data%2C%20%22.%22%2C%20label%3D%22data%22)%0A%20%20%20%20plt.plot(fitted%5B%22time%22%5D%2C%20fitted%5B%22x%22%5D%2C%20label%3D%22fit%22)%0A%20%20%20%20plt.xlabel(%22time%22)%0A%20%20%20%20plt.legend()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%23%20Initial%20conditions%2C%20transforms%20and%20scales%0A%20%20%20%20Initial%20conditions%20are%20fitted%20like%20parameters%2C%20by%20adding%20the%20variable%20to%20%60p0%60.%0A%0A%20%20%20%20We%20can%20also%20fit%20arbitrary%20functions%20of%20the%20variables%20and%20parameters%20by%20adding%20a%20%60transform%60%20to%20the%20%60Simulator%60%20and%20including%20it%20in%20the%20target.%20For%20our%20example%20we%20add%20the%20energy%20%24E%20%3D%20%5Cfrac%7B1%7D%7B2%7D%20v_x%5E2%20%2B%20%5Cfrac%7B1%7D%7B2%7D%20%5Comega%5E2%20x%5E2%24%2C%20which%20is%20expected%20to%20stay%20constant%20at%20%24E%20%3D%20%5Cfrac%7B1%7D%7B2%7D%20%5Comega%5E2%20%3D%202%24%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Oscillator%2C%20make_target%2C%20np%2C%20sim%2C%20t%2C%20x_data)%3A%0A%20%20%20%20sim_E%20%3D%20sim.with_transform(%0A%20%20%20%20%20%20%20%20%7B%22E%22%3A%20Oscillator.vx**2%20%2F%202%20%2B%20Oscillator.omega**2%20*%20Oscillator.x**2%20%2F%202%7D%2C%0A%20%20%20%20%20%20%20%20append%3DTrue%2C%0A%20%20%20%20)%0A%20%20%20%20target_E%20%3D%20make_target(%7BOscillator.x%3A%20x_data%2C%20%22E%22%3A%20np.full_like(t%2C%202.0)%7D%2C%20save_at%3Dt)%0A%20%20%20%20return%20sim_E%2C%20target_E%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20optional%20%60scale%60%20dictionary%20changes%20how%20much%20each%20variable%20weighs%20in%20the%20fit.%20Here%20%60scale%3D%7B%22E%22%3A%200.5%7D%60%20weighs%20the%20error%20in%20the%20energy%20half%20as%20much%20as%20the%20error%20in%20%60x%60.%20We%20also%20fit%20the%20initial%20value%20of%20%60x%60%2C%20bounded%20between%200%20and%2010%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Oscillator%2C%20fit_result%2C%20omega_real%2C%20sim_E%2C%20target_E)%3A%0A%20%20%20%20fit_E%20%3D%20fit_result(%0A%20%20%20%20%20%20%20%20sim_E%2C%0A%20%20%20%20%20%20%20%20target_E%2C%0A%20%20%20%20%20%20%20%20p0%3D%7BOscillator.omega%3A%20omega_real%20*%201.1%2C%20Oscillator.x%3A%20(None%2C%200%2C%2010)%7D%2C%0A%20%20%20%20%20%20%20%20scale%3D%7B%22E%22%3A%200.5%7D%2C%0A%20%20%20%20)%0A%20%20%20%20fit_E%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Uncertainties%0A%20%20%20%20Besides%20the%20best%20values%2C%20the%20result%20has%20the%20covariance%20matrix%20of%20the%20fitted%20parameters%20in%20%60.pcov%60.%20It%20is%20indexed%20by%20a%20pair%20of%20parameters%20(or%20their%20names)%2C%20and%20its%20diagonal%20gives%20the%20variance%20of%20each%20parameter%2C%20so%20the%20standard%20error%20is%20its%20square%20root%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Oscillator%2C%20fit%2C%20np)%3A%0A%20%20%20%20print(fit.pcov)%0A%20%20%20%20print(%22omega%20%3D%22%2C%20fit%5BOscillator.omega%5D%2C%20%22%2B%2F-%22%2C%20np.sqrt(fit.pcov%5BOscillator.omega%2C%20Oscillator.omega%5D))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%60np.asarray(fit.pcov)%60%20gives%20the%20plain%20matrix%2C%20ordered%20as%20%60fit.pcov.parameters%60.%20If%20the%20parameters%20have%20units%2C%20%60fit.pcov%5Bp%2C%20q%5D%60%20is%20in%20units%20of%20%60p%20*%20q%60.%0A%0A%20%20%20%20In%20the%20fit%20above%20we%20gave%20no%20experimental%20errors%2C%20so%20all%20points%20are%20assumed%20to%20have%20the%20same%20error%2C%20which%20is%20estimated%20from%20how%20far%20the%20data%20is%20from%20the%20fit.%20If%20we%20know%20the%20errors%20of%20our%20measurements%2C%20we%20can%20pass%20them%20with%20%60sigma%3D%60%2C%20in%20the%20same%20form%20as%20the%20target.%20The%20residuals%20are%20then%20divided%20by%20%60sigma%60%2C%20and%20the%20covariance%20uses%20those%20errors%20as%20they%20are%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Oscillator%2C%20fit_result%2C%20make_target%2C%20np%2C%20omega_real%2C%20sim%2C%20t%2C%20target)%3A%0A%20%20%20%20sigma%20%3D%20make_target(%7BOscillator.x%3A%20np.full_like(t%2C%200.05)%7D%2C%20save_at%3Dt)%0A%0A%20%20%20%20fit_sigma%20%3D%20fit_result(%0A%20%20%20%20%20%20%20%20sim%2C%20target%2C%20p0%3D%7BOscillator.omega%3A%20omega_real%20*%201.1%7D%2C%20sigma%3Dsigma%0A%20%20%20%20)%0A%20%20%20%20print(fit_sigma)%0A%20%20%20%20print(%22omega%20error%3A%22%2C%20np.sqrt(fit_sigma.pcov%5B%22omega%22%2C%20%22omega%22%5D))%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Fitting%20steady%20states%0A%20%20%20%20Sometimes%20the%20data%20are%20not%20time%20courses%20but%20steady%20states%20measured%20for%20different%20values%20of%20a%20parameter%20(or%20initial%20condition).%20%60fit_steady_state%60%20compares%20the%20data%20with%20a%20%60SteadyState.sweep%60%20over%20that%20parameter%20(see%20%5BAsymptotic%20behaviour%5D(https%3A%2F%2Fdyscolab.github.io%2Fstatic_notebooks%2Fasymptotic_behaviour.html)).%0A%0A%20%20%20%20As%20an%20example%2C%20take%20a%20mass%20%60m%60%20hanging%20from%20a%20spring%20of%20stiffness%20%60k%60%2C%20with%20a%20damping%20%60c%60.%20Measuring%20the%20position%20%60x%60%20downwards%20from%20the%20rest%20length%20of%20the%20spring%2C%20the%20equation%20of%20motion%20is%20%24m%20%5Cddot%7Bx%7D%20%3D%20m%20g%20-%20k%20x%20-%20c%20%5Cdot%7Bx%7D%24%2C%20and%20the%20mass%20comes%20to%20rest%20at%20%24x%20%3D%20m%20g%20%2F%20k%24%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20Derivative%2C%0A%20%20%20%20Parameter%2C%0A%20%20%20%20Simulator%2C%0A%20%20%20%20System%2C%0A%20%20%20%20Variable%2C%0A%20%20%20%20assign%2C%0A%20%20%20%20initial%2C%0A%20%20%20%20solvers%2C%0A)%3A%0A%20%20%20%20class%20HangingSpring(System)%3A%0A%20%20%20%20%20%20%20%20x%3A%20Variable%20%3D%20initial(default%3D0)%0A%20%20%20%20%20%20%20%20v%3A%20Derivative%20%3D%20x.derive(initial%3D0)%0A%0A%20%20%20%20%20%20%20%20m%3A%20Parameter%20%3D%20assign(default%3D1)%0A%20%20%20%20%20%20%20%20k%3A%20Parameter%20%3D%20assign(default%3D1)%0A%20%20%20%20%20%20%20%20c%3A%20Parameter%20%3D%20assign(default%3D1)%0A%20%20%20%20%20%20%20%20g%3A%20Parameter%20%3D%20assign(default%3D9.81)%0A%0A%20%20%20%20%20%20%20%20eq%20%3D%20v.derive()%20%3C%3C%20g%20-%20(k%20*%20x%20%2B%20c%20*%20v)%20%2F%20m%0A%0A%20%20%20%20sim_spring%20%3D%20Simulator(HangingSpring).with_solver(%0A%20%20%20%20%20%20%20%20solvers.LSODA(rtol%3D1e-10%2C%20atol%3D1e-12)%0A%20%20%20%20)%0A%20%20%20%20return%20HangingSpring%2C%20sim_spring%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20We%20hang%20several%20masses%20and%20measure%20where%20each%20one%20comes%20to%20rest.%20The%20target%20is%20a%20%60Dataset%60%20whose%20coordinate%20is%20the%20swept%20parameter%2C%20in%20the%20same%20format%20as%20the%20output%20of%20%60SteadyState.sweep%60%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(np%2C%20rng)%3A%0A%20%20%20%20import%20xarray%20as%20xr%0A%0A%20%20%20%20masses%20%3D%20np.array(%5B0.5%2C%201.0%2C%202.0%2C%204.0%5D)%0A%20%20%20%20k_real%20%3D%204.0%20%20%23%20%22Actual%22%20stiffness%0A%20%20%20%20x_rest%20%3D%20masses%20*%209.81%20%2F%20k_real%20%2B%20rng.normal(0%2C%200.02%2C%20masses.size)%0A%0A%20%20%20%20target_steady%20%3D%20xr.Dataset(%0A%20%20%20%20%20%20%20%20%7B%22x%22%3A%20xr.DataArray(x_rest%2C%20dims%3D%22m%22%2C%20coords%3D%7B%22m%22%3A%20masses%7D)%7D%0A%20%20%20%20)%0A%20%20%20%20target_steady%0A%20%20%20%20return%20k_real%2C%20masses%2C%20target_steady%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%60fit_steady_state%60%20takes%20the%20simulator%2C%20a%20%60SteadyState%60%20instance%2C%20the%20parameter%20that%20was%20varied%20and%20the%20target%2C%20plus%20%60p0%60%2C%20%60scale%60%20and%20%60sigma%60%20as%20in%20%60fit_result%60%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(HangingSpring%2C%20sim_spring%2C%20target_steady)%3A%0A%20%20%20%20from%20poincare%20import%20SteadyState%0A%20%20%20%20from%20poincare.analysis%20import%20fit_steady_state%0A%0A%20%20%20%20fit_steady%20%3D%20fit_steady_state(%0A%20%20%20%20%20%20%20%20sim_spring%2C%0A%20%20%20%20%20%20%20%20SteadyState()%2C%0A%20%20%20%20%20%20%20%20HangingSpring.m%2C%0A%20%20%20%20%20%20%20%20target_steady%2C%0A%20%20%20%20%20%20%20%20p0%3D%7BHangingSpring.k%3A%20(1.0%2C%201e-3%2C%20100)%7D%2C%0A%20%20%20%20)%0A%20%20%20%20fit_steady%0A%20%20%20%20return%20(SteadyState%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Fitting%20several%20experiments%20at%20once%0A%20%20%20%20Often%20parameters%20are%20measured%20in%20several%20experiments%2C%20for%20example%20with%20different%20initial%20conditions%2C%20or%20some%20as%20time%20courses%20and%20others%20as%20steady%20states.%20The%20%60Fitter%60%20class%20fits%20all%20of%20them%20at%20once%2C%20with%20the%20same%20values%20for%20the%20fitted%20parameters.%20It%20takes%3A%0A%0A%20%20%20%20-%20the%20%60Simulator%60%2C%0A%20%20%20%20-%20a%20list%20of%20simulation%20functions%2C%20one%20per%20experiment.%20Each%20takes%20the%20simulator%20(with%20the%20parameters%20being%20tried)%20and%20returns%20a%20result%20comparable%20to%20its%20target%2C%20so%20it%20is%20where%20each%20experiment's%20times%20and%20conditions%20are%20set%2C%0A%20%20%20%20-%20a%20list%20of%20targets%2C%0A%20%20%20%20-%20%60p0%60%2C%20as%20in%20%60fit_result%60%2C%20and%20optionally%20%60scales%60%20and%20%60sigma%60%20(one%20per%20target).%0A%0A%20%20%20%20The%20resting%20positions%20above%20tell%20us%20the%20stiffness%20%60k%60%2C%20but%20nothing%20about%20the%20damping%20%60c%60.%20To%20measure%20it%2C%20we%20release%20a%20mass%20of%201%20from%20the%20rest%20length%20of%20the%20spring%20and%20record%20how%20it%20rings%20down%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(HangingSpring%2C%20k_real%2C%20make_target%2C%20np%2C%20plt%2C%20rng%2C%20sim_spring)%3A%0A%20%20%20%20c_real%20%3D%200.8%20%20%23%20%22Actual%22%20damping%0A%20%20%20%20t_ring%20%3D%20np.linspace(0%2C%2015%2C%2060)%0A%20%20%20%20_true%20%3D%20sim_spring.with_values(%0A%20%20%20%20%20%20%20%20%7BHangingSpring.k%3A%20k_real%2C%20HangingSpring.c%3A%20c_real%7D%0A%20%20%20%20).solve(save_at%3Dt_ring)%0A%20%20%20%20x_ring%20%3D%20_true%5B%22x%22%5D.to_numpy()%20%2B%20rng.normal(0%2C%200.02%2C%20t_ring.size)%0A%20%20%20%20target_ring%20%3D%20make_target(%7BHangingSpring.x%3A%20x_ring%7D%2C%20save_at%3Dt_ring)%0A%0A%20%20%20%20plt.plot(t_ring%2C%20x_ring%2C%20%22.%22%2C%20label%3D%22x%22)%0A%20%20%20%20plt.xlabel(%22time%22)%0A%20%20%20%20plt.legend()%0A%20%20%20%20return%20t_ring%2C%20target_ring%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20We%20fit%20%60k%60%20and%20%60c%60%20to%20both%20experiments%3A%20the%20ring-down%20is%20compared%20with%20a%20simulation%20at%20the%20measured%20times%2C%20and%20the%20resting%20positions%20with%20a%20steady%20state%20sweep%20over%20the%20masses%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20HangingSpring%2C%0A%20%20%20%20SteadyState%2C%0A%20%20%20%20masses%2C%0A%20%20%20%20sim_spring%2C%0A%20%20%20%20t_ring%2C%0A%20%20%20%20target_ring%2C%0A%20%20%20%20target_steady%2C%0A)%3A%0A%20%20%20%20from%20poincare.analysis%20import%20Fitter%0A%0A%20%20%20%20fitter%20%3D%20Fitter(%0A%20%20%20%20%20%20%20%20sim_spring%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20lambda%20sim%3A%20sim.solve(save_at%3Dt_ring)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20lambda%20sim%3A%20SteadyState().sweep(sim%2C%20variable%3DHangingSpring.m%2C%20values%3Dmasses)%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20%5Btarget_ring%2C%20target_steady%5D%2C%0A%20%20%20%20%20%20%20%20p0%3D%7BHangingSpring.k%3A%20(1.0%2C%201e-3%2C%20100)%2C%20HangingSpring.c%3A%20(None%2C%200%2C%2010)%7D%2C%0A%20%20%20%20)%0A%20%20%20%20fit_both%20%3D%20fitter.fit()%0A%20%20%20%20fit_both%0A%20%20%20%20return%20(fitter%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%60Fitter.fit()%60%20returns%20the%20same%20kind%20of%20result%20as%20%60fit_result%60%2C%20with%20the%20covariance%20in%20%60.pcov%60.%20It%20can%20be%20called%20again%20with%20a%20different%20%60p0%60%20to%20try%20other%20initial%20guesses%20or%20bounds%20for%20the%20same%20parameters%2C%20and%20extra%20keyword%20arguments%20are%20passed%20to%20%60scipy.optimize.least_squares%60%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(HangingSpring%2C%20fitter)%3A%0A%20%20%20%20fitter.fit(p0%3D%7BHangingSpring.k%3A%20(3.0%2C%201e-3%2C%20100)%2C%20HangingSpring.c%3A%20(0.5%2C%200%2C%2010)%7D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Warnings%0A%20%20%20%20Fits%20can%20warn%20when%20their%20results%20may%20not%20be%20reliable.%20Both%20warnings%20are%20in%20%60poincare.analysis.fits%60.%0A%0A%20%20%20%20%23%23%23%20ToleranceWarning%0A%20%20%20%20%60least_squares%60%20estimates%20how%20the%20residuals%20change%20with%20each%20parameter%20by%20changing%20it%20by%20a%20very%20small%20relative%20step%20(about%20%2410%5E%7B-8%7D%24).%20If%20the%20solver%20error%20is%20larger%20than%20the%20effect%20of%20that%20step%2C%20the%20fit%20can%20stop%20early%20at%20wrong%20values.%20A%20%60ToleranceWarning%60%20means%20the%20solver%20tolerances%20are%20too%20loose%20for%20this.%20With%20the%20default%20solver%20tolerances%20we%20get%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Oscillator%2C%20Simulator%2C%20fit_result%2C%20target)%3A%0A%20%20%20%20import%20warnings%0A%0A%20%20%20%20from%20poincare.analysis.fits%20import%20CovarianceWarning%2C%20ToleranceWarning%0A%0A%20%20%20%20with%20warnings.catch_warnings(record%3DTrue)%20as%20caught%3A%0A%20%20%20%20%20%20%20%20warnings.simplefilter(%22always%22)%0A%20%20%20%20%20%20%20%20fit_result(Simulator(Oscillator)%2C%20target%2C%20p0%3D%7BOscillator.omega%3A%202.2%7D)%0A%0A%20%20%20%20print(*%5Bw.message%20for%20w%20in%20caught%20if%20w.category%20is%20ToleranceWarning%5D%2C%20sep%3D%22%5Cn%22)%0A%20%20%20%20return%20CovarianceWarning%2C%20warnings%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20What%20to%20try%3A%0A%0A%20%20%20%20-%20tighten%20the%20tolerances%20with%20%60sim.with_solver(...)%60%2C%20for%20example%20%60solvers.LSODA(rtol%3D1e-10%2C%20atol%3D1e-12)%60%2C%20as%20we%20did%20above%2C%0A%20%20%20%20-%20or%2C%20if%20tighter%20tolerances%20make%20the%20simulations%20too%20slow%2C%20pass%20a%20larger%20step%20to%20the%20fit%2C%20e.g.%20%60fit_result(...%2C%20diff_step%3D1e-4)%60.%0A%0A%20%20%20%20%23%23%23%20CovarianceWarning%0A%20%20%20%20A%20%60CovarianceWarning%60%20means%20the%20fitted%20values%20may%20be%20fine%2C%20but%20their%20covariance%20(and%20so%20their%20errors)%20is%20not%20reliable.%20It%20appears%20when%3A%0A%0A%20%20%20%20-%20a%20parameter%20ended%20at%20one%20of%20its%20bounds%3A%20widen%20the%20bounds%2C%20or%20check%20if%20the%20model%20or%20initial%20guess%20is%20right%2C%0A%20%20%20%20-%20there%20are%20not%20enough%20data%20points%20to%20estimate%20the%20noise%20when%20no%20%60sigma%60%20is%20given%3A%20add%20data%20or%20pass%20%60sigma%60%2C%0A%20%20%20%20-%20some%20parameters%20are%20not%20determined%20separately%20by%20the%20data.%20For%20example%2C%20a%20capacitor%20%60C%60%20discharging%20through%20a%20resistor%20%60R%60%20depends%20only%20on%20the%20product%20%60R%20*%20C%60%2C%20so%20any%20pair%20with%20the%20same%20product%20fits%20equally%20well%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20CovarianceWarning%2C%0A%20%20%20%20Parameter%2C%0A%20%20%20%20Simulator%2C%0A%20%20%20%20System%2C%0A%20%20%20%20Variable%2C%0A%20%20%20%20assign%2C%0A%20%20%20%20fit_result%2C%0A%20%20%20%20initial%2C%0A%20%20%20%20make_target%2C%0A%20%20%20%20np%2C%0A%20%20%20%20solvers%2C%0A%20%20%20%20warnings%2C%0A)%3A%0A%20%20%20%20class%20RCCircuit(System)%3A%0A%20%20%20%20%20%20%20%20V%3A%20Variable%20%3D%20initial(default%3D5.0)%0A%20%20%20%20%20%20%20%20R%3A%20Parameter%20%3D%20assign(default%3D1.0)%0A%20%20%20%20%20%20%20%20C%3A%20Parameter%20%3D%20assign(default%3D0.5)%0A%0A%20%20%20%20%20%20%20%20eq%20%3D%20V.derive()%20%3C%3C%20-V%20%2F%20(R%20*%20C)%0A%0A%20%20%20%20_t%20%3D%20np.linspace(0%2C%205%2C%2020)%0A%20%20%20%20_target%20%3D%20make_target(%7BRCCircuit.V%3A%205%20*%20np.exp(-_t%20%2F%200.5)%7D%2C%20save_at%3D_t)%0A%0A%20%20%20%20with%20warnings.catch_warnings(record%3DTrue)%20as%20caught_cov%3A%0A%20%20%20%20%20%20%20%20warnings.simplefilter(%22always%22)%0A%20%20%20%20%20%20%20%20fit_rc%20%3D%20fit_result(%0A%20%20%20%20%20%20%20%20%20%20%20%20Simulator(RCCircuit).with_solver(solvers.LSODA(rtol%3D1e-10%2C%20atol%3D1e-12))%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20_target%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20p0%3D%7BRCCircuit.R%3A%201.2%2C%20RCCircuit.C%3A%200.4%7D%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%20%20%20%20print(fit_rc)%0A%20%20%20%20print(*%5Bw.message%20for%20w%20in%20caught_cov%20if%20w.category%20is%20CovarianceWarning%5D%2C%20sep%3D%22%5Cn%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20product%20%60R%20*%20C%60%20is%20well%20determined%2C%20but%20%60R%60%20and%20%60C%60%20separately%20are%20not.%20In%20this%20case%2C%20fix%20one%20parameter%20of%20each%20group%20(leave%20it%20out%20of%20%60p0%60)%2C%20or%20add%20data%20that%20distinguishes%20them%2C%20for%20example%20another%20experiment%20fitted%20together%20with%20%60Fitter%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
a4c44c7dab5aa2e3435e2952b7275a98