Skip to content

Instantly share code, notes, and snippets.

@vuddameri
Created April 28, 2024 06:16
Show Gist options
  • Select an option

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

Select an option

Save vuddameri/fe7eb9e0fd89b7bdb2341c7d5d900c1f to your computer and use it in GitHub Desktop.
Assignment4-Problem2.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyPmTD3Q2gA7MTN0CEUe1zgc",
"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/fe7eb9e0fd89b7bdb2341c7d5d900c1f/assignment4-problem2.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"id": "9803PvKUJEMl"
},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"source": [
"# <h4> Assignment 4: Problem 2"
],
"metadata": {
"id": "L6a0JlACJFBY"
}
},
{
"cell_type": "markdown",
"source": [
"Find the particular solution of 3y” + y’-2y = 2cos(x)."
],
"metadata": {
"id": "MoRea56dJOPB"
}
},
{
"cell_type": "markdown",
"source": [
"# <h5> Solution Strategy:"
],
"metadata": {
"id": "DPR0XqucJSef"
}
},
{
"cell_type": "markdown",
"source": [
"\n",
"\n",
"1. Check if the characteristic equation less than zero. If it is then there will be a cos term in the complementary solution.\n",
"2. If there is no cos(x) term in the complementary solution the guess for yp = Acos(x) + Bsin(x) - Make sure you include both sin and cosine\n",
"3. If there is a cos(x) term in the complementary solution then use the guess Axcos(x) + Bsin(x) as the guess and proceed. \n",
"\n"
],
"metadata": {
"id": "mPaP1hDHJYK0"
}
},
{
"cell_type": "markdown",
"source": [
"<hr>"
],
"metadata": {
"id": "_kyac33SLIhf"
}
},
{
"cell_type": "markdown",
"source": [
"# <h5> Code"
],
"metadata": {
"id": "yC9quOIEU7R4"
}
},
{
"cell_type": "code",
"source": [
"# Import libraries\n",
"import numpy as np\n",
"import sympy as sp"
],
"metadata": {
"id": "2DrQUCsCLN8p"
},
"execution_count": 35,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Calculate the value of the discriminant function\n",
"a = 3\n",
"b = 1\n",
"c = -2\n",
"disc = np.sqrt(np.power(b,2)-4*a*c)\n",
"if(disc<0):\n",
" print(\"Cosine term in Complementary Solution: Guess = (AxCos(x) + BxSin(x))\")\n",
"else:\n",
" print(\"No cosine term in Complementary solution: Guess = (ACos(x)+BSin(x))\")"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "_10baOBMLes2",
"outputId": "a0c8e238-433c-45f5-ac03-f5ed22ebe286"
},
"execution_count": 42,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"No cosine term in Complementary solution: Guess = (ACos(x)+BSin(x))\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# Assume the Particular solution is Acos(x) + B sin(x)\n",
"x= sp.symbols('x')\n",
"A = sp.symbols('A')\n",
"B = sp.symbols('B')\n",
"y = sp.Function('y')\n",
"y = A*sp.cos(x)+B*sp.sin(x) # define function\n",
"y1 = y.diff(x) # Get the first derivative\n",
"y2 = y.diff(x,x) # Get the second derivative term\n",
"\n"
],
"metadata": {
"id": "nsOrnoNBMr6S"
},
"execution_count": 44,
"outputs": []
},
{
"cell_type": "code",
"source": [
"ode = 3*y2 + y1 - 2*y-2*sp.cos(x) # Write the ODE bring the Forcing function from RHS to LHS\n",
"sp.solve(odex,[A,B]) # Solve non-homogeneous ODE for unknowns A and B of the particular solution"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "fvSR1DCQPIPR",
"outputId": "a1ffa57c-cf75-496d-8739-35d7da454a48"
},
"execution_count": 47,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"{A: -5/13, B: 1/13}"
]
},
"metadata": {},
"execution_count": 47
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment