Euler's method for second order differential - Mathematics Stack Exchange
Solve 2nd order ODE using Euler Method
Modified Euler Method for second order differential equations - Mathematics Stack Exchange
Math HL Calculus: How do we do Euler's Method for Differential Equations on a Calculator?
Videos
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?
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;
}
As lhf mentioned, we need to write this as a system of first order equations and then we can use Euler's Modified Method (EMM) on the system.
We can follow this procedure to write the second order equation as a first order system. Let $w_1(x) = y(x)$, $w_2(x) = y'(x)$, giving us:
- $w'_1 = y' = w_2$
- $w'_2 = y'' = \left(\dfrac{2}{x}\right)w_2 -\left(\dfrac{2}{x^2}\right)w_1 - \dfrac{1}{x^2}$
Our initial conditions become:
$$w_1(1) = 0, w_2(1) = 1$$
Now, you can apply EMM and you can see how you step through that (only Euler method, but it will give you the approach) at The Euler Method for Higher Order Differential Equations
From the given conditions, we are only doing one step of the algorithm, since we are starting at $x=1$ and want to find the result at $x=1.2$, where $h=0.2$.
Also note, we can compare the numerical solution to the exact result, which is:
$$y(x) = \dfrac{1}{2}\left(x^2-1\right)$$
That should be enough to guide you.
Since this is a second-order equation, you need to pack $u=(y,y')$ and then write $u'=(y',y'')=(y',f(x,y))=g(x,u)$.