Вы находитесь на странице: 1из 10

Chapter 7 _________________________________________

Ordenary differential equatins and symbolic mathematics


7.1 INTRODUCTION
In mathematics, an ordinary differential equation (or ODE) is a relation that contains functions of only one independent variable, and one or more of their derivatives with respect to that variable. A simple example is Newton's second law of motion, which leads to the differential equation

for the motion of a particle of constant mass m. In general, the force F depends upon the position x(t) of the particle at time t, and thus the unknown function x(t) appears on both sides of the differential equation, as is indicated in the notation F(x(t)). Ordinary differential equations are distinguished from partial differential equations, which involve partial derivatives of functions of several variables. Ordinary differential equations arise in many different contexts including geometry, mechanics, astronomy and population modelling. Many famous mathematicians have studied differential equations and contributed to the field, including Newton, Leibniz, the Bernoulli family, Riccati, Clairaut, d'Alembert and Euler. Much study has been devoted to the solution of ordinary differential equations. In the case where the equation is linear, it can be solved by analytical methods. Unfortunately, most of the interesting differential equations are non-linear and, with a few exceptions, cannot be solved exactly. Approximate solutions are arrived at using computer approximations

7.2 SYNTAX FOR ODE SOLVERS

ode23, ode45, ode113, ode15s, ode23s, ode23t, ode23tb initial value problems for ordinary differential equations Syntax

Solve

[T,Y]=solver(odefun,tspan,y0) [T,Y]=solver(odefun,tspan,y0,options) [T,Y,TE,YE,IE]=solver(odefun,tspan,y0,options) sol = solver(odefun,[t0 tf],y0...)


where solver is one of ode45, ode23, ode113, ode15s, ode23s, ode23t, or ode23tb. Arguments The following table describes the input arguments to the solvers.

odefun

A function handle that evaluates the right side of the differential equations. All solvers solve systems of equations in the form or problems that involve a mass matrix,

. The ode23s solver can solve only equations with constant mass matrices. ode15s and ode23t can solve problems with a mass matrix that is singular, i.e., differential-algebraic equations (DAEs).

tspan

A vector specifying the interval of integration, [t0,tf]. The solver imposes the initial conditions at tspan(1), and integrates from

tspan(1) to tspan(end). To obtain solutions at specific times (all


increasing or all decreasing), use tspan = [t0,t1,...,tf]. For tspan vectors with two elements [t0 tf], the solver returns the solution evaluated at every integration step. For tspan vectors with more than two elements, the solver returns solutions evaluated at the given time points. The time values must be in order, either increasing or

decreasing. Specifying tspan with more than two elements does not affect the internal time steps that the solver uses to traverse the interval from

tspan(1) to tspan(end). All solvers in the ODE suite obtain


output values by means of continuous extensions of the basic formulas. Although a solver does not necessarily step precisely to a time point specified in tspan, the solutions produced at the specified time points are of the same order of accuracy as the solutions computed at the internal time points. Specifying tspan with more than two elements has little effect on the efficiency of computation, but for large systems, affects memory management.

y0

A vector of initial conditions.

options Structure of optional parameters that change the default integration


properties. This is the fourth input argument. [t,y] = solver(odefun,tspan,y0,options) You can create options using the odeset function. See odeset for details. The following table lists the output arguments for the solvers.

T Y

Column vector of time points. Solution array. Each row in Y corresponds to the solution at a time returned in the corresponding row of T.

TE YE IE

The time at which an event occurs. The solution at the time of the event. The index i of the event function that vanishes.

sol Structure to evaluate the solution.

Example 7.1 Obtain the numerical solution of the following first order differential equation: dx/dt = -2x as x(0) =1 , for 0 < t < 10 , using ode45 solver Solution An M file containing MATLAB command is written in edit window function xdot = func1(t,x) xdot= -2.*x save this function as M-file named as func1.m and use the following command in the command window: tspan = [0,10]; yo=1; [t,x]=ode45('func1',tspan,yo); plot(t,x); grid on xlabel('time t') ylabel('x') title('graph between solutions obtained and time')

graph between solutions obtained and time 1 0.9 0.8 0.7 0.6 x 0.5 0.4 0.3 0.2 0.1 0

5 time t

10

7.3 SYMBOLIC MATHEMATICS


