[PDF] A brief introduction to using ode45 in MATLAB



Previous PDF Next PDF







A brief introduction to using ode45 in MATLAB

A brief introduction to using ode45 in MATLAB MATLAB’s standard solver for ordinary di erential equations (ODEs) is the function 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)



Matlab’s Function ode45 - Wayne State University

Matlab’s Function ode45 Matlab has several built-in ODE solvers One particular solver, called ode45, which is based on fourth- and fifth-order Runge-Kutta methods The function can solve a single first-order ODE or a system of ODEs The basic call has the syntax: [t,y]=ode45(fun, tspan, y0), where y is the numerical solution array where each



matlab examples ODE23 45 - Auburn University

First create a MatLab function and name it fun1 m function f=fun1(t,y) f=-t*y/sqrt(2-y^2); Now use MatLab functions ode23 and ode45 to solve the initial value problem numerically and then plot the numerical solutions y, respectively In the MatLab window, type in the following commands line by line >> [tv1 f1]=ode23('fun1',[0 5],1);



Solving ODE in MATLAB - Union College

The basic usage for MATLAB’s solver ode45 is ode45(function,domain,initial condition) That is, we use >>[x,y]=ode45(f,[0 5],1) and MATLAB returns two column vectors, the first with values of x and the second with values of y (The MATLAB output is fairly long, so I’ve omitted it here ) Since x and y are



Using MATLAB to Solve Differential Equations

Solving First Order Differential Equations with ode45 The MATLAB commands ode 23 and ode 45 are functions for the numerical solution of ordinary differential equations They use the Runge-Kutta method for the solution of differential equations This example uses ode45 The function ode45 uses higher order



State Variables and MATLAB ode45 x state vector, Mastering

state vector (state variables) and ode45 Let's use MATLAB® to plot v2 as a function of time Using Kirchhoff's laws in the time domain, we could write the third-order differential equation for v2 and then convert it to three coupled first-order equations following the procedure outlined in the MATLAB® book It is much easier, however, to





Solving ODE in MATLAB - WordPresscom

Inline function: f(x,y) = x*yˆ2+y The basic usage for MATLAB’s solver ode45 is ode45(function,domain,initial condition) That is, we use >>[x,y]=ode45(f,[0 5],1) and MATLAB returns two column vectors, the first with values of x and the second with values of y (The MATLAB output is fairly long, so I’ve omitted it here ) Since x and y are

[PDF] le message andrée chedid fnac

[PDF] grille évaluation projet

[PDF] comment bien faire l amour ? son mari pdf

[PDF] le roman de renart fiche de lecture

[PDF] évaluer un projet d'animation

[PDF] comment évaluer un projet éducatif

[PDF] le roman de renart bibliocollège pdf

[PDF] formation extraction huiles essentielles

[PDF] cours de sexologie pdf

[PDF] inventaire floristique transect

[PDF] comment mieux faire l'amour ? une femme pdf

[PDF] technique d inventaire botanique

[PDF] memoire inventaire floristique

[PDF] relevé floristique définition

[PDF] inventaire floristique définition

Nur Adila Faruk Senan

Department of Mechanical Engineering

University of California at Berkeley

A brief introduction to usingode45inMATLAB

MATLAB's standard solver for ordinary dierential equations (ODEs) is the function ode45. This function implements a Runge-Kutta method with a variable time step for ecient computation.ode45is 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 and f(t;x) is a function oftandx. The mathematical problem is specied when the vector of functions on the right-hand side of Eq. (1),f(t;x), is set and the initial conditions, x=x0at timet0are given. In ME175, the solution is often not complete once you have solved the problem and obtained the ode's governing the systems motion. It is often benecial to produce a visual representation of what exactly the trajectories of a particle represented by a highly complicated looking ordinary dierential equation looks like and the following is a brief explanation of how to accomplish this.

Step 1: Reduce the given ode to a series of rst order equationsThis is the very rst step you should do, ideally on a separate piece of paper. For

