# Writing code to implement Runge-Kutta methods 1 and 4 for a second order nonlinear ODE

34 views (last 30 days)
Bear12 on 5 May 2016
Answered: Manali Gupta on 21 Jun 2019
Hi, I am very stuck... I rewrote the equation to where it is written as two 1st order ODE's.
Given: y"(t)+0.05y'(t)+y^3(t)=7.5cos(t)
After re-writing I have:
y_1=y y'_1=y_2
y_2=y' y'_2=-0.05y_2-(y^3_1)+7.5cos(t)
So,
y_(1,i+1)=y_(1,i)+h*y_(2,i) y_(2,i+1)=y_(2,i)+h*(-0.05y_(2,i)-y^3_1+7.5cos(t)
Initial conditions as: y_1(0)=0 and y_2(0)=1
I need to implement these y_1 and y_2 into the Runge Kutta methods for RK1 and RK4.
Thank you!

Jan on 5 May 2016
Please use the "{} Code" button to format your equations. Currently they are not readable. Are you aware that we cannot guiess what "RK1" and "RK4" means? Did you search for the thousands of examples for Runge-Kutta integrators written in Matlab?
James Tursa on 5 May 2016
@Jan: The usual meaning is RK1 is 1st order Runge Kutta (i.e., Euler Method), and RK4 is 4th order Runge Kutta.
James Tursa on 5 May 2016
@Bear12: Are you intending to use MATLAB provided functions for the integration? I.e., ode45? Or are you supposed to write the RK1 and RK4 integration code yourself?

Manali Gupta on 21 Jun 2019
Hi,
Attached file trk.m calculates the derivatives of the variables. The RK4 algorithm to solve the set of equations is implemented in solveOde.m.
I hope this helps.