Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > b38d2da330d1936e5ab1307c039c4941 > files > 274

octave-doc-3.6.4-3.mga4.noarch.rpm

<html lang="en">
<head>
<title>Functions of Multiple Variables - GNU Octave</title>
<meta http-equiv="Content-Type" content="text/html">
<meta name="description" content="GNU Octave">
<meta name="generator" content="makeinfo 4.13">
<link title="Top" rel="start" href="index.html#Top">
<link rel="up" href="Numerical-Integration.html#Numerical-Integration" title="Numerical Integration">
<link rel="prev" href="Orthogonal-Collocation.html#Orthogonal-Collocation" title="Orthogonal Collocation">
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
<meta http-equiv="Content-Style-Type" content="text/css">
<style type="text/css"><!--
  pre.display { font-family:inherit }
  pre.format  { font-family:inherit }
  pre.smalldisplay { font-family:inherit; font-size:smaller }
  pre.smallformat  { font-family:inherit; font-size:smaller }
  pre.smallexample { font-size:smaller }
  pre.smalllisp    { font-size:smaller }
  span.sc    { font-variant:small-caps }
  span.roman { font-family:serif; font-weight:normal; } 
  span.sansserif { font-family:sans-serif; font-weight:normal; } 
--></style>
</head>
<body>
<div class="node">
<a name="Functions-of-Multiple-Variables"></a>
<p>
Previous:&nbsp;<a rel="previous" accesskey="p" href="Orthogonal-Collocation.html#Orthogonal-Collocation">Orthogonal Collocation</a>,
Up:&nbsp;<a rel="up" accesskey="u" href="Numerical-Integration.html#Numerical-Integration">Numerical Integration</a>
<hr>
</div>

<h3 class="section">23.3 Functions of Multiple Variables</h3>

<p>Octave does not have built-in functions for computing the integral of
functions of multiple variables directly.  It is possible, however, to
compute the integral of a function of multiple variables using the
existing functions for one-dimensional integrals.

   <p>To illustrate how the integration can be performed, we will integrate
the function

<pre class="example">     f(x, y) = sin(pi*x*y)*sqrt(x*y)
</pre>
   <p>for x and y between 0 and 1.

   <p>The first approach creates a function that integrates f with
respect to x, and then integrates that function with respect to
y.  Because <code>quad</code> is written in Fortran it cannot be called
recursively.  This means that <code>quad</code> cannot integrate a function
that calls <code>quad</code>, and hence cannot be used to perform the double
integration.  Any of the other integrators, however, can be used which is
what the following code demonstrates.

<pre class="example">     function q = g(y)
       q = ones (size (y));
       for i = 1:length (y)
         f = @(x) sin (pi*x.*y(i)) .* sqrt (x.*y(i));
         q(i) = quadgk (f, 0, 1);
       endfor
     endfunction
     
     I = quadgk ("g", 0, 1)
           &rArr; 0.30022
</pre>
   <p>The above process can be simplified with the <code>dblquad</code> and
<code>triplequad</code> functions for integrals over two and three
variables.  For example:

<pre class="example">     I = dblquad (@(x, y) sin (pi*x.*y) .* sqrt (x.*y), 0, 1, 0, 1)
           &rArr; 0.30022
</pre>
   <!-- dblquad scripts/general/dblquad.m -->
   <p><a name="doc_002ddblquad"></a>

<div class="defun">
&mdash; Function File:  <b>dblquad</b> (<var>f, xa, xb, ya, yb</var>)<var><a name="index-dblquad-2335"></a></var><br>
&mdash; Function File:  <b>dblquad</b> (<var>f, xa, xb, ya, yb, tol</var>)<var><a name="index-dblquad-2336"></a></var><br>
&mdash; Function File:  <b>dblquad</b> (<var>f, xa, xb, ya, yb, tol, quadf</var>)<var><a name="index-dblquad-2337"></a></var><br>
&mdash; Function File:  <b>dblquad</b> (<var>f, xa, xb, ya, yb, tol, quadf, <small class="dots">...</small></var>)<var><a name="index-dblquad-2338"></a></var><br>
<blockquote><p>Numerically evaluate the double integral of <var>f</var>. 
<var>f</var> is a function handle, inline function, or string
containing the name of the function to evaluate.  The function <var>f</var> must
have the form z = f(x,y) where <var>x</var> is a vector and <var>y</var> is a
scalar.  It should return a vector of the same length and orientation as
<var>x</var>.

        <p><var>xa</var>, <var>ya</var> and <var>xb</var>, <var>yb</var> are the lower and upper limits of
integration for x and y respectively.  The underlying integrator determines
whether infinite bounds are accepted.

        <p>The optional argument <var>tol</var> defines the absolute tolerance used to
integrate each sub-integral.  The default value is 1e^-6.

        <p>The optional argument <var>quadf</var> specifies which underlying integrator
function to use.  Any choice but <code>quad</code> is available and the default
is <code>quadcc</code>.

        <p>Additional arguments, are passed directly to <var>f</var>.  To use the default
