Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

2nd order ODE using RK4

Asked by 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.

0 Comments

freestyler000218

Tags

Products

No products are associated with this question.

2 Answers

Answer by 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])

http://www.mathworks.com/help/techdoc/ref/ode23.html

0 Comments

Honglei Chen
Answer by 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 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.

freestyler000218

Contact us