ZHAWNotes/Notes/Semester 2/AN2 - Analysis 2/Analysis 2.ipynb

240 lines
28 KiB
Plaintext
Raw Normal View History

2022-05-30 18:54:42 +00:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"import sympy\n",
"from sympy import Symbol, Function, cos, diff\n",
"from sympy.codegen.ast import Assignment, Print\n",
"from sympy.integrals.integrals import integrate, Integral\n",
"from sympy.plotting import plot\n",
"from sympy.plotting.pygletplot import PygletPlot as Plot\n",
"from IPython.display import Markdown"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle f{\\left(x \\right)} = x^{2}$"
],
"text/plain": [
"Eq(f(x), x**2)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$\\displaystyle \\operatorname{f'}{\\left(x \\right)} = 2 x$"
],
"text/plain": [
"Eq(f'(x), 2*x)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$\\displaystyle \\operatorname{f''}{\\left(x \\right)} = 2$"
],
"text/plain": [
"Eq(f''(x), 2)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"x = Symbol('x')\n",
"\n",
"def f(x):\n",
" return x**2\n",
"\n",
"display(sympy.Eq(Function('f')(x), f(x)))\n",
"display(sympy.Eq(Function('f\\'')(Symbol('x')), diff(f(x))))\n",
"display(sympy.Eq(Function('f\\'\\'')(Symbol('x')), diff(diff(f(x)))))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\int \\left(x^{2} + x + 1\\right)\\, dx$"
],
"text/plain": [
"Integral(x**2 + x + 1, x)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{x^{3}}{3} + \\frac{x^{2}}{2} + x$"
],
"text/plain": [
"x**3/3 + x**2/2 + x"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"x = Symbol('x')\n",
"i = Integral(x**2 + x + 1, x)\n",
"sympy.solvers.solve(i, x)\n",
"display(i)\n",
"display(i.doit())"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\int \\cos{\\left(x \\right)}\\, dx$"
],
"text/plain": [
"Integral(cos(x), x)"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/latex": [
"$\\displaystyle \\sin{\\left(x \\right)}$"
],
"text/plain": [
"sin(x)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"i = Integral(cos(x), x)\n",
"display(i)\n",
"display(i.doit())"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\left\\{-1, 1\\right\\}$"
],
"text/plain": [
"{-1, 1}"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = Symbol('x')\n",
"y = Symbol('y')\n",
"sympy.FiniteSet(*sympy.solvers.solve(sympy.Eq(x**2, 1), x))"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"![](plot.svg)"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAV0AAADnCAYAAAC9roUQAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAABF+ElEQVR4nO3deVxU1f/H8dcwrAqybwKKCyqrC+CS+5qaZS5l5lZamktqpWWZZaVmfVs0rVzStL5u1U/lm4p90zR3cUMEERdAZZVF9n3m/v4g+aosijJzZ4bzfDx8OMydufczA7y5c8655ygk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RRBDqi10RSbz1y3k6N7Nn9bhAzIx1fz6F2jC40AWY3c+L4jI1q/6+hqlSyYIh3iJ4BUEP/BGVwqyt4QQ2tWXdS0GYmxhW4IKBhq5CoeCdga0pKVOz/mgcJkoF8wyg11MQDNkfUSnM2HwWPzdr1r8UTANTg4wnwwxdKA/eBUO8KVOrWX0oFpVaYv5T4oxXEHRR6IVkXt9yDj83a36a1BErcxO5S9IYgw1dKA/ej57xxUih4IcjcZSpJT582kcEryDokN0Ryczceo627tZsnGjYgQsGHrpQHrwfPu2DiVLB2sNxlKnVfPyMnxjVIAg64PfzSczeFk57Dxs2TOyIpZnBR5Lhhy6UB+97g70xVhrx/cFrlKkklgzzF8ErCDIKCU/kjW3hBHna8eNLwTSsB4EL9SR0oTx4336yNSZGCr756yplaonPRgQYzIBrQdAnW8Nu8O6OC3RqZmfQnWZVqT+vlPLgfXNAa5RGRny97zKFJSq+GtXW4MYBCoIuu7PCd89WjqwaG6gX65rVpXo
"text/plain": [
"<Figure size 432x288 with 1 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"from sympy import symbols\n",
"from sympy.plotting import plot\n",
"x = symbols('x')\n",
"p1 = plot(x**2, show=False, label='$f(x)=x^2$', legend=True)\n",
"p2 = plot(x, label='$g(x)=x$', show=False)\n",
"p2.extend(plot(-x**2, label='$h(x)=-x$', show=False))\n",
"p1.extend(p2)\n",
"p1.save('plot.svg')\n",
"Markdown('![](plot.svg)')"
]
}
],
"metadata": {
"interpreter": {
"hash": "96fc3968b0459c7bb0304f66d2259bbe1d74f60096901795baa92b56e1ba9010"
},
"kernelspec": {
"display_name": "Python 3.10.2 ('AN2_Analysis_2-DUdzbZjr')",
"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.10.2"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}