value for <var>tol</var> or <var>quadf</var> one may pass ':' or an empty matrix ([]). 
<!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
<!-- A simple blank line produces the correct behavior. -->
<!-- @sp 1 -->

     <p class="noindent"><strong>See also:</strong> <a href="doc_002dtriplequad.html#doc_002dtriplequad">triplequad</a>, <a href="doc_002dquad.html#doc_002dquad">quad</a>, <a href="doc_002dquadv.html#doc_002dquadv">quadv</a>, <a href="doc_002dquadl.html#doc_002dquadl">quadl</a>, <a href="doc_002dquadgk.html#doc_002dquadgk">quadgk</a>, <a href="doc_002dquadcc.html#doc_002dquadcc">quadcc</a>, <a href="doc_002dtrapz.html#doc_002dtrapz">trapz</a>. 
</p></blockquote></div>

<!-- triplequad scripts/general/triplequad.m -->
   <p><a name="doc_002dtriplequad"></a>

<div class="defun">
&mdash; Function File:  <b>triplequad</b> (<var>f, xa, xb, ya, yb, za, zb</var>)<var><a name="index-triplequad-2339"></a></var><br>
&mdash; Function File:  <b>triplequad</b> (<var>f, xa, xb, ya, yb, za, zb, tol</var>)<var><a name="index-triplequad-2340"></a></var><br>
&mdash; Function File:  <b>triplequad</b> (<var>f, xa, xb, ya, yb, za, zb, tol, quadf</var>)<var><a name="index-triplequad-2341"></a></var><br>
&mdash; Function File:  <b>triplequad</b> (<var>f, xa, xb, ya, yb, za, zb, tol, quadf, <small class="dots">...</small></var>)<var><a name="index-triplequad-2342"></a></var><br>
<blockquote><p>Numerically evaluate the triple integral of <var>f</var>. 
<var>f</var> is a function handle, inline function, or string
containing the name of the function to evaluate.  The function <var>f</var> must
have the form w = f(x,y,z) where either <var>x</var> or <var>y</var> is a
vector and the remaining inputs are scalars.  It should return a vector of
the same length and orientation as <var>x</var> or <var>y</var>.

        <p><var>xa</var>, <var>ya</var>, <var>za</var> and <var>xb</var>, <var>yb</var>, <var>zb</var> are the lower
and upper limits of integration for x, y, and z respectively.  The
underlying integrator determines whether infinite bounds are accepted.

        <p>The optional argument <var>tol</var> defines the absolute tolerance used to
integrate each sub-integral.  The default value is 1e^-6.

        <p>The optional argument <var>quadf</var> specifies which underlying integrator
function to use.  Any choice but <code>quad</code> is available and the default
is <code>quadcc</code>.

        <p>Additional arguments, are passed directly to <var>f</var>.  To use the default
value for <var>tol</var> or <var>quadf</var> one may pass ':' or an empty matrix ([]). 
<!-- Texinfo @sp should work but in practice produces ugly results for HTML. -->
<!-- A simple blank line produces the correct behavior. -->
<!-- @sp 1 -->

     <p class="noindent"><strong>See also:</strong> <a href="doc_002ddblquad.html#doc_002ddblquad">dblquad</a>, <a href="doc_002dquad.html#doc_002dquad">quad</a>, <a href="doc_002dquadv.html#doc_002dquadv">quadv</a>, <a href="doc_002dquadl.html#doc_002dquadl">quadl</a>, <a href="doc_002dquadgk.html#doc_002dquadgk">quadgk</a>, <a href="doc_002dquadcc.html#doc_002dquadcc">quadcc</a>, <a href="doc_002dtrapz.html#doc_002dtrapz">trapz</a>. 
</p></blockquote></div>

   <p>The above mentioned approach works, but is fairly slow, and that problem
increases exponentially with the dimensionality of the integral.  Another
possible solution is to use Orthogonal Collocation as described in the
previous section (see <a href="Orthogonal-Collocation.html#Orthogonal-Collocation">Orthogonal Collocation</a>).  The integral of a function
f(x,y) for x and y between 0 and 1 can be approximated
using n points by
the sum over <code>i=1:n</code> and <code>j=1:n</code> of <code>q(i)*q(j)*f(r(i),r(j))</code>,
where q and r is as returned by <code>colloc(n)</code>.  The
generalization to more than two variables is straight forward.  The
following code computes the studied integral using n=8 points.

<pre class="example">     f = @(x,y) sin (pi*x*y') .* sqrt (x*y');
     n = 8;
     [t, ~, ~, q] = colloc (n);
     I = q'*f(t,t)*q;
           &rArr; 0.30022
</pre>
   <p class="noindent">It should be noted that the number of points determines the quality
of the approximation.  If the integration needs to be performed between
a and b, instead of 0 and 1, then a change of variables is needed.

<!-- DO NOT EDIT!  Generated automatically by munge-texi.pl. -->
<!-- Copyright (C) 1996-2012 John W. Eaton -->
<!-- This file is part of Octave. -->
<!-- Octave is free software; you can redistribute it and/or modify it -->
<!-- under the terms of the GNU General Public License as published by the -->
<!-- Free Software Foundation; either version 3 of the License, or (at -->
<!-- your option) any later version. -->
<!-- Octave is distributed in the hope that it will be useful, but WITHOUT -->
<!-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -->
<!-- FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License -->
<!-- for more details. -->
<!-- You should have received a copy of the GNU General Public License -->
<!-- along with Octave; see the file COPYING.  If not, see -->
<!-- <http://www.gnu.org/licenses/>. -->
   </body></html>