Skip to content

Instantly share code, notes, and snippets.

@vuddameri
Created April 30, 2024 19:36
Show Gist options
  • Select an option

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

Select an option

Save vuddameri/364463b24a2f390686cb3cf7d6ddfc18 to your computer and use it in GitHub Desktop.
Assignment4-5.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/vuddameri/364463b24a2f390686cb3cf7d6ddfc18/assignment4-5.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"id": "d6fb5e38",
"metadata": {
"id": "d6fb5e38"
},
"source": [
"<h4>Problem Statement - Assignment 4-5"
]
},
{
"cell_type": "markdown",
"id": "6ae6aaae",
"metadata": {
"id": "6ae6aaae"
},
"source": [
"Use the method of variation of parameters to solve the ODE: y\" + 9y = sin(3x)"
]
},
{
"cell_type": "markdown",
"id": "8c0c1c51",
"metadata": {
"id": "8c0c1c51"
},
"source": [
"<h4> Solve the Complementary Solution"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "f8f08d60",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 39
},
"id": "f8f08d60",
"outputId": "1516c43f-6640-4cee-8583-6164033f7b50"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Eq(y(x), C1*sin(3*x) + C2*cos(3*x))"
],
"text/latex": "$\\displaystyle y{\\left(x \\right)} = C_{1} \\sin{\\left(3 x \\right)} + C_{2} \\cos{\\left(3 x \\right)}$"
},
"metadata": {},
"execution_count": 1
}
],
"source": [
"# Import library\n",
"from sympy import diff, dsolve, symbols, Function, sin, cos, integrate, simplify\n",
"\n",
"# Define the function and variables\n",
"x = symbols('x')\n",
"y = Function('y')(x)\n",
"hode = y.diff(x,x) + 9*y # Homogeneous Equation for complementary function\n",
"sol = dsolve(hode) #obtain the complementary function or the soln of the homogeneous ode\n",
"sol"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "baf47fef",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 37
},
"id": "baf47fef",
"outputId": "63040aca-ed05-42ad-b638-1290844482b8"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"-3"
],
"text/latex": "$\\displaystyle -3$"
},
"metadata": {},
"execution_count": 3
}
],
"source": [
"# Calculate the Wronksian\n",
"y1 = sin(3*x)\n",
"y1p = y1.diff(x)\n",
"y2 = cos(3*x)\n",
"y2p = y2.diff(x)\n",
"W = y1*y2p - y2*y1p\n",
"simplify(W)"
]
},
{
"cell_type": "markdown",
"id": "22e4e064",
"metadata": {
"id": "22e4e064"
},
"source": [
"Obtain the particular solution using the formula\n",
"\n",
"$ y_p = -y1(x) \\int \\frac{y2(x) f(x)}{W(x)}dx + y2(x) \\int \\frac{y1(x) f(x)}{W(x)}dx $"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "7feca356",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 53
},
"id": "7feca356",
"outputId": "4d26ee2b-8fff-48b3-a109-3fd26746fbb2"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"-x*cos(3*x)/6"
],
"text/latex": "$\\displaystyle - \\frac{x \\cos{\\left(3 x \\right)}}{6}$"
},
"metadata": {},
"execution_count": 5
}
],
"source": [
"Fx = sin(3*x)\n",
"yp1 = -y1* integrate(y2*Fx/W) # This is the first part of the Particular solution\n",
"yp2 = y2* integrate(y1*Fx/W) # this is the second part\n",
"yp = yp1 + yp2\n",
"yp\n",
"simplify(yp) # This is my particular solution"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "f2f427aa",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 48
},
"id": "f2f427aa",
"outputId": "fe4bd0dc-35d2-4e7d-d79c-c557dc5c56d5"
},
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"Eq(y(x), C2*sin(3*x) + (C1 - x/6)*cos(3*x))"
],
"text/latex": "$\\displaystyle y{\\left(x \\right)} = C_{2} \\sin{\\left(3 x \\right)} + \\left(C_{1} - \\frac{x}{6}\\right) \\cos{\\left(3 x \\right)}$"
},
"metadata": {},
"execution_count": 11
}
],
"source": [
"# Check using direct solution:\n",
"odex = hode - Fx\n",
"dsolve(odex)"
]
},
{
"cell_type": "markdown",
"id": "070df20e",
"metadata": {
"id": "070df20e"
},
"source": [
"Note the constant 1/18 for the sin(3x) in the particular solution is subsumed within C2. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5ee15b31",
"metadata": {
"id": "5ee15b31"
},
"outputs": [],
"source": []
}
],
"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.10.12"
},
"colab": {
"provenance": [],
"include_colab_link": true
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment