{ "cells": [ { "cell_type": "markdown", "id": "0ccff721-972d-4559-a495-c7b72251aaa9", "metadata": {}, "source": [ "# Ordinary Differential Equations\n", "**Instructor:** Bingchen Liu (bil013@ucsd.edu)\n", "\n", "**TAs:** Argen Smith (argen@ucsd.edu) and Lauren Harvey (lrharvey@ucsd.edu)\n", "\n", "## 1 | Notation\n", "\n", "In these notes, I use $y$ as the dependent variable and $x$ as the\n", "independent variable consistently. However, it’s important to note that\n", "these can be represented by any character. For example, one of the most\n", "common is $x$ being the dependent variable and $t$ being the independent\n", "variable, especially in physical, time-dependent problems. \n", " \n", "In these notes, I use the notation $\\frac{dy}{dx}$ to represent a\n", "derivative of $y(x)$ with respect to $x$. However, the notation $y'(x)$\n", "is often used. Here we would write $\\frac{d^2y}{dx^2}$ as $y''(x)$. In\n", "this case, some high $n^{th}$ derivative of $y$, written here as\n", "$\\frac{d^ny}{dx^n}$ is written as $y^{(n)}(x)$. \n", " \n", "In time-dependent problems, the derivative of a function with respect to\n", "time is often written as $\\dot{x}(t)$, with higher derivatives\n", "increasing the number of dots (up to a reasonable point, before\n", "$x^{(n)}(t)$ is used), so a second derivative is written as\n", "$\\ddot{x}(t)$.\n", "\n", "## 2 | Classification of ODEs\n", "\n", "### 2.1 What is an ODE?\n", "\n", "An ordinary differential equation is an equation that relates the\n", "derivative(s) ($\\frac{dy}{dx}$) of the dependent variable (y) to some\n", "function of the the independent variable (x) and dependent variable\n", "itself (y(x)): $$\\frac{dy}{dx} = F(x,y).$$\n", "\n", "The word ‘ordinary’ means that the differential equation dependent on\n", "**only** a single independent variable (x), in contrast to partial\n", "differential equations (PDEs) that is respect to **more** than one\n", "independent variable (x, y, z...).\n", "\n", "### 2.2 Order of an ODE\n", "\n", "The order of an ODE refers to the highest derivative of the function\n", "present in the equation. For example,\n", "\n", "$$\\frac{d^{2}y}{dx^2} +\\frac{dy}{dx} +y(x) = 0$$ is **second order**,\n", "because the highest derivative, $\\frac{d^2y}{dx^2}$, is the second\n", "derivative of the function $y(x)$.\n", "$$\\frac{d^{5}y}{dx^5} +4\\frac{dy}{dx} +y(x) = 0$$ is **fifth order**,\n", "because the highest derivative is the fifth derivative of $y(x)$.\n", "\n", "### 2.3 Linear vs Non-linear ODEs\n", "\n", "A linear ODE is linear in the dependent variable, $y(x)$, and all of its\n", "derivatives. This means that the dependent variable $y(x)$ cannot have a\n", "power that isn’t one or zero, and nor can any of its derivatives. The\n", "dependent variable and its derivatives also cannot multiply or divide\n", "each other. The general form of an $n^{th}$-order linear ODE is:\n", "$$A_n(x) \\frac{d^ny}{dx^n} + A_{n-1}(x) \\frac{d^{n-1}y}{dx^{n-1}} + ... + A_2(x) \\frac{d^2y}{dx^2} + A_1(x) \\frac{dy}{dx} + A_0(x) y(x) = f(x).$$\n", "It is important to notice that the functions\n", "${A_n(x), A_{n-1}(x), ... , A_2(x), A_1(x), A_0(x)}$ do not depend on\n", "the dependent variable or any of its derivatives. These are arbitrary\n", "functions of $x$. Although it would make it hard to solve, it can still\n", "be linear even if these functions are exponential, trigonometric,\n", "hyperbolic, or anything else, so long as they are only functions of\n", "$x$. \n", " \n", "In general, most ODEs you’ll be able to solve analytically (meaning, by\n", "hand) are linear (though certainly not all linear ODEs can be solved\n", "analytically!). There are some exceptions though! \n", " \n", "On the other hand, a non-linear ODE violates these rules. For example,\n", "it may have the dependent variable and/or its derivatives raised to a\n", "power of two or more, or it could have any or all of the functions\n", "${A_n(x), A_{n-1}(x), ... , A_2(x), A_1(x), A_0(x)}$ depend on $y$ or\n", "its derivatives.\n", "\n", "### 2.4 Homogeneous vs Inhomogeneous ODEs\n", "\n", "A homogeneous ODE is an ODE for which $y = 0$ is a solution. In\n", "practice, this means that a homogeneous ODE does not contain any\n", "function of $x$ that is not multiplied by a function of $y$ or its\n", "derivatives. For example, $$\\frac{d^2y}{dx^2} + y = 0,$$ is a\n", "homogeneous ODE. But, $$\\frac{d^2y}{dx^2} + y + \\sin(x)= 0,$$ is an\n", "inhomogeneos ODE. A homogeneous ODE is usually written in a form so that\n", "the right hand side is 0. This means that every term on the left hand\n", "side is a function of $y$, its derivatives, and $x$, but none are\n", "functions of $x$ alone. \n", " \n", "An inhomogeneous ODE is usually written with the function of $x$ alone\n", "(this function is referred to as the forcing) on the right hand side.\n", "For example, the inhomogeneous example from before would be written as:\n", "$$\\frac{d^2y}{dx^2} + y= -\\sin(x),$$ where $-\\sin(x)$ is the forcing.\n", "However, of course, ODEs aren’t always written in this form, but it’s\n", "often how you’ll see them in practice. \n", " \n", "As a quick exercise, let’s classify the following ODEs:\n", "$$\\frac{d^{2}y}{dx^2} +4\\frac{dy}{dx} +3y(x) = 0$$\n", "$$x\\frac{dy}{dx} + 4y(x) = \\frac{\\cos(x)}{x^3}$$\n", "$$e^x\\frac{d^{3}y}{dx^3} +4\\sin(x)\\frac{dy}{dx} +x^2y(x) = 0$$\n", "$$yx\\frac{d^{2}y}{dx^2} +4\\frac{dy}{dx} +y(x) = 0$$\n", "$$\\frac{dy}{dx} - \\tan(x)y = e^x$$ $$(\\frac{dy}{dx})^2 +y(x) = 0$$\n", "$$\\arcsin(x)e^x\\frac{d^{5}y}{dx^5} +4\\cosh(x)e^{x^2-x^3}\\sin(x)\\frac{dy}{dx} + y(x) = 0$$\n", "$$\\frac{dy}{dx} = y+x^2y$$ $$(\\frac{dy}{dx})^2 = y^2$$\n", "\n", "## 3 | An Introduction to Solving ODEs\n", "\n", "Many ODEs cannot be solved analytically. For those taking Janet’s class,\n", "you’ll go through a lot of different solution strategies, but a few are\n", "assumed to be known at the beginning of the class. I’ll go through these\n", "briefly today.\n", "\n", "### 3.1 Boundary and Initial Conditions\n", "\n", "An ODE is incomplete without specifying boundary or initial conditions.\n", "Without these conditions, you can only solve an $n^{th}$-order ODE up to\n", "$n$ arbitrary constants (this is called the general solution to the\n", "ODE). These constants can be specified by applying $n$ boundary or\n", "initial conditions. \n", " \n", "Note that a total of n conditions are required to solve n-th order\n", "ODEs. \n", " \n", "Boundary conditions are when the dependent variable is specified at $n$\n", "values of the independent variable. For example, for a second order ODE,\n", "the boundary conditions are $y(a) = A, \\ \\ y(b) = B$. In order to apply\n", "these conditions, you first need to find the general solution to the\n", "ODE, and then find $y(a), \\ y(b)$ in terms of the two arbitrary\n", "constants, and then solve for the constant by setting them equal to $A$\n", "and $B$ respectively. \n", " \n", "Initial conditions for an $n^{th}$ order ODE specify the value of the\n", "dependent variable and its next $n-1$ derivatives at $t=0$. For a first\n", "order ODE, for example, this is simply specifying $y(0) = A$. For a\n", "second order ODE, $y(0) = A, \\ y'(0) = B$ must both be specified in\n", "order to obtain an exact solution.\n", "\n", "### 3.2 First Order ODEs\n", "\n", "There are many approaches to solving first order ODEs. Today we’ll look\n", "only at the two most common strategies, separation and integrating\n", "factor.\n", "\n", "#### 3.2.1 Separable First Order ODEs\n", "\n", "The simplest way to solve an ODE is to separate so that the LHS is only\n", "a function of $y$, multiplied by $\\frac{dy}{dx}$ and the RHS is only a\n", "function of $x$. However, this only works for a special kind of first\n", "order ODE called separable. These can be written in the form\n", "$$f(y)\\frac{dy}{dx} = g(x).$$ The solution strategy for solving this\n", "kind of ODE is as follows: \n", " \n", "1. Separate the ODE into the form above \n", " \n", "2. \"Multiply\" both sides by $dx$ (in general, its heinous to refer to\n", "this as multiplication. $dx$ is an infinitesimal quantity that\n", "approaches 0, and so formally you cannot multiply through an equation by\n", "$dx$, however, in this case only, it’s easiest to do this as if you\n", "can). Now you have an equation of the form $$f(y)dy = g(x)dx$$. \n", " \n", "3. Integrate both sides! After integrating, you have the general\n", "solution to the ODE. $$\\int f(y) dy = \\int g(x) dx$$ \n", " \n", "4. Apply the initial/boundary condition to specify the constant of\n", "integration. What is left is the exact solution to the ODE. \n", " \n", "**Example:** $\\frac{dy}{dx} + \\sin(x)y = 0$, subject to the initial\n", "condition $y(0) = 5e$ \n", " \n", "1. To get this in separable form, we can move the second term to the\n", "RHS, and then divide both sides by $y$. This becomes:\n", "$$\\frac{1}{y} \\frac{dy}{dx} = -\\sin(x)$$ \n", " \n", "2. \"Multiply\" by $dx$: $$\\frac{1}{y} dy = -\\sin(x) dx$$ \n", " \n", "3. Integrate!\n", "$$\\int \\frac{1}{y} dy = \\int -\\sin(x) dx \\Rightarrow \\ln{y} = \\cos(x) +C \\\\ \\\\ \\Rightarrow y = e^{\\cos(x) + C} = e^C e^{\\cos(x)} = A e^{\\cos(x)}$$ \n", " \n", "4. Apply the initial condition:\n", "$$y(0) = Ae^{cos(0)} = Ae = 5e \\Rightarrow A = 5$$ and now we have the\n", "solution: $$y = 5e^{\\cos(x)}.$$\n", "\n", "#### 3.2.2 Integrating Factor\n", "\n", "This is the most useful method for solving first order ODEs, as it can\n", "always be used for any linear first order ODE. The trick is to find a\n", "function of $x$ that you can multiply the whole equation by so that it\n", "can be directly integrated. \n", " \n", "Note, the ODE (except in very few cases) must be linear for this to\n", "work. This means it can be written in the form:\n", "$$\\frac{dy}{dx} + P(x)y = Q(x),$$ for some functions of $x$ only $P(x)$\n", "and $Q(x)$. \n", " \n", "The solution strategy for the integrating factor method is as follows: \n", " \n", "1. Find the integrating factor. The integrating factor can be found by\n", "finding $$\\mu(x) = e^{\\int P(x) dx}.$$ Note that in this case, we don’t\n", "need a constant of integration. \n", " \n", "2. Multiply the ODE through by the integrating factor:\n", "$$\\mu(x) \\frac{dy}{dx} + \\mu(x)P(x)y = \\mu(x) Q(x).$$ Due to the form of\n", "the integrating factor, this can ALWAYS be written as\n", "$$\\frac{d}{dx}(\\mu(x)y) = \\mu(x)Q(x).$$ because\n", "$\\frac{d\\mu}{dx} = \\mu(x)P(x)$ by definition. \n", " \n", "3. Integrate! $$y = \\mu^{-1}(x) \\left[A+\\int \\mu(x) Q(x) dx \\right]$$ \n", " \n", "4. Specify the constant of integration with the initial or boundary\n", "condition. \n", " \n", "**Example**: $3x\\frac{dy}{dx} + y = x^3 + x^2$ subject to\n", "$y(1) = \\frac{17}{70}$ \n", " \n", "1. Find the integrating factor: here, we need to rearrange to put it in\n", "the correct form. dividing by $3x$, we get\n", "$\\frac{dy}{dx} + \\frac{y}{3x} = \\frac{1}{3}(x^2 + x)$.\n", "\n", "$P(x) = \\frac{1}{3x}$, so we can find $\\mu(x)$ by\n", "$$\\mu(x) = e^{\\int P(x) dx} = e^{\\int \\frac{1}{3x} dx} = e^{\\frac{1}{3}\\ln{x}} = e^{\\ln{x}^{\\frac{1}{3}}} = x^{\\frac{1}{3}}.$$ \n", " \n", "2. Multiply through by the integrating factor: using the trick outlined\n", "in step 2 above (where $Q(x) = \\frac{1}{3} (x^2+x)$), we can immediately\n", "write\n", "$$\\frac{d}{dx}(y x^{\\frac{1}{3}}) = \\frac{1}{3}(x^{\\frac{7}{3}}+x^{\\frac{4}{3}})$$ \n", " \n", "3. Integrate:\n", "$$yx^{\\frac{1}{3}} = \\frac{1}{10}x^{\\frac{10}{3}} + \\frac{1}{7}x^{\\frac{7}{3}} + A \\Rightarrow y = \\frac{1}{10} x^3 + \\frac{1}{7}x^2 + C$$ \n", " \n", "4. Specify the constant of integration with the initial condition:\n", "$$y(1) = \\frac{17}{70}+C = \\frac{17}{70} \\Rightarrow y = \\frac{1}{10} x^3 + \\frac{1}{7}x^2.$$\n", "\n", "### 3.3 Second Order ODEs\n", "\n", "#### 3.3.1 Classification (again)\n", "\n", "Here, we are only going to look at linear, homogeneous ODEs with\n", "constant coefficients. This means that the ODE can be written in the\n", "form: $$A\\frac{d^2y}{dx^2} +B\\frac{dy}{dx}+ Cy = 0,$$ where $A, B, C$\n", "are all constants. This is called a constant coefficient second order\n", "ODE (and in this case, it’s also homogeneous and linear).\n", "\n", "#### 3.3.2 Solution by Ansatz\n", "\n", "For linear, constant coefficient, second order ODEs, the easiest way to\n", "find a solution is by making a guess. This is called the \"ansatz\". For\n", "constant coefficient, we ALWAYS make the guess that $y \\propto e^{sx}$\n", "for some unknown constant $s$. The solution strategy is as follows: \n", " \n", "1. Sub the ansatz $y \\propto e^{sx}$ into the ODE, and divide through by\n", "$e^{sx}$. There should be no $x$s left in the equation, it will be a\n", "quadratic in $s$. This is called the characteristic equation of the\n", "ODE. \n", " \n", "2. Solve the characteristic equation for $s$, which should have two\n", "solutions. There are three cases: \n", " \n", "(i) the characteristic equation has two real distinct roots. \n", " \n", "(ii) the characteristic equation has a repeated root. \n", " \n", "(iii) the characteristic equation has two roots that are a complex\n", "conjugate pair. \n", " \n", "3. (i) If the characteristic equation has two distinct real roots,\n", "$s = a_1, a_2$, then the general solution to the ODE is\n", "$$y = C_1e^{a_1x} + C_2e^{a_2x}.$$ \n", " \n", "3. (ii) If the characteristic equation has a single repeated root,\n", "$s = a$ then the general solution to the ODE is\n", "$$y = C_1 e^{ax} + C_2 x e^{ax}.$$ \n", " \n", "3. (iii) If the characteristic equation has a complex conjugate pair of\n", "roots $s = a+ib, a-ib$, then the general solution to the ODE is\n", "$$y = e^{ax}(C_1\\sin(bx) + C_2\\cos(bx)).$$ \n", " \n", "4. Use the initial or boundary conditions to solve for $C_1$ and $C_2$\n", "and thus the exact solution. \n", " \n", "**Example**: $\\frac{d^2y}{dx^2} + 4\\frac{dy}{dx} + 5y = 0$, subject to\n", "$y(0) = 3, \\ \\ y(\\pi/2) = e^{-\\pi}$ \n", " \n", "1. Sub in the ansatz: $s^2e^{sx} + 4se^{sx} + 5e^{sx} = 0$, and divide\n", "by $e^{sx}$: $s^2 + 4s + 5 = 0$ is our characteristic equation. \n", " \n", "2. Solve for $s$: using the quadratic formula, we obtain\n", "$s = -2 \\pm i$. \n", " \n", "3. Since we have a complex conjugate pair, the general solution is given\n", "by (iii): $$y = e^{-2x}(C_1\\sin(x) + C_2\\cos(x)).$$ \n", " \n", "4. Use the BCs to solve for $C_1$ and $C_2$:\n", "$$y(0) = C_2 = 3, y(\\pi/2) = e^{-\\pi}(C_1) = e^{-\\pi} \\Rightarrow C_1 = 1$$\n", "so, we have the solution: $$y = e^{-2x}(\\sin(x) + 3\\cos(x)).$$" ] }, { "cell_type": "code", "execution_count": null, "id": "59954827-30a8-4563-8844-fac3fe47fde2", "metadata": { "trusted": true, "vscode": { "languageId": "python" } }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "", "name": "" }, "language_info": { "name": "" } }, "nbformat": 4, "nbformat_minor": 5 }