Skip to content

Instantly share code, notes, and snippets.

@lucianoinso
Created September 30, 2018 11:34
Show Gist options
  • Select an option

  • Save lucianoinso/9141d0b3b9bb04d9a041e89a906727d9 to your computer and use it in GitHub Desktop.

Select an option

Save lucianoinso/9141d0b3b9bb04d9a041e89a906727d9 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Ctrl+enter en la celda de abajo para correr el script"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"\n",
"# Equation\n",
"def f(x, y):\n",
" return (3*x*x*y)\n",
"\n",
"def t4(x, y, h):\n",
" k1 = f(x, y)\n",
" k2 = f(x + (h / 2.0), y + k1*(h/2.0))\n",
" k3 = f(x + (h / 2.0), y + k2*(h/2.0))\n",
" k4 = f(x + h, y + (k3*h))\n",
" return ((1/6.0)*(k1 + 2*k2 + 2*k3 + k4))\n",
"\n",
"def rungeKutta(x0, y0, h, iter):\n",
" result = [(x0, y0)]\n",
"\n",
" x = x0\n",
" y = y0\n",
"\n",
" for i in range(1, iter):\n",
" yn = y + (h * t4(x, y, h))\n",
" x = x + h\n",
" y = yn\n",
" result.append((x, y))\n",
" return result\n",
"\n",
"if __name__ == '__main__':\n",
" x0 = 0\n",
" y0 = 1\n",
" h = 0.1\n",
" iters = 100\n",
" result = rungeKutta(x0, y0, h, iters)\n",
" # print(result)\n",
"\n",
" axes = plt.gca()\n",
" # X axis range\n",
" axes.set_xlim([0, 8])\n",
" # Y axis range\n",
" axes.set_ylim([0, 7])\n",
"\n",
" plt.grid()\n",
" plt.plot([i[0] for i in result], [j[1] for j in result])\n",
" plt.show()"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment