I have a question regarding 2nd order Runge-Kutta methods, specifically where it regards the bounds of the solution.
Let's say I have to solve a 1st order ODE $\frac{dy}{dx}=f(x,y)$ numerically using Heun's method:
$$y_{i+1}=y_i+\frac{1}{2}h(k_1+k_2)$$ $$k_1=f(x_i,y_i), k_2=f(x_i+h,y_i+k_1h)$$
The ODE can be solved within a range let's say $a<x<b$. My question is whether the initial condition needs to coincide with $x=a$ or whether it only needs between the bounds.
Here is an example I was messing around with when I noticed the potential issue.
Solve the following ODE using Heun's method within the bounds $a<x<b$ with a step size of $h=0.2$. $$xy^\prime+y=x\sin(x), y(\pi)=2$$
If the range is $1<x<3$ where $x_0=\pi$ is outside of the range the numerical error is very high giving the following plot after calculating in MATLAB. I noticed the shape of the approximation was similar to the exact solution.

If I change the range to $3<x<5$ so that $x_0=\pi$ falls within the range, the error is significantly smaller. Is this because $a<x_0<b$ or is it because $\pi\approx3$ and I should have started at $x_0=3$?

There is only one function that solves the IVP, regardless for which interval you produce the function table/plot.
So for the first plot you would use the nodes $x=\pi,3,2.8,2.6,...$ with the step size the difference between the nodes, always negative and after the first step $-0.2$.
For the second plot you need one step backwards to $x=3$ and then forwards, either from $x=3$ or from $x=\pi$ to the next node $x=3.2$.
The error in all this should be smaller than $0.05$ or $5\%$, so probably well visible but very close to the exact solution.