.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/11_applications/plot_02_ph_process.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code or to run this example in your browser via Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_11_applications_plot_02_ph_process.py: Operating regions of a pH neutralization process ================================================ The process gain of a pH reactor changes by orders of magnitude along the titration curve, so a single linear model cannot describe it. Segmenting the record by its local *dynamics* (dynamic PCA, Bankó, 2015) and fitting a first-order ARX model to every segment shows how the local gain and time constant depend on the operating point -- the information needed to schedule a gain-scheduled controller. .. GENERATED FROM PYTHON SOURCE LINES 12-22 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import pandas as pd import mvtsa as mv from mvtsa import segmentation as sg P = mv.datasets.simulate_ph_cstr(T=960, random_state=3) .. GENERATED FROM PYTHON SOURCE LINES 23-25 Segmentation by local dynamics ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 25-43 .. code-block:: Python seg = sg.DPCASegmenter(n_segments=8, lags="auto", n_components=0.99).segment(P) rows = [] for a, b in seg.segments: u, y = P[a:b, 0], P[a:b, 1] A = np.column_stack([y[:-1], u[:-1], np.ones(b - a - 1)]) (a1, b1, _), *_ = np.linalg.lstsq(A, y[1:], rcond=None) rows.append( { "start": a, "stop": b, "mean pH": y.mean(), "local gain": b1 / (1 - a1) if a1 < 1 else np.nan, "pole": a1, } ) print(pd.DataFrame(rows).round(3).to_string(index=False)) mv.viz.plot_segmentation(P, seg, names=["base flow", "pH"]) .. image-sg:: /auto_examples/11_applications/images/sphx_glr_plot_02_ph_process_001.png :alt: plot 02 ph process :srcset: /auto_examples/11_applications/images/sphx_glr_plot_02_ph_process_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none start stop mean pH local gain pole 0 120 2.829 1.780 0.691 120 180 10.741 5.392 0.557 180 300 6.658 19.029 0.714 300 480 3.451 4.614 0.900 480 540 10.511 5.405 0.713 540 600 2.930 1.297 0.177 600 840 6.562 55.043 0.858 840 960 11.051 1.407 0.675 .. GENERATED FROM PYTHON SOURCE LINES 44-46 Local gain against the operating point -------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 46-52 .. code-block:: Python df = pd.DataFrame(rows) fig, ax = plt.subplots(figsize=(4.8, 3.2)) ax.semilogy(df["mean pH"], np.abs(df["local gain"]), "o", color="#285780") ax.set_xlabel("mean pH of the segment") ax.set_ylabel("|local static gain|") plt.show() .. image-sg:: /auto_examples/11_applications/images/sphx_glr_plot_02_ph_process_002.png :alt: plot 02 ph process :srcset: /auto_examples/11_applications/images/sphx_glr_plot_02_ph_process_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.095 seconds) .. _sphx_glr_download_auto_examples_11_applications_plot_02_ph_process.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/ComplexSystemsIntelligence/mvtsa/main?urlpath=lab/tree/notebooks/auto_examples/11_applications/plot_02_ph_process.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_ph_process.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_ph_process.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_ph_process.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_