<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Lecture 4
Step1:  We first solve the homogeneous equation
Step2:  Now solve the non-homogeneous case for some $f(x)$
<ASSISTANT_TASK:>
Python Code:
from sympy import *
init_printing()
from IPython.display import display
Explanation: Lecture 4: Ordinary differential equation extensions
This notebook presents some extensions of material handled in lectures. It presents 
A general method for non-homogeneous second-order ordinary differential equations; and
The formulation of the differential equation for an elastic rod in one dimension via an integral approach.
We present an extension to what was covered in the lecture, namely the variation of parameters method for non-homogeneous equations.
Method of undetermined coefficients
In the previous lecture, we covered the solution of non-homogeneous second-order differential equations. Although not explicitly stated, the method we used is known as method of undetermined coefficients. It relied on computing a solution to the homogeneous problem, and then having a suitable 'guess' for for the particular solution $y_{P}$.
But what if we do not know what $y_{P}$ should look like? In this case we can use the variation of parameters method.
Variation of parameters method
We wish to find a solution to the problem
$$
\frac{d^{2}y}{dx^{2}} + p(x) \frac{dy}{dx} + q(x) y = f(x)
$$
By linearity, if $y_{P}$ is a solution to the above equation and $y_{CF}$ is a solution to the homogeneous equation,
$$
\frac{d^{2}y}{dx^{2}} + p(x) \frac{dy}{dx} + q(x) y = 0
$$
then the general solution is $y = y_{CF} + y_{P}$. 
For the case of constant coefficients, we know how to find the solution to the homogeneous problem, which is most cases is of the form
$$
y_{CF} = \underbrace{A_{1} e^{\lambda_{1} x}}{y{1}} + \underbrace{A_{2} e^{\lambda_{2} x}}{y{2}} 
$$
where $\lambda$ are the roots of $\lambda + p\lambda + q$.
Consider a particular solution $y_{P}$ of the form 
$$
y_{P} = v_{1}(x) y_{1} + v_{2}(x)y_{2}
$$
with the objective of finding the functions $v_{1}$ and $v_{2}$. We will insist of the constraint
$$
\frac{d v_{1}}{dx} y_{1} + \frac{d v_{2}}{dx} y_{2} = 0.
$$
Computing derivatives of $y_{P}$:
$$
\begin{align}
\frac{d y_{P}}{d x} &= v_{1} \frac{d y_{1}}{dx} + v_{2} \frac{d y_{2}}{dx}
\
\frac{d^{2} y_{P}}{d x^{2}} &= \frac{d v_{1}}{dx} \frac{d y_{1}}{dx} + \frac{d v_{2}}{dx} \frac{d y_{2}}{dx} +
v_{1} \frac{d^{2} y_{1}}{dx^{2}} + v_{2} \frac{d^{2} y_{2}}{dx^{2}}
\end{align}
$$
Inserting the expressions for $y_{P}$ is its derivatives into the original equations (keeping in mind that $y_{1}$ and $y_{2}$ are solutions to the homogeneous problem), we find
$$
\frac{d v_{1}}{dx} \frac{d y_{1}}{dx} + \frac{d v_{2}}{dx} \frac{d y_{2}}{dx} = f(x)
$$
Combining the above equations with the conditon $v_{1}^{\prime} y_{1} + v_{2}^{\prime} y_{2} = 0$,
$$
\begin{align}
\frac{d v_{1}}{dx} &= -\frac{y_{2} f(x)}{W(x)}
\
\frac{d v_{2}}{dx} &= \frac{y_{1} f(x)}{W(x)}
\end{align}
$$
where $W(x) = y_{1} y^{\prime}{2} - y^{\prime}{1} y_{2}$ is known as the Wronskian. Integrating both sides of the above equations,
$$
\begin{align}
v_{1} &= -\int \frac{y_{2} f(x)}{W(x)} dx
\
v_{2} &= \int \frac{y_{1} f(x)}{W(x)} dx
\end{align}
$$
Solving a given non-homogeneous equation symbolically
We now solve and ODE symbolically with a RHS side $f(x)$ of unknown form. We first import SymPy:
End of explanation
x = Symbol("x")
y = Function("y")
f = Function("f")
eqn = Eq(Derivative(y(x), x, x) + 2*Derivative(y(x), x) + y(x), 0)
display(eqn)
dsolve(eqn)
Explanation: We first solve the homogeneous equation:
End of explanation
eqn = Eq(Derivative(y(x), x, x) + 2*Derivative(y(x), x) + y(x), f(x))
dsolve(eqn)
Explanation: Now solve the non-homogeneous case for some $f(x)$:
End of explanation
<END_TASK>