Sie sind auf Seite 1von 8

MATLAB Lecture 3

1. Introduction Today we will use MATLAB to numerically solve ordinary dierential equations. 2. Differential Equations They dynamic behavior of systems is an important subject. A mechanical system involves displacements, velocities and accelerations. An electric or electronic system involves voltages, currents and time derivatives of these quantities. An equation that involves one or more derivatives of the unknown function is called an ordinary dierential equation or ODE. The order of the ODE is the order of the highest derivative appearing within the equation. The problems of solving an ODE are classied into initial value problems (IVP) and boundary value problems (BVP), depending on how the conditions at the endpoints of the domain are specied. All the conditions of an initial-value problem are specied at the initial point. On the other hand, the problem becomes a boundary-value problem if the conditions are needed for both initial and nal points. The ODE in the time domain are initial-value problems, so all the conditionsa re specied at the initial time, such as t = 0 or x = 0. For notations, we use t or x as an independent variable. 3. Analytic Methods MATLAB is capable of solving many dierential equations. There is a built-in function dsolve which takes care of things in one fell swoop, all we need to know is how to plug in the dierential equation. Example 3.1. Lets nd the general solution of the dierential equation dy + 2ty = t dt We enter this as follows: dsolve(Dy+2*t*y=t,t) We get the following answer: C2/(2*exp(t^2))+1/2 Equivalently,
C 2et2

1 2

where C is a constant.

Note that MATLAB interprets Dy as the derivative of y , or y , and the t by itself indicates the independent variable. Also note that since we are not entering a function, we dont need the special versions of *, ^ and /. MATLAB gets even better. It can deal with an initial condition too using dsolve. Lets solve the same dierential equation with the initial condition that y (0) = 2.
1

dsolve(Dy+2*t*y=t,y(0)=2,t) And get the following output: 3/(2*exp(t^2)) + 1/2 Lets nish up by plotting this solution on the interval [0, 2]. t=0:.1:2; f=matlabFunction(dsolve(Dy+2*t*y=t,y(0)=2,t)) y=f(t); plot(t,y)

In the above code, I used the command matlabFunction because dsolve outputs a symbolic answer and I needed a function in order to plot the graph (ezplot can get around this, but it has limited capabilities). Youll notice that dsolve initially outputted the solution 3/(2*exp(t^2)) + 1/2 and matlabFunction changed it to @(t)exp(-t.^2).*(3.0./2.0)+1.0./2.0. The @(t) indicates that f is a function of t and then matlabFunction changed *,^ and / to their special function versions of .*, .^ and / (everything else is essentially the same). If youre going to plot the solution in the end, its often easiest to just start out with the line f=matlabFunction(dsolve(.......)). MATLAB can handle solving a wide spectrum of types of dierential equations (nth order dierential equations, systems of dierential equations, boundary value problems, etc.). As one example, well use dsolve to solve a 2nd order dierential equation.
y Example 3.2. Consider the second order boundary value problem d dt2 = 4y with the conditions y (0) = 1 and y ( 3 ) = 0. dsolve has no problem solving this. The
2

syntax for writing

d2 y dt2

in dsolve is D2y.

dsolve(D2y = -4*y, y(0) = 1, Dy(pi/3) = 0,t) ans = cos(2*t) - 3^(1/2)*sin(2*t)

Lets plot this on the interval [0, 4 ]. f=matlabFunction(dsolve(D2y = -4*y, y(0) = 1, Dy(pi/3) = 0,t)) t=0:.1:4*pi; y=f(t); plot(t,y) 4. Numerical Methods Numerical methods are commonly used for solving mathematical problems that are formulated in science and engineering where it is dicult or even impossible to obtain exact solutions. Only a limited number of dierential equations can be solved analytically. Numerical methods, on the other hand, can give an approximate solution to (almost) any equation. An ordinary dierential equation (ODE) is an equation that contains an independent variable, a dependent variable and derivatives of the dependent variable. The simplest method of numerically approximating a dierential equation is called Eulers method. 4.1. Eulers Method. Eulers method is a simple method of solving rst-order ODEs, particularly suitable for quick programming because of their great simplicity, although their accuracy is not terribly high. Eulers method for approximating the solution to the ODE y = f (x, y ) uses linear approximation. We start with a given initial value x0 and y0 , and an x-step, h. x1 = x0 + h and we approximate y1 by following the tangent line at (x0 , y0 ) as follows: y1 = y0 + h f (x0 , y0 ) we continue by this method to nd each subsequent y -value yn+1 = yn + h f (xn , yn ) 4.1.1. Steps for MATLAB Implementation. For some reason, MATLAB does not include Euler functions. Therefore, if you really need one, you have to code it by yourself. MATLAB does, however, have very sophisticated functions built-in for solving ODEs using Runge-Kutta algorithms. (Well get to that later on) The typical steps of Eulers method are given below Step Step Step Step 1 2 3 4 dene f (x, y ) input initial values x0 and y0 input step sizes h and number of steps n calculate x and y : for i=1:n x=x+h y=y+h*f*(x,y) end

As an application, consider the following initial value problem dy x = , y (0) = 1 dx y which we are choosing because it has a very simple analytical solution that we can use to compare our approximation to. Using dsolve, its exact analytic solution can be found to be y = x2 + 1 Here we wish to approximate y (0.3) using Eulers method with step sizes h = 0.1 and h = 0.05. We nd by hand calculation,

