0

I'd like to solve this system using RK4, on octave: $$ y''(x)+\sin(x)y(x)=0 \\~\\ y(0)= α,~~~ y'(0)= β $$

I know we have to first write $z(x) = y'(x)$ so we have $z'(x)=y''(x)=-\sin(x)y(x)$ and $z(0)=β$. However, for RK4, we need to know what's y' as a function of the other values, to do: \begin{align} K_{11}&=h·y'(X_n,Y_n,Z_n), \\ K_{12} &= h·z'(X_n,Y_n,Z_n) \\ K_{21} &= h·y'(X_n+(h/2), Y_n+(K_{11}/2), Z_n+(K_{12}/2)) \end{align} etc... (with $h$ the step)

To then calculate: $$ Y_{n+1}= Y_n+ (1/6)(k_1+2k_2+2k_3+k_4) $$

How can I find $y'$ as a function of $x$, $y$ and $z$? Since we need it to do the approximation but we do not have it by default in the main equation.

Lutz Lehmann
  • 131,652
Nii
  • 11
  • 1
    You get the numerical value of $y'$ by extrapolating $z$ using the equation for $z'$. To put it another way, the fact that this is second order is not any different than it being a first order system. You still have $(y',z')=f(t,y,z)$ and you still compute the $k$'s (which are vectors) in the same way. – Ian Jun 17 '22 at 14:14
  • I don't know if I understood correctly, (y',z')=f(t,y,z)? Is it another function for (y',z')? And how can I get f(t,y,z) when I don't have z? Sorry for asking, I really don't understand. – Nii Jun 17 '22 at 14:26
  • 1
    Oh, your question is actually about a programming misunderstanding. Specifically, $z$ is part of the state, you have to carry it around throughout. So in the first step you have $k_1=f(0,(\alpha,\beta))=(\beta,-\sin(0) \alpha)=(\beta,0)$. Then $k_2=f(h/2,(\alpha,\beta)+h k_1/2)$ etc. To be explicit, $f(x,(y,z))=(z,-\sin(x) y)$. – Ian Jun 17 '22 at 14:30
  • So f would give me a vector for k? shouldn't I obtain a single value for k1, k2, ect... since my final result is Yn+1 = Yn+(1/6)(k1+2k2+2k3+k4)? – Nii Jun 17 '22 at 14:38
  • 1
    No, at each time you have a vector with two entries, one for $y$ and the other for $z$. – Ian Jun 17 '22 at 14:38
  • I'm sorry, I still can't figure it out. Maybe I'm dumb. Sorry. I really don't understand how to solve this problem despite your explanations. – Nii Jun 17 '22 at 18:37
  • 1
    See my answer to https://math.stackexchange.com/questions/4176684/how-to-implement-a-runge-kutta-method-rk4-for-a-second-order-differential for links to related questions. – Lutz Lehmann Jun 17 '22 at 18:49

0 Answers0