# What types of integration can MATLAB perform?

17 views (last 30 days)

Show older comments

MathWorks Support Team
on 10 Sep 2012

Edited: MathWorks Support Team
on 17 Nov 2021

I would like to know if I can solve ordinary differential equations in MATLAB.

I also would like to know if there are methods included in the ODE Suite for Green's Theorem, Stoke's Theorem, and/or the Gauss Divergence Theorem.

I would also like to know about symbolic and numerical integration, boundary value problems, and differential algebraic equations or equations written in semi-explicit form.

### Accepted Answer

MathWorks Support Team
on 12 Oct 2021

Edited: MathWorks Support Team
on 17 Nov 2021

You can do the following types of integration with MATLAB using our pre-written integration routines:

Ordinary Differential Equations (ODEs)

------------------------------------------------------------------------

Method(s): ODE45, ODE23, ODE113, ODE15S, ODE23S, ODE23T, ODE23TB

Required Toolboxes: MATLAB only

Example given below: 1

Symbolic Differential Equations

------------------------------------------------------------------------

Method(s): DSOLVE

Required Toolboxes: Symbolic Math

Example given below: 2

Differential Algebraic Equations of Index 1 (DAEs)

------------------------------------------------------------------------

Method(s): ODE15s

Required Toolboxes: MATLAB only

Boundary Value Problems (BVPs)

------------------------------------------------------------------------

Method(s): ODE Solvers, FMINS, functions in the Optimization Toolbox, BVP4C (as of MATLAB 6)

Required Toolboxes: MATLAB only or Optimization Toolbox

Numerically Computing Integral (integrand defined by function file)

------------------------------------------------------------------------

Method(s): INTEGRAL, INTEGRAL2

Required Toolboxes: MATLAB only

Example given below: 3

Numerically Computing Integral (integrand defined by numerical data)

------------------------------------------------------------------------

Method(s): TRAPZ, CUMTRAPZ

Required Toolboxes: MATLAB only

Example given below: 4

Symbolic Integrals (definite and indefinite)

------------------------------------------------------------------------

Method(s): INT

Required Toolboxes: Symbolic Math

Example given below: 5

Delay Differential Equations

------------------------------------------------------------------------

Method(s): DDE23

Required Toolboxes: MATLAB only

Example given below: 6

Following is some additional information on all the methods. At the end of this document examples are shown. If you simply want to reduce the order of your ODE, see the Related Solution listed at the bottom of the page.

************************************************************************

A. For solving ODEs:

In MATLAB 5 we introduced solvers for initial value problems of ordinary differential equations. Hence, any problem that can be expressed as an IVP can be done. In general, MATLAB can solve any problem that can be written in the following format:

dy/dt = F(t,y) with the y(t0) vector specified

If you have a line integral: if x = f(t), y = g(t), z = h(t), for a curve w

tb

/ /

| w ds = | w(f(t),g(t),h(t)) * sqrt(f'(t)^2 + g'(t)^2 + h'(t)^2) dt

/ /

C ta

then the function to integrate is:

F(t,Y) = w(f(t),g(t),h(t)) * sqrt(f'(t)^2 + g'(t)^2 + h'(t)^2)

on the interval [ta tb]. Note that F does not depend on Y.

However, Surface Integrals, Triple Integration, Green's Theorem, Stoke's Theorem, and the Gauss Divergence Theorem all involve integration across multiple dimensions. Therefore the ODE Suite does not support these integration techniques. One reason that these techniques cannot be expressed in the format: dy/dt=F(t,y), is that they all involve d/dx's and d/dy's. Ordinary differential equations involve derivatives with respect to only one variable.

For more information on using the ODE Suite, please refer to the following link:

https://www.mathworks.com/help/matlab/math/choose-an-ode-solver.html

Using the DOC command (ex, 'doc ode45') will allow you to get several examples as well as additional documentation.

B. For solving a differential equation symbolically:

The DSOLVE function can be used for this. This function is included in the Symbolic Toolbox.

C. For solving differential algebraic equations (DAEs):

Equations written as f(x',x,t) = 0 are differential algebraic equations (DAE). There are no methods known to solve these problems in general; only some special cases can be solved (i.e. index less than 2). You may want to look at "Numerical Solution of Initial-Value Problems in Differential-Algebraic Equations" by K.E. Brenan, S.L. Campbell, L.R. Petzold for more information on this.

The ODE15S, which was added in MATLAB 5.3, can be used to solve differential algebraic equations of index 1. The attached paper describes this in more detail.

D. For solving boundary value problems (BVPs):

A Boundary Value Problem is similiar to an Initial Value Problem in dealing with Ordinary Differential Equations. The difference is that instead of knowing n initial conditions, you might know n conditions at various points in time.

For example, with a second order Initial Value Problem, you need to know the initial position and initial velocity, but with a Boundary Value Problem, you might know initial position and final position, or initial velocity and final position, etc.

Since all Boundary Value Problems are different, we do not have any routines that solve a general example.

It may be possible to solve a Boundary Value Problem by writing your own function, using a combination of ODE solvers and optimization tools like FMINS, or the functions in the Optimization Toolbox, but we don't have any examples on how to do this. You may want to look at references for the "shooting method," a technique used for solving these types of problems.

As of MATLAB 6.0 ( R12) we have the BVP4C for solving Boundary Value Problems. For more information about using BVP4C, including information on how to solve BVPs with periodic boundary conditions, see the attached paper, bvp_paper.pdf.

E. For numerically computing integrals whose integrand is defined by a function:

Use the INTEGRAL and INTEGRAL2 functions.

F. For numerically computing integrals whose integrand is defined by numerical data:

The TRAPZ and CUMTRAPZ functions perform a trapezoidal integration over a given data set.

G. For solving symbolic integrals (definite and indefinite) using the Symbolic Toolbox:

The INT command in the Symbolic Math Toolbox can be used for this.

H. For solving delay differential equations

The DDE23 command solves a differential equation where the derivative of the function y at a certain time depends on the value of the function y at earlier times. For example, the following equation is a delay differential equation:

y'(t) = sin(3t)*y(t) + y(t-1)

y(t) = 1 when t <= 0

Here are some examples of how these functions can be used:

%________________________________________________________________________________

% 1. Solving a differential equation using the ODE Suite

%--------------------------------------------------------------------------------

f = @(t,x) -3*x;

% More complicated differential equations

% should be written as function files instead of

% inline functions

Timespan=[0 2];

InitialCondition=10;

[t y] = ode45(f,Timespan,InitialCondition);

plot(t,y)

% ode45(f,Timespan,InitialCondition) by itself will

% plot the results.

Note that if you are using a version of MATLAB older than MATLAB 7.0 (R14), you will need to use a function file or an inline function for “f”. To use an inline function, try the following:

f = inline('-3*x','t','x');

%________________________________________________________________________________

% 2. Solving a differential equation using the Symbolic Toolbox

%--------------------------------------------------------------------------------

S = dsolve('Dx=-3*x','x(0)=10')

ezplot(S,[0 2])

%________________________________________________________________________________

% 3. Numerically computing an integrals - integrand is defined by a function

%--------------------------------------------------------------------------------

% We'll compute the integral of SIN(X) over 0 to pi

integral(@sin,0,pi)

%________________________________________________________________________________

% 4. Numerically computing an integral - integrand is defined by data

%--------------------------------------------------------------------------------

x = 0:.1:pi;

y = sin(x);

% Integrate y with respect to x

trapz(x,y)

% The "running integral" can be computed with cumtrapz(x,y)

%________________________________________________________________________________

% 5. Symbolic integration

%--------------------------------------------------------------------------------

syms x

f = sin(x);

Indefinite = int(f)

Definite = int(f,0,pi)

%________________________________________________________________________________

% 6. Delay differential equation

%--------------------------------------------------------------------------------

ddefun = @(t,y,z) sin(3*t).*y+z(:,1);

lags = 1;

history = 1;

tspan = [0 10];

sol = dde23(ddefun, lags, history, tspan);

plot(sol.x, sol.y,'r-')

% If you are not using MATLAB 7.0 (R14) or later,

% use the following inline function to define “ddefun”:

% ddefun = inline('sin(3*t).*y+z(:,1)','t','y','z');

##### 1 Comment

Walter Roberson
on 9 Oct 2015

### More Answers (0)

### See Also

See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!