MATLAB Answers

Mark Purdy

Need help solving second order differential equations using ODE45

Asked by Mark Purdy
on 23 Feb 2012

Hey all,

This is my first post on the community so not entirely sure how it works but here goes!

I'm trying to solve a second order differential equation in the form:

x'' = - (γ*x')+ (x*w^2)-(e*x^3) + F(t); where x is being differentiated with respect to t. 

I've been asked to solve it using the ode45 function and I've spent a while looking at the help in MatLab but I'm stuck. I think I understand how the ode45 function works but I'm not sure how to put the differential equation into a form that Matlab will understand.

So far I have:

   function dx = fx(t,x);
     %have global variables w, y, e, F
     dx = zeros (2,1);
     dx(1) = x(2);
     dx(2)= -y*x(2) + x(1)*w^2 + e*(x(1))^3 + F;

and then I try:

      [T,X] = ode45(@fx, [0 4000], [0 0 1]);

I'm trying to integrate it over t=0 to 4000, and with the intial conditions x=0; dx/dt= 1; at t=0. I've looked at other examples but they've generally been just first order differentials with only two initial conditions. Do I have to add a 3rd part to my 'fx' function to take into account the dx/dt part of the equation, and how would I do this? I'm very much a MATLAB novice and any advice would be appreciated!



No products are associated with this question.

1 Answer

Answer by Jiro Doke
on 23 Feb 2012
 Accepted answer

The initial conditions imply that t=0, so you simply need to pass in the two initial conditions for x and dx/dt. So for your example, [0 1], not [0 0 1].

  1 Comment

Mark Purdy
on 10 Mar 2012

Hey, this was very useful, and put me on the right track, much appreciated!

Discover MakerZone

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

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!