Example: stock market

A brief introduction to using ode45 in MATLAB

Nur Adila Faruk SenanDepartment of Mechanical EngineeringUniversity of California at BerkeleyA brief introduction to usingode45inMATLABMATLAB s standard solver for ordinary differential equations (ODEs) is the functionode45. This function implements a runge - kutta method with a variable time step forefficient designed to handle the following general problem:dxdt=f(t,x),x(t0) =x0,(1)where t is the independent variable,xis a vector of dependent variables to be found andf(t,x) is a function oftandx. The mathematical problem is specified when the vectorof functions on the right-hand side of Eq. (1),f(t,x), is set and the initial conditions,x=x0at timet0are ME175, the solution is often not complete once you have solved the problem andobtained the ode s governing the systems motion.

ode45. This function implements a Runge-Kutta method with a variable time step for e cient computation. ode45 is designed to handle the following general problem: dx dt = f(t;x); x(t 0) = x 0; (1) where t is the independent variable, x is a vector of dependent variables to be found and f(t;x) is a function of tand x.

Tags:

  Runge, Kutta, Runge kutta

Information

Domain:

Source:

Link to this page:

Please notify us if you found a problem with this document:

Other abuse

Advertisement

Transcription of A brief introduction to using ode45 in MATLAB

1 Nur Adila Faruk SenanDepartment of Mechanical EngineeringUniversity of California at BerkeleyA brief introduction to usingode45inMATLABMATLAB s standard solver for ordinary differential equations (ODEs) is the functionode45. This function implements a runge - kutta method with a variable time step forefficient designed to handle the following general problem:dxdt=f(t,x),x(t0) =x0,(1)where t is the independent variable,xis a vector of dependent variables to be found andf(t,x) is a function oftandx. The mathematical problem is specified when the vectorof functions on the right-hand side of Eq. (1),f(t,x), is set and the initial conditions,x=x0at timet0are ME175, the solution is often not complete once you have solved the problem andobtained the ode s governing the systems motion.

2 It is often beneficial to produce avisual representation of what exactly the trajectories of a particle represented by ahighly complicated looking ordinary differential equation looks like and the following isa brief explanation of how to accomplish 1: Reduce the given ode to a series of first order equationsThis is the very first step you should do, ideally on a separate piece of paper. Forexample, if the given ode is,m y+ yey y2= 5, y0= 3, y0= 1,(2)then the vectorxhas two components:yand y, or,x(1) =yx(2) = y,(3)andddtx(1) =x(2)ddtx(2) =1m(5 x(2)ex(1)+ (x(1))2),(4)where we have made use of the representations fory, yand ygiven in Eqn (3) to write(4).

3 What if we have more than one ode? For example, suppose that in addition to Eq. (2)above, we also have a second equation,d3zdt3+d2zdt2 sin(z) =t, z0= 0, z0= 0, z0= 1.(5)Then, we can easily solve for bothyandzsimultaneously by makingxa larger vector:x(3) =zx(4) =dzdtx(5) =d2zdt2(6)1 Thus,x= [y, y,z, z, z],(7)andx|t=0=[y|t=0,dydt|t=0,z|t=0,dzd t|t=0,d2zdt2|t=0].(8)There is no preference for placing the y-related variables as the first two variables insteadof the z-related variables. In fact, any arbitrary order is perfectly valid, such as,x= [y,z, y, z, z] andx= [z, z, z,y, y].(9)What is important however is that you remain consistent throughout your computationsin terms of how you write out your system of first order ode , if you use theordering given in Eq.

4 (7), then the system of first order ode s are comprised of,ddtx(1) =x(2)ddtx(2) =1m(5 x(2)ex(1)+ (x(1))2),ddtx(3) =x(4)ddtx(4) =x(5)ddtx(5) =t x(5) + sin (x(3)),(10)while employing the representationx= [y,z, y, z, z] results in,ddtx(1) =x(3)ddtx(2) =x(4)ddtx(3) =1m(5 x(2)ex(2)+ (x(2))2),ddtx(4) =x(5)ddtx(5) =t x(5) + sin (x(2)).(11)Basically, you could have an arbitrary number of higher order ode s. What is importantis that you reduce them to multiple first order ode s and keep track of what order you veassigned the different derivatives in the finalxvector that will be 2: Coding it inNow that you have everything in first order form, you will need the following commandsin your main code:[t,x] = ode45 (@fname, tspan, xinit, options) fnameis the name of the function Mfile used to evaluate the right-hand-sidefunction in Eq.

