2nd order ODE using RK4
17 views (last 30 days)
Show older comments
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.
0 Comments
Answers (2)
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])
0 Comments
freestyler000218
on 28 Mar 2012
1 Comment
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.
See Also
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!