Skip to content

Instantly share code, notes, and snippets.

@vuddameri
Created April 25, 2024 20:19
Show Gist options
  • Select an option

  • Save vuddameri/ca88918e084bea585d38dc96a6d893dc to your computer and use it in GitHub Desktop.

Select an option

Save vuddameri/ca88918e084bea585d38dc96a6d893dc to your computer and use it in GitHub Desktop.
sympy-Assignment4-Pb7.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyOxFJvKj2RupszM1FnzEJuh",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/vuddameri/ca88918e084bea585d38dc96a6d893dc/sympy-assignment4-pb7.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 106
},
"id": "Vhy1zEEAwdn6",
"outputId": "f85dbb3b-f523-4f32-80a0-5309fc9d23c8"
},
"outputs": [
{
"output_type": "error",
"ename": "SyntaxError",
"evalue": "invalid syntax (<ipython-input-1-299510c2fa03>, line 1)",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-1-299510c2fa03>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m <h1>\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": []
},
{
"cell_type": "markdown",
"source": [
"<h1> Problem 7 from Assignment 4"
],
"metadata": {
"id": "ZUYRceS2woja"
}
},
{
"cell_type": "markdown",
"source": [],
"metadata": {
"id": "kJSD_M6Awi_Y"
}
},
{
"cell_type": "code",
"source": [
"# Attempt 2 using help from Sympy documentation\n",
"from sympy import Function, dsolve, Derivative, checkodesol\n",
"from sympy.abc import t\n",
"y = Function('y')\n",
"result = dsolve(Derivative(y(t), t, t) + 2*Derivative(y(t),t)+26*y(t), y(t))\n",
"checkodesol(Derivative(y(t), t, t) + 2*Derivative(y(t),t)+26*y(t), result)\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "WdojGx2iyZfV",
"outputId": "a17e748a-e3b7-4e59-ec94-9ba618cb09b6"
},
"execution_count": 23,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(True, 0)"
]
},
"metadata": {},
"execution_count": 23
}
]
},
{
"cell_type": "code",
"source": [
"result"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 38
},
"id": "-ylUTxIA0XQe",
"outputId": "57794087-6be1-44ff-8d3f-e4c7f92984c4"
},
"execution_count": 24,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Eq(y(t), (C1*sin(5*t) + C2*cos(5*t))*exp(-t))"
],
"text/latex": "$\\displaystyle y{\\left(t \\right)} = \\left(C_{1} \\sin{\\left(5 t \\right)} + C_{2} \\cos{\\left(5 t \\right)}\\right) e^{- t}$"
},
"metadata": {},
"execution_count": 24
}
]
},
{
"cell_type": "code",
"source": [
"constants = result.subs(t, 0).subs(y(0), 6).subs(Derivative(y(t)).subs(t, 0), 0)"
],
"metadata": {
"id": "6Zn6CfKF0rBI"
},
"execution_count": 27,
"outputs": []
},
{
"cell_type": "code",
"source": [
"constants"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 38
},
"id": "Udz-Jkwr0t_J",
"outputId": "d92d3505-4eae-4a2d-b7bd-a7f20d7c253b"
},
"execution_count": 28,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Eq(6, C2)"
],
"text/latex": "$\\displaystyle 6 = C_{2}$"
},
"metadata": {},
"execution_count": 28
}
]
},
{
"cell_type": "code",
"source": [
"# Attempt 2 using help from Sympy documentation\n",
"from sympy import Function, dsolve, Derivative, checkodesol\n",
"from sympy.abc import t # Independent variable\n",
"x = Function('x')\n",
"result = dsolve(Derivative(x(t), t, t) + 2*Derivative(x(t),t)+26*x(t), x(t),ics={x(t).diff(t).subs(t, 0): 0,x(t).subs(t, 0): 6})\n",
"\n",
"checkodesol(Derivative(x(t), t, t) + 2*Derivative(x(t),t)+26*x(t), result)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "r_uEbnQZ1NAO",
"outputId": "52afbe50-3684-4557-dba4-85065a79e2ff"
},
"execution_count": 31,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(True, 0)"
]
},
"metadata": {},
"execution_count": 31
}
]
},
{
"cell_type": "markdown",
"source": [],
"metadata": {
"id": "MCNjIeJBydB_"
}
},
{
"cell_type": "code",
"source": [
"result"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 58
},
"id": "w4xCYwYL1wxq",
"outputId": "3f76628f-23a6-48c3-ef45-e6d0fa1e0a7f"
},
"execution_count": 32,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Eq(x(t), (6*sin(5*t)/5 + 6*cos(5*t))*exp(-t))"
],
"text/latex": "$\\displaystyle x{\\left(t \\right)} = \\left(\\frac{6 \\sin{\\left(5 t \\right)}}{5} + 6 \\cos{\\left(5 t \\right)}\\right) e^{- t}$"
},
"metadata": {},
"execution_count": 32
}
]
},
{
"cell_type": "markdown",
"source": [
"<h1> Non-Homogeneous Equation x\" + 2x' + 26x -82 cos(4t) = 0"
],
"metadata": {
"id": "0VKXvK2-4DBJ"
}
},
{
"cell_type": "code",
"source": [
"from sympy import Function, dsolve, Derivative, cos, checkodesol\n",
"from sympy.abc import t # Independent variable\n",
"x = Function('x')\n",
"result = dsolve(Derivative(x(t), t, t) + 2*Derivative(x(t),t)+26*x(t)-82*cos(4*t), x(t),ics={x(t).diff(t).subs(t, 0): 0,x(t).subs(t, 0): 6})\n",
"checkodesol(Derivative(x(t), t, t) + 2*Derivative(x(t),t)+26*x(t)-82*cos(4*t), result)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "K4PMIITw4UkO",
"outputId": "0fa90581-355b-4834-d404-da6ce6186036"
},
"execution_count": 33,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(True, 0)"
]
},
"metadata": {},
"execution_count": 33
}
]
},
{
"cell_type": "code",
"source": [
"# The solution to the non-homogeneous equation is\n",
"result"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 38
},
"id": "vApCWNwB45uC",
"outputId": "ffb33080-82e8-4039-b587-59af5dfef9e9"
},
"execution_count": 34,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Eq(x(t), (-3*sin(5*t) + cos(5*t))*exp(-t) + 4*sin(4*t) + 5*cos(4*t))"
],
"text/latex": "$\\displaystyle x{\\left(t \\right)} = \\left(- 3 \\sin{\\left(5 t \\right)} + \\cos{\\left(5 t \\right)}\\right) e^{- t} + 4 \\sin{\\left(4 t \\right)} + 5 \\cos{\\left(4 t \\right)}$"
},
"metadata": {},
"execution_count": 34
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment