Euler's method for second order differential Given the differential:
$y'' + y' - y = x$ , $y(0) = 2$ , $y'(0)=1$
I am asked to calculate $y'(2)$ for (a) $h=2$ and (b) $h=1$
I have used Euler's method for a second order differential so I am unsure if how I answered the question is correct and would appreciate if someone could tell if it is.
My attempt
$$
    \begin{matrix}
    x & y & y'& y'' \\
    0 & 2 & 1 & \color{red}{1} \\
    2 & \color{blue}{4} & \color{green}{3} \\
    \end{matrix}
$$
$y''(0) = y(0) + x(0) - y'(0) = \color{red}{1}$
$y(2) = y_0 + h * f'(x_o,y_0,y''_0) = 2+2*(2+0-1) = \color{blue}{4}$
$y'(2) = y'_0 + h * f'(x_o,y_0,y''_0) = 1 + 2 *(2+0-1)  = \color{green}{3}$
So answer for (a) is $y'(2) = 3$
Is that how to apply Euler's method for a second order differential?
Thank you!
 A: No, that's not how we do it.
The first step to applying Euler's method, or most any method originally built for first-order equations, to a higher-order differential equation, is to convert that higher-order equation to a system of first-order equations.
How do we do that? From our initial $Y_0(x)=y$, we define another function $Y_1(x)=y'$. Now, in terms of $Y_0$ and $Y_1$, our equation $y''=-y'+y+x$ becomes the system
\begin{align*}Y_0'(x) &= Y_1(x)\\ Y_1'(x) &= -Y_1(x) + Y_0(x) +x\end{align*}
That's the vector of derivatives of the $Y_i$, written in terms of the $Y_i$ and $x$. And now, what does Euler's method look like on a vector? Exactly the same as for scalars; we estimate
$$\begin{pmatrix}Y_0(x+h)\\Y_1(x+h)\end{pmatrix} \approx \begin{pmatrix}Y_0(x)\\Y_1(x)\end{pmatrix} + \begin{pmatrix}Y_0'(x)\\Y_1'(x)\end{pmatrix}h$$
For part (a), that's
$$\begin{pmatrix}y(2)\\y'(2)\end{pmatrix} \approx \begin{pmatrix}y(0)\\y'(0)\end{pmatrix} + \begin{pmatrix}y'(0)\\-y'(0)+y(0)+0\end{pmatrix}\cdot 2 = \begin{pmatrix}2\\1\end{pmatrix}+\begin{pmatrix}1\\1\end{pmatrix}\cdot 2=\begin{pmatrix}4\\3\end{pmatrix}$$
The value $y'(2)$ we're interested in is $3$.
While this method produced the same answers you got, that looks like a coincidence. It's definitely not the same in the details.
Now that you've seen the method, can you do the second part, with two steps of size 1?
A: Your first step is to convert one 2nd order system into two 1st order systems. This is done by creating a new variable $v = y'$. Now you can write
$$ \left. \begin{aligned}
  v' + v - y & = x \\
  y' &= v 
\end{aligned} \right\}  
\begin{aligned}
  v' &= y-v-x \\
  y' & = v
\end{aligned}
$$
with the initial conditions $y(0)=2$ and $v(0)=1$.
To integrate the above with a scheme like Euler's method, you apply each stage to both equations above:
double v_der(x,y,v) { return y-v-x; }
double y_der(x,y,v) { return v;  }

y = 2, v = 1;
while(x<x_end)
{
    y_step = h*y_der(x,y,v);
    v_step = h*v_der(x,y,v);

    x = x + h;
    y = y + y_step;
    v = v + v_step;
}