example, if the given ode is, my+ _yeyy2= 5; y0= 3;_y0=1;(2) then the vectorxhas two components:yand _y, or, x(1) =y x(2) = _y;(3) and ddt x(1) =x(2) ddt x(2) =1m

5x(2)ex(1)+ (x(1))2

;(4) where we have made use of the representations fory;_yand ygiven in Eqn (3) to write (4). What if we have more than one ode? For example, suppose that in addition to Eq. (2) ab o v e, w e also ha v e a second equation, d 3zdt

3+d2zdt

2sin(z) =t; z0= 0;_z0= 0;z0= 1

:(5) Then, we can easily solve for bothyandzsimultaneously by makingxa larger vector: x(3) =z x(4) =dzdt x(5) =d2zdt 2(6) 1 Thus, x= [y;_y;z;_z;z];(7) and xjt=0=" yjt=0;dydt jt=0;zjt=0;dzdt jt=0;d2zdt

2jt=0#

:(8) There is no preference for placing the y-related variables as the rst two variables instead of 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 computations in terms of how you write out your system of rst order ode's.

1Thus, if you use the

ordering given in Eq. (7), then the system of rst order ode's are comprised of, ddt x(1) =x(2) ddt x(2) =1m

5x(2)ex(1)+ (x(1))2

ddt x(3) =x(4) ddt x(4) =x(5) ddt x(5) =tx(5) + sin(x(3));(10) while employing the representationx= [y;z;_y;_z;z] results in, ddt x(1) =x(3) ddt x(2) =x(4) ddt x(3) =1m

5x(2)ex(2)+ (x(2))2

ddt x(4) =x(5) ddt x(5) =tx(5) + sin(x(2)):(11) Basically, you could have an arbitrary number of higher order ode's. What is important is that you reduce them to multiple rst order ode's and keep track of what order you've assigned the dierent derivatives in the nalxvector that will be solved.

Step 2: Coding it inNow that you have everything in rst order form, you will need the following commands

in your main code: [t,x] = ode45(@fname, tspan, xinit, options) fnameis the name of the function Mle used to evaluate the right-hand-side function in Eq. (1). This is the function where we will input the system of rst order ode's to be integrated (such as in Eqs. (10) and (11)). I will explain this in a little more detail later on.1 Of course, there might be some subtleties with regards to howode45numerically integrates the given equation and in some cases, it might make sense to solve some equations before others but for the simple problems we will be dealing with, this is a non-issue. 2 tspanis the vector dening the beginning and end limits of integration, as well as how large we want our time steps to be. I.e. if we are integrating from t= 0 tot= 10, and want to take 100 time steps, thentspan=[0:0.1:10] or tspan=linspace(0,10,100)). xinitis the vector of initial conditions. Make sure that the order corresponds to the ordering used to writey;zand their derivatives in terms ofx. Also note that ifxconsists 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 ofMATLAB. For most purposes, using the default value is sucient. tis the value of the independent variable at which the solution arrayxis cal- culated. This vector is not necessarily equal totspanabove because ode45 does some amount of playing about with step sizes to maximize both accuracy and ef- ciency (taking smaller steps where the problem changes rapidly and larger steps where it's relatively constant). The length ofthowever is the same as that of tspan xis what this is all about.2xis an array (or matrix) with sizelength(t)by length(xinit). Each column ofxis a dierent dependent variable. For example, if we havex= [y;_y;z;_z;z], and assume (for simplicity) that we require only the values att= 0;1;2;3;:::;10 (i.e. we evaluate the function at 11 dierent times) then x=2 6 yjt=10_yjt=10zjt=10_zjt=10zjt=103 7

7777775:(12)

Thus,x(1,4) gives us the value of _zatt= 0,x(7,4) gives us the value of _zatt= 6, and x(11,4) gives us the value of _zatt= 10 since _zis the fourth variable inx. The same holds for all the other variables. In short, x(:,k) gives us the column vector of thek'th variable:k= 1 would correspond to y,k= 2 to _y, etc. x(j,:) gives us the values of all the computed variables at a single instant of time corresponding to indexj.2 Before the invention of the Hokey Pokey dance, ancient children attending ancient campre events would sit around ancient campres singing:

You put your left foot in

You put your left foot out

You put your left foot in

And you shake it all about

You use the ode45MATLABfunction

to get your homeworks done on time xis what it's all about! Unfortunately, the lack of computers (andMATLAB) soon made this version obsolete. 3 )x(j,k)x(time, variable of interest). The line following[t,x] = ode45(@fname, tspan, xinit, options)should be a re- denition 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); zdot=x(:,4); zdotdot=x(:,5); Of course, you could just as well not bother to deney,ydot, etc. and instead deal directly with the indicial form ofx(e.g. usingx(:,1) whenever we meany, etc) but dening the variables clearly does make things a little easier to debug at 3 in the morning the day the homework is due. Below this, you usually plot what exactly it is you're interested in showing (or have been asked to show): The trajectory of a particle as a function of time? The relationship between r andfor a planar pendulum? etc. Theplot andsubplot commands in MATLABare lucidly explained in theMATLABhelp and I won't go into detail about them 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 t multiple plots into one page. Don't go overboard with this however - 20 plots on a single page isn't a good idea either. Additionally, don't forget to label your graphs adequately: Each plot should have a title, labels for the x-axis, y-axis and a legend if there are multiple curves on the same plot.A plot without attached lables is just a bunch of lines! Finally, note that everything in[t,x] = ode45(@fname, tspan, xinit, options) are just variables. You can use whatever terms you wish -Tinstead oft,x0 instead ofxinit, orOUT instead ofxto cite a few examples. Just remember that if you have dened a variable, then you have to always refer to it by the same name. [TIME,OUT] = ode45(@...)followed byy= x(:,1); will give you an error sincexis undened. (The correct equation should bey= OUT(:,1);) Another common mistake is to redene the same variable. For example, doing some- thing likex= x(:,5);. If you're lucky, you will get an error message. If you're not however (which usually happens if the error is not as glaring), then you could end up spending hours trying to gure out why your solution looks a little funny. Aside: What is'fname?Recall that we still haven't toldMATLABwhat exactly the equations of motion are that need to be integrated. This is wherefname comes in.fname is the name of the function containing all the rst order ode's we wrote right at the beginning. Wecan name this function anything we likeso long as the name you give it is the same as what you use when calling it in[t,x] = ode45(@fname, tspan, xinit, options).

Thus, if you call your functionsuperman, then

[t,x] = ode45(@superman, tspan, xinit, options)is right but [t,x] = ode45(@batman, tspan, xinit, options)is wrong (if you have a function 4 namedbatman in your library, then you'll end up getting the solution to that problem instead!). Furthermore,, the functiondoesn't have to be in the same mleas your original code - some people prefer to write it as a sub-function right at the end of the program, especially if the code isn't too large or complicated. For example, say your function is calledME175example. Then, your mle would look like this: function dxdt = ME175example(t,x) %Here, t,x and dxdt are again, just variables. You can call them whatever you want, %so long as t is the independent variable, x is the vector of dependent variables and %dxdt is the vector of rst order derivatives (with respect to the independent variable) % Dene the constants: m= 1; %Dene the variables to make things little more legible %Recall that x = [y, ydot, z, zdot, zdotdot] y=x(1); ydot=x(2); z=x(3); zdot=x(4); zdotdot=x(5); %Notice that here, x is just a 1 by 5 row vector and not a large array like in %[t,x] = ode45(@fname, tspan, xinit, options) %the array dxdt is the same length as x dxdt = zeros(size(x)); dxdt(1) = ydot; dxdt(2) = 1/m5x(2)exp(y) +y2; %This is ydotdot dxdt(3) = zdot; dxdt(4) = zdotdot; dxdt(5) = t-zdotdot+sin(z); %This is zdotdotdot %Note that the input arguments must be t and x (in that order) even in the case where t is not explicitly used in the function.

A basic templateThe following is a basic template you can just cut and paste intoMATLABevery time you would

like to integrate a higher order ODE:quotesdbs_dbs27.pdfusesText_33