[Math] Solve a second order DEQ using Euler’s method in MATLAB

MATLABordinary differential equations

I need to solve the equation below with Euler's method:

$$y''+ \pi ye^{x/3}(2y' \sin(\pi x)+\pi y\cos (\pi x)) = \frac{y}{9}$$ for the initial conditions $y(0)=1$, $y'(0)=-1/3$

So I know I need to turn the problem into a system of two first order differential equations.

Therefore $u_1=y'$ and $u_2=y''$ I can now write the system as:

$$u_1=y' \\ u_2=\dfrac{y}{9}-\pi y e^{x/3}(2u_1 \sin(\pi x)-\pi y\cos (\pi x))$$

How do I proceed from here?

Best Answer

Letting $u=y'$ is the right idea. This gives you the pair of equations \begin{align*} u' &= \frac{y}{9} - \pi ye^{x/3}(2u\sin(\pi x) + \pi y\cos(\pi x))\\ y' &= u \end{align*} which is a standard initial value problem. Notice that there are no $x$ derivatives, so you can integrate each value of $x$ separately. So fix some value for $x$ and discretize the system using your favorite method -- I am a big fan of Velocity Verlet: \begin{align*} y_{n+1} &= y_n + hu_n\\ u_{n+1} &= u_n + h \left(\frac{y_{n+1}}{9}-\pi y_{n+1}e^{x/3}(2u_n\sin(\pi x)+\pi y_{n+1}\cos(\pi x))\right), \end{align*} where $h$ is the time step; you could also use e.g. forward or backward Euler (you say you want "Euler's method" but there are at least three!). You know $u_0$ and $v_0$ from the initial conditions, so just iteratively apply the above rules to trace out the trajectory through time.

EDIT: Forward Euler would be

\begin{align*} u_{n+1} &= u_n + h \left(\frac{y_{n}}{9}-\pi y_{n}e^{x/3}(2u_n\sin(\pi x)+\pi y_{n}\cos(\pi x))\right),\\ y_{n+1} &= y_n + hu_{n+1} \end{align*} Notice that I strongly recommend against using Explicit Euler in pretty much any circumstance.

Related Question