Class_Work/ME_2016/Untitled.ipynb
2024-09-30 14:54:05 -04:00

322 lines
10 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "2ebd353f-ace3-422b-bcd3-f93019dcd2f7",
"metadata": {},
"source": [
"# "
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "c6fdaca9-fd8a-473d-8ae4-d54486617890",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle t^{2} y$"
],
"text/plain": [
"t**2*y"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from sympy import *\n",
"t = symbols('t')\n",
"x = Function('x')(t)\n",
"y = symbols('y')\n",
"x=y*t**2\n",
"x"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c8fa8842-ac96-40c0-95be-48626f4951ea",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle 2 t y$"
],
"text/plain": [
"2*t*y"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"diff(x, t)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "0fe70daa-5646-495d-9937-13e28c59200a",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{t^{2} y^{2}}{2}$"
],
"text/plain": [
"t**2*y**2/2"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"integrate(x,y)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "d7f45a70-e116-4cb4-81cf-95eee393f1a6",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\frac{t^{3} y}{3}$"
],
"text/plain": [
"t**3*y/3"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"integrate(x,t)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "f8d0291f-aca1-46bd-a73a-2b0f2ea1e1cc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"newfun = lambdify([t,y],x)\n",
"newfun(1,3)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "73475974-e48a-42cf-bb44-12c2f120939e",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle x{\\left(t \\right)} = C_{1} + C_{2} t$"
],
"text/plain": [
"Eq(x(t), C1 + C2*t)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = Function('x')(t)\n",
"results = dsolve(Derivative(x,t,t))\n",
"results"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "e05c3b36-9175-47ac-bbd0-5d20cb6a083b",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\epsilon^{3} x_{3}{\\left(t \\right)} + \\epsilon^{2} x_{2}{\\left(t \\right)} + \\epsilon x_{1}{\\left(t \\right)} + x_{0}{\\left(t \\right)}$"
],
"text/plain": [
"epsilon**3*x3(t) + epsilon**2*x2(t) + epsilon*x1(t) + x0(t)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"x0 = Function('x0')(t); x1 = Function('x1')(t); x2 = Function('x2')(t); x3 = Function('x3')(t); x = Function('x')\n",
"t = Symbol('t')\n",
"eps = Symbol('epsilon')\n",
"x = x0 + eps*x1 + eps**2 *x2 + eps**3 *x3\n",
"display(x)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "c89c7a7f-0e1d-4f46-85bf-d0585dc7f0bf",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle \\epsilon^{3} x_{3}{\\left(t \\right)} + \\epsilon^{3} \\frac{d}{d t} x_{3}{\\left(t \\right)} + \\epsilon^{2} x_{2}{\\left(t \\right)} + \\epsilon^{2} \\frac{d}{d t} x_{2}{\\left(t \\right)} - \\epsilon \\left(\\epsilon^{3} x_{3}{\\left(t \\right)} + \\epsilon^{2} x_{2}{\\left(t \\right)} + \\epsilon x_{1}{\\left(t \\right)} + x_{0}{\\left(t \\right)}\\right)^{2} + \\epsilon x_{1}{\\left(t \\right)} + \\epsilon \\frac{d}{d t} x_{1}{\\left(t \\right)} + x_{0}{\\left(t \\right)} + \\frac{d}{d t} x_{0}{\\left(t \\right)}$"
],
"text/plain": [
"epsilon**3*x3(t) + epsilon**3*Derivative(x3(t), t) + epsilon**2*x2(t) + epsilon**2*Derivative(x2(t), t) - epsilon*(epsilon**3*x3(t) + epsilon**2*x2(t) + epsilon*x1(t) + x0(t))**2 + epsilon*x1(t) + epsilon*Derivative(x1(t), t) + x0(t) + Derivative(x0(t), t)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"expr = x.diff(t) + x - eps*x**2\n",
"display(expr)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "1f29a2d0-d861-45ac-8442-7cc4e52a496a",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle - \\epsilon^{7} x_{3}^{2}{\\left(t \\right)} - 2 \\epsilon^{6} x_{2}{\\left(t \\right)} x_{3}{\\left(t \\right)} - 2 \\epsilon^{5} x_{1}{\\left(t \\right)} x_{3}{\\left(t \\right)} - \\epsilon^{5} x_{2}^{2}{\\left(t \\right)} - 2 \\epsilon^{4} x_{0}{\\left(t \\right)} x_{3}{\\left(t \\right)} - 2 \\epsilon^{4} x_{1}{\\left(t \\right)} x_{2}{\\left(t \\right)} - 2 \\epsilon^{3} x_{0}{\\left(t \\right)} x_{2}{\\left(t \\right)} - \\epsilon^{3} x_{1}^{2}{\\left(t \\right)} + \\epsilon^{3} x_{3}{\\left(t \\right)} + \\epsilon^{3} \\frac{d}{d t} x_{3}{\\left(t \\right)} - 2 \\epsilon^{2} x_{0}{\\left(t \\right)} x_{1}{\\left(t \\right)} + \\epsilon^{2} x_{2}{\\left(t \\right)} + \\epsilon^{2} \\frac{d}{d t} x_{2}{\\left(t \\right)} - \\epsilon x_{0}^{2}{\\left(t \\right)} + \\epsilon x_{1}{\\left(t \\right)} + \\epsilon \\frac{d}{d t} x_{1}{\\left(t \\right)} + x_{0}{\\left(t \\right)} + \\frac{d}{d t} x_{0}{\\left(t \\right)}$"
],
"text/plain": [
"-epsilon**7*x3(t)**2 - 2*epsilon**6*x2(t)*x3(t) - 2*epsilon**5*x1(t)*x3(t) - epsilon**5*x2(t)**2 - 2*epsilon**4*x0(t)*x3(t) - 2*epsilon**4*x1(t)*x2(t) - 2*epsilon**3*x0(t)*x2(t) - epsilon**3*x1(t)**2 + epsilon**3*x3(t) + epsilon**3*Derivative(x3(t), t) - 2*epsilon**2*x0(t)*x1(t) + epsilon**2*x2(t) + epsilon**2*Derivative(x2(t), t) - epsilon*x0(t)**2 + epsilon*x1(t) + epsilon*Derivative(x1(t), t) + x0(t) + Derivative(x0(t), t)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"expr = expr.expand()\n",
"display(expr)\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "3c1ef020-baf0-459b-8066-d70e86912203",
"metadata": {},
"outputs": [
{
"data": {
"text/latex": [
"$\\displaystyle - \\epsilon^{7} x_{3}^{2}{\\left(t \\right)} - 2 \\epsilon^{6} x_{2}{\\left(t \\right)} x_{3}{\\left(t \\right)} + \\epsilon^{5} \\left(- 2 x_{1}{\\left(t \\right)} x_{3}{\\left(t \\right)} - x_{2}^{2}{\\left(t \\right)}\\right) + \\epsilon^{4} \\left(- 2 x_{0}{\\left(t \\right)} x_{3}{\\left(t \\right)} - 2 x_{1}{\\left(t \\right)} x_{2}{\\left(t \\right)}\\right) + \\epsilon^{3} \\left(- 2 x_{0}{\\left(t \\right)} x_{2}{\\left(t \\right)} - x_{1}^{2}{\\left(t \\right)} + x_{3}{\\left(t \\right)} + \\frac{d}{d t} x_{3}{\\left(t \\right)}\\right) + \\epsilon^{2} \\left(- 2 x_{0}{\\left(t \\right)} x_{1}{\\left(t \\right)} + x_{2}{\\left(t \\right)} + \\frac{d}{d t} x_{2}{\\left(t \\right)}\\right) + \\epsilon \\left(- x_{0}^{2}{\\left(t \\right)} + x_{1}{\\left(t \\right)} + \\frac{d}{d t} x_{1}{\\left(t \\right)}\\right) + x_{0}{\\left(t \\right)} + \\frac{d}{d t} x_{0}{\\left(t \\right)}$"
],
"text/plain": [
"-epsilon**7*x3(t)**2 - 2*epsilon**6*x2(t)*x3(t) + epsilon**5*(-2*x1(t)*x3(t) - x2(t)**2) + epsilon**4*(-2*x0(t)*x3(t) - 2*x1(t)*x2(t)) + epsilon**3*(-2*x0(t)*x2(t) - x1(t)**2 + x3(t) + Derivative(x3(t), t)) + epsilon**2*(-2*x0(t)*x1(t) + x2(t) + Derivative(x2(t), t)) + epsilon*(-x0(t)**2 + x1(t) + Derivative(x1(t), t)) + x0(t) + Derivative(x0(t), t)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"epsforms = collect(expr,eps)\n",
"display(epsforms)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "d79677a6-b658-4970-aca7-8aaf8dfdd5ad",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[x0(t) + Derivative(x0(t), t),\n",
" -x0(t)**2 + x1(t) + Derivative(x1(t), t),\n",
" -2*x0(t)*x1(t) + x2(t) + Derivative(x2(t), t),\n",
" -2*x0(t)*x2(t) - x1(t)**2 + x3(t) + Derivative(x3(t), t)]"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"ename": "TypeError",
"evalue": "'x0' object is not callable",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[17], line 7\u001b[0m\n\u001b[1;32m 4\u001b[0m EqLHS\u001b[38;5;241m.\u001b[39mappend(collect(epsforms, eps, evaluate\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m)[eps\u001b[38;5;241m*\u001b[39m\u001b[38;5;241m*\u001b[39mk])\n\u001b[1;32m 5\u001b[0m display(EqLHS)\n\u001b[0;32m----> 7\u001b[0m a \u001b[38;5;241m=\u001b[39m dsolve(Eq(EqLHS[\u001b[38;5;241m0\u001b[39m],\u001b[38;5;241m0\u001b[39m),x0, ics \u001b[38;5;241m=\u001b[39m{\u001b[43mx0\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m)\u001b[49m:\u001b[38;5;241m2\u001b[39m})\n",
"\u001b[0;31mTypeError\u001b[0m: 'x0' object is not callable"
]
}
],
"source": [
"EqLHS = [ ] \n",
"Orders = [0,1,2,3]\n",
"for k in Orders:\n",
" EqLHS.append(collect(epsforms, eps, evaluate=False)[eps**k])\n",
"display(EqLHS)\n",
"\n",
"a = dsolve(Eq(EqLHS[0],0),x0, ics ={x0(0):2})"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}