5 (1). This is the function where we will input the system of firstorder ode s to be integrated (such as in Eqs. (10) and (11)). I will explain this ina little more detail later course, there might be some subtleties with regards to howode45numerically integrates thegiven equation and in some cases, it might make sense to solve some equations before others but forthe simple problems we will be dealing with, this is a tspanis the vector defining the beginning and end limits of integration, as wellas how large we want our time steps to be. if we are integrating fromt= 0 tot= 10, and want to take 100 time steps, thentspan=[0 :10] ortspan=linspace(0,10,100)).

6 Xinitis the vector of initial conditions. Make sure that the order corresponds tothe ordering used to writey,zand their derivatives in terms ofx. Also note thatifxconsists of 5 variables, then we need an input of 5 initial conditions (see Eqn.(8)). optionsis something that is very well explained in the help session most purposes, using the default value is sufficient. tis the value of the independent variable at which the solution arrayxis cal-culated. This vector is not necessarily equal totspanabove because ode45 doessome amount of playing about with step sizes to maximize both accuracy and ef-ficiency (taking smaller steps where the problem changes rapidly and larger stepswhere it s relatively constant).

7 The length ofthowever is the same as that oftspan xis what this is all an array (or matrix) with sizelength(t)bylength(xinit). Each column ofxis a different dependent variable. For example,if we havex= [y, y,z, z, z], and assume (for simplicity) that we require only thevalues att= 0,1,2,3,..,10 ( we evaluate the function at 11 different times)thenx= y|t=0 y|t=0z|t=0 z|t=0 z|t=0y|t=1 y|t=1z|t=1 z|t=1 z|t= |t=10 y|t=10z|t=10 z|t=10 z|t=10 .(12)Thus,x(1,4) gives us the value of zatt= 0,x(7,4) gives us the value of zatt= 6, andx(11,4) gives us the value of zatt= 10 since zis the fourth variable inx.

8 The sameholds for all the other variables. In short, x(:,k) gives us the column vector of thek th variable:k= 1 would correspond toy,k= 2 to y, etc. x(j,:) gives us the values of all the computed variables at a single instant of timecorresponding to the invention of the Hokey Pokey dance, ancient children attending ancient campfire eventswould sit around ancient campfires singing:You put your left foot inYou put your left foot outYou put your left foot inAnd you shake it all aboutYou use the ode45 MATLAB functionto get your homeworks done on timexis what it s all about!Unfortunately, the lack of computers (andMATLAB) soon made this version x(j,k) x(time, variable of interest).

9 The line following[t,x] = ode45 (@fname, tspan, xinit, options)should be a re-definition of your variables. Thus, if you usedx= [y, y,z, z, z], then you would write:[t,x] = ode45 (fname, tspan, xinit, options)y=x(:,1);ydot=x(:,2);z=x(:,3);zd ot=x(:,4);zdotdot=x(:,5);Of course, you could just as well not bother to definey,ydot, etc. and instead dealdirectly with the indicial form ofx( usingx(:,1) whenever we meany, etc) butdefining the variables clearly does make things a little easier to debug at 3 in the morningthe day the homework is this, you usually plot what exactly it is you re interested in showing (or have beenasked to show): The trajectory of a particle as a function of time?

10 The relationshipbetween r and for a planar pendulum? etc. Theplot andsubplot commands inMATLABare lucidly explained in theMATLAB help and I won t go into detail aboutthem here. Bear in mind that if you plan to hand in 20 plots, you will do the grader(and mother nature) a favor by using thesubplot function to fit multiple plots into onepage. Don t go overboard with this however - 20 plots on a single page isn t a good ideaeither. Additionally, don t forget to label your graphs adequately: Each plot shouldhave a title, labels for the x-axis, y-axis and a legend if there are multiple curves on thesame plot without attached lables is just a bunch of lines!


Related search queries