x0 = 0, y0 = 1

x1 = 0.1,

x2 = 0.2,

x3 = 0.3

y1 = y0 + hf (x0 , y0 ) = y0 + hx0 /y0 = 1 y2 = y1 + hx1 /y1 = 1.01 y3 = y2 + hx2 /y2 = 1.0298 Since y (0.3) = (0.3)2 + 1 = 1.044030, we nd that Error =

|y (0.3) y3 | 100 = 1.36% y (0.3) Similarly, for the step size h = 0.05, we nd that the error is |y (0.3) y6 | Error = 100 = 0.67% y (0.3) 4.1.2. MATLAB Code. To use Eulers method to approximate numerical solutions we will need to create an m-le. In your folder, create a function m-le called myeuler.m. Dene it as follows: function[x,y]=myeuler(f,xinit,yinit,xfinal,n) %Euler approximate for ODE initial value problem %Calculation of h from xinit, xfinal, and n h=(xfinal-xinit)/n; %Initialization of x and y as column vectors x=[xinit zeros(1,n)]; y=[yinit zeros(1,n)]; %Calculation of x and y for i=1:n x(i+1)=x(i)+h; y(i+1)=y(i)+h*f(x(i),y(i)); end end

Example 4.1. Lets test this function with the dierential equation from before: dy x dx = y and approximate the value of y (0.3). First, we need to dene the function in MATLAB. dy=@(x,y) x./y; Next, lets calculate the exact solution, which we know from above g=@(x) sqrt(x.^2+1); xe=0:0.01:0.3; ye=g(xe); Lets use our myeuler.m le to approximate the solution with various numbers of increments: [x1,y1]=myeuler(dy,0,1,0.3,6); [x2,y2]=myeuler(dy,0,1,0.3,30); Well plot the exact solution and the two approximations on the same set of axes using the plot command: plot(xe,ye,k-,x1,y1,r-.,x2,y2,b--)

Finally, well calculate the error of each approximation of y (0.3) and put this information on our plot.

error1=[First error: , num2str(-100*(ye(end)-y1(end))/ye(end)) %] error2=[Second error: , num2str(-100*(ye(end)-y2(end))/ye(end)) %] text(0.04,1.03,error1,color,r,FontSize,16) text(0.04,1.025,error2,color,b,FontSize,16)

4.2. Built-in Solvers. MATLAB has several dierent functions (built-in) for the nding numerical solutions of ODEs. We will focus primarily on one of them, ode45. These solvers can be used with the following syntax: [x,y]=solver(@odefun,tspan,y0) where solver is the solver you are using, such as ode45 or ode23. odefun is the function that denes the derivates, so odefun denes y as a function of the independent variable (typically x or t) as well as y and other parameters. tspan is a vector that species the interval of the solution (e.g. [0,0.3]). y0 is the initial value of y . [x,y] is the output, which is the solution of the ODE. 4.2.1. Runge-Kutta Methods. There are many variants of the Runge-Kutta method, but the most widely used one is the following. Given: y = f (x, y ) y (xn ) = yn we compute in turn k1 = hf (xn , yn ) h k1 , yn + ) 2 2 h k2 k3 = hf (xn + , yn + ) 2 2 k4 = hf (xn + h, yn + k3 ) 1 yn+1 = yn + (k1 + 2k2 + 2k3 + k4 ) 6 This method is similar to Eulers method. yn+1 is found from yn plus h times the weighted average of several dierent slopes: the slope at the beginning of the interval, the slope at the end of the interval and two approximations of the slope at the midpoint of the interval. k2 = h(f (xn +

4.2.2. Using ode45. ode45 is based on an explicit Runge-Kutta (4,5) formula, the Dormand-Prince pair. That means that the numerical solver ode45 combines a fourth order method and a fth order method, both of which are similar to the classical fourth order RungeKutta method discussed above. The modied Runge-Kutta varies the step size, choosing the step size at each step in an attempt to achieve the desired accuracy. Therefore, the solver ode45 is suitable for a wide variety of initial value problems in practical applications. In general, ode45 is the best function to apply as a rst try for most problems. The following table lists ODE solvers, which are MATLAB built-in functions. Each version of MATLAB includes more and more solvers. These are just a select few. A short description of each solver is included. Solver ode45 ode23 ode113 ode15s Accuracy Description Medium This should be the rst solver you try Low Less accurate than ode45 Low to high For computationally intensive problems Low to medium Use if ode45 failed

Example 4.2. We will return to the previous example of approximating y (0.3) dy where dx =x y and y (0) = 1, instead this time using ode45. You should have g, dy, xe and ye already dened. If not, going back to redene them exactly as in the previous example. To approximate the solution to this IVP using ode45 enter the following: [x3,y3]=ode45(dy,[0,0.3],1); We will now graph the analytic solution and the approximate solution, label our graphs and calculate the error: plot(xe,ye,k-,x3,y3,ro) legend(Analytical,ode45) error=[Error: num2str(-100*(ye(end)-y3(end))/ye(end)) %] text(0.04,1.03,error,color,r,FontSize,16)

Note: The integration proceeds by steps, taken to the values specied in tspan. Note that the step size (the distance between consecutive elements of tspan) does not have to be uniform.

Das könnte Ihnen auch gefallen