2nd order ODE using RK4

17 views (last 30 days)
freestyler000218
freestyler000218 on 28 Mar 2012
Ok, I have a second order ODE and I need to solve it using Runge-Kutta 4. I know that I have to make it into 2 first order equations.
So what I have is x''+1.14x'+3.14x=2.14cos(t)
I make this look like...
x'=v
x''=dvdt=2.14cos(t)-1.14v-3.14x
my step size is h=1.
and i am going from t=0 to t=5
initial conditions are x(0)=1 and x'(0)=0
I can solve this by hand on paper, however I am not too clear on how to write this out in MATLAB. Any help with the coding is greatly appreciated.

Answers (2)

Honglei Chen
Honglei Chen on 28 Mar 2012
You can use ode45. You first define a function for your differential equation
function dy = myode(t,y)
dy = zeros(2,1);
dy(1) = y(2);
dy(2) = 2.14*cos(t)-1.14*y(2)-3.14*y(1);
then you can call ode45 with your initial conditions
[T,Y] = ode45(@myode,0:5,[1;0])

freestyler000218
freestyler000218 on 28 Mar 2012
is there a way to do this without using ode45? I saw examples of code where people were using something like "feval"
  1 Comment
Matt Tearle
Matt Tearle on 28 Mar 2012
Why would you want to? ode45 is an adaptive stepsize 4th-order method, so it's going to do a better job than the vanilla RK4 we all know and love from our differential equations course.
And you don't need feval, even if you do want to write your own. You can just call a function, either hard-coded or passed to a function as a function handle.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!