The symbolic toolbox is a bit difficult to use but it is of great utility in applications in which symbolic expressions are necessary for reasons of accuracy in calculations. The toolbox simply calls the MAPLE kernel with whatever symbolic expressions you have declared, and then returns a (usually symbolic) expression back to MATLAB. It is important to remember that MAPLE is not a numeric engine, which means that there are certain things it doesn't let you do that MATLAB can do. Rather, it is useful as a supplement to provide functions which MATLAB, as a numerical engine, has difficulty with. The symbolic math toolbox takes some time to initialize, so if nothing happens for a few seconds after you declare your first symbolic variable of the session, it doesn't mean you did anything wrong. The MATLAB student version comes with a copy of the symbolic math toolbox.

The symbolic toolbox is a bit difficult to use but it is of great utility in applications in which symbolic expressions are necessary for reasons of accuracy in calculations. The toolbox simply calls the MAPLE kernel with whatever symbolic expressions you have declared, and then returns a (usually symbolic) expression back to MATLAB. It is important to remember that MAPLE is not a numeric engine, which means that there are certain things it doesn't let you do that MATLAB can do. Rather, it is useful as a supplement to provide functions which MATLAB, as a numerical engine, has difficulty with. The symbolic math toolbox takes some time to initialize, so if nothing happens for a few seconds after you declare your first symbolic variable of the session, it doesn't mean you did anything wrong. The MATLAB student version comes with a copy of the symbolic math toolbox. Symbolic Variables You can declare a single symbolic variable using the 'sym' function as follows. >> a = sym('a1') a = a1 You can create arrays of symbolic expressions like everything else >> a2 = sym('a2'); >> a = [a1, a2] a = [ a1, a2]

Symbolic variables can also be declared many at a time using the 'syms' function. By default, the symbolic variables created have the same names as the arguments of the 'syms' function. The following creates three symbolic variables, a b and c >> syms a b c >> a

a=a

Symbolic Numbers Symbolic numbers allow exact representations of fractions, intended to help avoid rounding errors and representation errors. This section helps explain how to declare them. If you try to add a number into a symbolic array it will automatically turn it into a symbolic number >> syms a1, a2; >> a = [a1, a2]; >> a(3) = 1; %would normally be class 'double' >> class(a(3)) ans = sym Symbolic numbers can also be declared using the syntax a(3) = sym('number'). The difference between symbolic numbers and normal MATLAB numbers is that, if possible, MAPLE will keep the symbolic number as a fraction, which is an exact representation of the answer. For example, to represent the number 0.5 as a fraction, you can use Other class conversions are possible as well; for instance, to change it into a string use the 'char' function. There is no function to directly change a symbolic variable into a function handle, unfortunately. A caveat: Making a symbolic variable of negative exponentials can create problems if you don't use the correct syntax. You cannot do this >> sym('2^-5') ??? Error using ==> sym.sym>char2sym Not a valid symbolic expression.

Instead, you must do this >> sym('2^(-5)') ans = 2^(-5)

Differentiation and Integration with One Variable Differentiation of functions with one or more variables is achieved using the 'diff' function. As usual, you can either define the function before the differentiation (recommended for M files) or you can manually write it in as an argument (recommended for command-line work). If there is only one symbolic variable in the expression, MATLAB assumes that is the variable you are differentiating with respect to. The syntax is simply: >> syms x >> f = x^2 - 3*x + 4; >> diff(f) % or diff('x^2 - 3*x + 4') ans = 2*x - 3

Integration, similarly, is achieved using the 'int' function. Only specifying the function results in an indefinite integral, or the antiderivative of the function. >> int(f) ans = x^3/3 - (3*x^2)/2 + 4*x

Note that if you only specify one output argument (or none at all), the 'int' function omits the integration constant. You just have to know it's there. To do a definite integral on a one-variable function, simply specify the beginning and end points. >> int(f, 0,1) ans = 17/6

EXAMPLE 8.2
Evaluate: 1. a=9/5 2. b=6/5+4/3 using symbolic computation solution: >> a=sym(9)/sym(5)

a= 9/5 >> a=sym(6)/sym(5)+sym(4)/sym(3) a= 38/15

EXAMPLE 8.4
suppose , a=x^n. diffenciate a w.r.t x the commands used are as syms x n; a=x^n; diff(a) result obtained is given below ans =

x^n*n/x

EXAMPLE 8.5
Find the integration of the following 1. a=cos(x)_ 2. a=x2 solution 1.sym x; a=cos(x); int(a) ans = sin(x) 2.syms x a= x^2; int(a) ans = 1/3*x^3

Вам также может понравиться