{ "cells": [ { "cell_type": "markdown", "id": "20e2498e", "metadata": {}, "source": [ "# Mackey-Glass equation\n", "\n", "[![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/brainpy/examples/blob/main/classical_dynamical_systems/mackey_glass_eq.ipynb)\n", "[![Open in Kaggle](https://kaggle.com/static/images/open-in-kaggle.svg)](https://kaggle.com/kernels/welcome?src=https://github.com/brainpy/examples/blob/main/classical_dynamical_systems/mackey_glass_eq.ipynb)" ] }, { "cell_type": "markdown", "id": "0768cbc0", "metadata": {}, "source": [ "The Mackey-Glass equation is the nonlinear time delay differential equation\n", "\n", "$$\n", "\\frac{dx}{dt} = \\beta \\frac{ x_{\\tau} }{1+{x_{\\tau}}^n}-\\gamma x, \\quad \\gamma,\\beta,n > 0,\n", "$$\n", "\n", "where $\\beta, \\gamma, \\tau, n$ are real numbers, and $x_{\\tau}$ represents the value of the variable $x$ at time $(t−\\tau)$. Depending on the values of the parameters, this equation displays a range of periodic and chaotic dynamics. " ] }, { "cell_type": "code", "execution_count": 1, "id": "887f7e85", "metadata": { "ExecuteTime": { "end_time": "2023-07-22T05:45:16.407016Z", "start_time": "2023-07-22T05:45:15.482524200Z" } }, "outputs": [], "source": [ "import brainpy as bp\n", "import brainpy.math as bm\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": 7, "id": "6e0de11d8851212d", "metadata": { "ExecuteTime": { "end_time": "2023-07-22T05:45:28.740850800Z", "start_time": "2023-07-22T05:45:28.723839100Z" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'2.4.3'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bp.__version__" ] }, { "cell_type": "code", "execution_count": 2, "id": "363863c4", "metadata": { "ExecuteTime": { "end_time": "2023-07-22T05:45:16.422990900Z", "start_time": "2023-07-22T05:45:16.407016Z" } }, "outputs": [], "source": [ "bm.set_dt(0.05)" ] }, { "cell_type": "code", "execution_count": 3, "id": "0ea282b9", "metadata": { "ExecuteTime": { "end_time": "2023-07-22T05:45:16.438658Z", "start_time": "2023-07-22T05:45:16.422990900Z" } }, "outputs": [], "source": [ "class MackeyGlassEq(bp.Dynamic):\n", " def __init__(self, num, beta=2., gamma=1., tau=2., n=9.65):\n", " super(MackeyGlassEq, self).__init__(num)\n", "\n", " # parameters\n", " self.beta = beta\n", " self.gamma = gamma\n", " self.tau = tau\n", " self.n = n\n", " self.delay_len = int(tau/bm.get_dt())\n", "\n", " # variables\n", " self.x = bm.Variable(bm.zeros(num))\n", " self.x_delay = bm.LengthDelay(\n", " self.x,\n", " delay_len=self.delay_len,\n", " initial_delay_data=lambda sh, dtype: 1.2+0.2*(bm.random.random(sh)-0.5)\n", " )\n", " self.x_oldest = bm.Variable(self.x_delay(self.delay_len))\n", "\n", " # functions\n", " self.integral = bp.odeint(lambda x, t, x_tau: self.beta * x_tau / (1 + x_tau ** self.n) - self.gamma * x,\n", " method='exp_auto')\n", "\n", " def update(self):\n", " self.x.value = self.integral(self.x.value, bp.share['t'], self.x_oldest.value, bp.share['dt'])\n", " self.x_delay.update(self.x.value)\n", " self.x_oldest.value = self.x_delay(self.delay_len)" ] }, { "cell_type": "code", "execution_count": 4, "id": "07cccad0", "metadata": { "ExecuteTime": { "end_time": "2023-07-22T05:45:16.799632900Z", "start_time": "2023-07-22T05:45:16.438658Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "No GPU/TPU found, falling back to CPU. (Set TF_CPP_MIN_LOG_LEVEL=0 and rerun for more info.)\n" ] } ], "source": [ "eq = MackeyGlassEq(1, beta=0.2, gamma=0.1, tau=17, n=10)\n", "# eq = MackeyGlassEq(1, )" ] }, { "cell_type": "code", "execution_count": 5, "id": "10a5e8eb", "metadata": { "ExecuteTime": { "end_time": "2023-07-22T05:45:17.073506900Z", "start_time": "2023-07-22T05:45:16.799632900Z" } }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "96058c38735f4cf9b3d287bec160ad7c", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/20000 [00:00" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "plt.plot(runner.mon.x[1000:, 0], runner.mon.x_oldest[1000:, 0])\n", "plt.show()" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "formats": "ipynb,auto:light", "notebook_metadata_filter": "-all" }, "kernelspec": { "display_name": "brainpy", "language": "python", "name": "brainpy" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.12" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 5 }