SOLVE SECOND ORDER DIFFERENTIAL EQUATIONS IN MATLAB
algebra solve rational radical quadratic expression calculator, combination of the multiplication and division of rational expression , square numbers and square roots activities and games, ratios and percentages in maths in power point , rational radical quadratic expression calculator  

Thank you for visiting our site! You landed on this page because you entered a search term similar to this: solve second order differential equations in matlab, here's the result:


Solving Differential Equations

Matlab has two functions, ode23 and ode45, which are capable ofnumerically solving differential equations. Both of them use asimilar numerical formula, Runge-Kutta, but to a different order ofapproximation.

The syntax for actually solving a differential equation with thesefunctions is:

[T,Y] = ode45('yprime',t0,tF,y0);
'yprime' is the name of a function that you write that describes yoursystem of differential equations. t0 and tf are the initial and finaltimes that you want a solution for, and y0 is a vector of the initialvalues of the variables in your system of equations. You get back avector of times, T, and a matrix Y that has the values of eachvariable in your system of equations over the times in the timevector. Each column of Y is a different variable.

The hardest part of this is actually defining your differentialequation so that matlab understands it. Matlab has a very specificway to define a differential equation, as a function that takes onevector of variables in the differential equation, plus a time vector,as an argument and returns the derivative of that vector. The onlyway that matlab keeps track of which variable is which inside thevector is the order you choose to use the variables in. You defineyour differential equations based on that ordering of variables in thevector, you define your initial conditions in the same order, and thecolumns of your answer are also in that order.

If you follow a careful system to write your differential equationfunction each time you need to solve a differential equation, it's nottoo difficult. I'll do an example in this system to explain how itworks. In my example, x and y are functions of t.

The system of differential equations we're trying to solve is_1The first thing to notice is that this is not a first orderdifferential equation, because it has an_1 in it.To create a function that returns a second derivative, one of thevariables you give it has to be the first derivative. So thevariables the function needs to start with are_1 ,_1 , and_1 . The function needs to tellmatlab how to get from those variables to_1 ,_1 , and_1 .

The first step is to write the first line of the function. Since xand y are functions of t, and we eventually want the differentialequation to give us x and y, we'll define the function with

function dxy = diffxy(t, xy)
This function is called diffxy. That means it should be in a filecalled
diffxy.m
. It takes a vector xy (which has all threeof the variables we said we had to give matlab to define ourdifferential equation in it) and a time vector, and gives us thederivative of the three things in the the xy vector.

The next step is to assign some variables from the xy vector that thisfunction is given. This is when we decide what order the three thingsin the xy vector will be in. We could just keep calling them xy(1),xy(2), and xy(3), but then we might forget which one of them wassupposed to represent x, which one was supposed to represent y, etc.So the first thing we do is rename some variables:

x = xy(1);xdot = xy(2);y = xy(3);
Now we can use x, xdot, and y to define the three things that thisfunction has to return---xdot, xdoubledot, and ydot. This is where weactually refer to the differential equation and implement it.

The first definition is trivial. We need to express xdot (thederivative of x) in terms of the three variables we're given. xdot isalready one of the three variables we're given, so we can use it tostart with:

xdot = xdot;
Then xdoubledot comes from the first equation:

xdoubledot = 3 - ydot + 2*xdot;
Whoops, we don't _have_ a ydot yet. It's not one of the threevariables we're given in xy. But the second equation tells us whatydot is equal to in terms of things we _are_ given in xy. So what wereally want to say is

ydot = 3*x + 2*y + 5;xdoubledot = 3 - ydot + 2*xdot;
The last thing we want to define is ydot, which is the derivative ofy. We just defined it above in order to get xdoubledot, so we'redone.

So dxy, the thing we return, has to contain xdot, xdoubledot, and ydotin order. The final thing we have to do is combine those values intothe vector dxy:

dxy = [xdot; xdoubledot; ydot];
Then we have a finished function that takes t and xy, and returns dxy,which is the derivative of all the elements in xy. You can followthis approach for any number of differential equations.

The final program, diffxy.m, looks like

function dxy = diffxy(t, xy)%%split xy into variables in our equations%x = xy(1);xdot = xy(2);y = xy(3);%% define the derivatives of these variables from equations%xdot = xdot;ydot = 3*x + 2*y + 5;xdoubledot = 3 - ydot + 2*xdot;%%return the derivatives in dxy in the right order%dxy = [xdot; xdoubledot; ydot]
To get a solution, we can type

[T, XY] ode45('diffxy',0,10,[0 1 0])
which uses times from 0 to 10 and assumes that the initial value of xis 0, xdot is 1, and ydot is 0, and gives us a time vector and a threecolumn vector that contains values of x, xdot, and y over that timevector.