The posted code actually has a bug. Also it is almost set up properly for vector solutions, but not quite. Make these changes:
yp = zeros(numel(tspan),numel(y0));
k2 = dydt(tp(n) + (1/2)*h, y0 + (1/2)*k1*h);
k3 = dydt(tp(n) + (1/2)*h, y0 + (1/2)*k2*h);
k4 = dydt(tp(n) + h, y0 + k3*h);
yp(n+1,:) = y0 + ((1/6)*(k1 + 2*k2 + 2*k3 + k4)*h);
Then it is just a matter of defining your derivative function. For your case it is 2nd order so you will need a function that returns a two element vector. Using this equation:
Define a two element state vector y such that:
y(1) = the original y
y(2) = y'
Then the derivatives are simply:
d y(1) /dx = dy/dx = y' = y(2)
d y(2) /dx = dy'/dx = y'' = 8x(9-x)-2y = 8*x*(9-x)-2*y(1)
Then you simply write this function coding these derivatives:
function dy = myderivative(x,y)
dy = [y(2),8*x*(9-x)-2*y(1)];
Where the 2nd element is obtained by solving the y''+2y=8x(9-x) equation for y'' and using y(1) for the original y.
One caveat to this is that your posted code is set up to store the state vectors as rows of the final solution, so that is how i wrote the derivative function ... i.e., it returns a row vector. However, if you use ode45( ) your derivative function would have to return a column vector instead. Something to be aware of.