annotate doc/interpreter/quad.txi @ 7948:af10baa63915 ss-3-1-50

3.1.50 snapshot
author John W. Eaton <jwe@octave.org>
date Fri, 18 Jul 2008 17:42:48 -0400
parents fd42779a8428
children bbaa5d7d0143
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7018
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
1 @c Copyright (C) 1996, 1997, 1999, 2002, 2007 John W. Eaton
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
2 @c
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
3 @c This file is part of Octave.
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
4 @c
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
5 @c Octave is free software; you can redistribute it and/or modify it
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
6 @c under the terms of the GNU General Public License as published by the
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
7 @c Free Software Foundation; either version 3 of the License, or (at
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
8 @c your option) any later version.
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
9 @c
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
10 @c Octave is distributed in the hope that it will be useful, but WITHOUT
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
11 @c ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
12 @c FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
13 @c for more details.
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
14 @c
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
15 @c You should have received a copy of the GNU General Public License
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
16 @c along with Octave; see the file COPYING. If not, see
fd42779a8428 [project @ 2007-10-13 00:52:12 by jwe]
jwe
parents: 6939
diff changeset
17 @c <http://www.gnu.org/licenses/>.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
18
6741
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
19 @node Numerical Integration
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
20 @chapter Numerical Integration
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
21
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
22 Octave comes with several built-in functions for computing the integral
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
23 of a function numerically. These functions all solve 1-dimensional
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
24 integration problems.
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
25
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
26 @menu
6741
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
27 * Functions of One Variable::
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
28 * Functions of Multiple Variables::
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
29 * Orthogonal Collocation::
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
30 @end menu
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
31
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3368
diff changeset
32 @node Functions of One Variable
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
33 @section Functions of One Variable
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
34
6741
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
35 Octave supports three different algorithms for computing the integral
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
36 @iftex
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
37 @tex
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
38 $$
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
39 \int_a^b f(x) d x
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
40 $$
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
41 @end tex
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
42 @end iftex
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
43 of a function @math{f} over the interval from @math{a} to @math{b}.
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
44 These are
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
45
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
46 @table @code
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
47 @item quad
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
48 Numerical integration based on Gaussian quadrature.
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
49
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
50 @item quadl
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
51 Numerical integration using an adaptive Lobatto rule.
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
52
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
53 @item trapz
6939
46d1ad37d943 [project @ 2007-10-01 16:12:20 by jwe]
jwe
parents: 6778
diff changeset
54 Numerical integration using the trapezoidal method.
6741
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
55 @end table
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
56
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
57 @noindent
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
58 Besides these functions Octave also allows you to perform cumulative
6939
46d1ad37d943 [project @ 2007-10-01 16:12:20 by jwe]
jwe
parents: 6778
diff changeset
59 numerical integration using the trapezoidal method through the
6741
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
60 @code{cumtrapz} function.
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
61
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3294
diff changeset
62 @DOCSTRING(quad)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
63
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3294
diff changeset
64 @DOCSTRING(quad_options)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
65
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
66 Here is an example of using @code{quad} to integrate the function
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
67 @iftex
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
68 @tex
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
69 $$
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
70 f(x) = x \sin (1/x) \sqrt {|1 - x|}
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
71 $$
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
72 from $x = 0$ to $x = 3$.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
73 @end tex
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
74 @end iftex
6741
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
75 @ifnottex
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
76
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
77 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
78 @var{f}(@var{x}) = @var{x} * sin (1/@var{x}) * sqrt (abs (1 - @var{x}))
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
79 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
80
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
81 @noindent
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
82 from @var{x} = 0 to @var{x} = 3.
6741
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
83 @end ifnottex
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
84
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
85 This is a fairly difficult integration (plot the function over the range
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
86 of integration to see why).
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
87
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
88 The first step is to define the function:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
89
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
90 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
91 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
92 function y = f (x)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
93 y = x .* sin (1 ./ x) .* sqrt (abs (1 - x));
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
94 endfunction
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
95 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
96 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
97
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
98 Note the use of the `dot' forms of the operators. This is not necessary
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
99 for the call to @code{quad}, but it makes it much easier to generate a
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
100 set of points for plotting (because it makes it possible to call the
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
101 function with a vector argument to produce a vector result).
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
102
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
103 Then we simply call quad:
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
104
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
105 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
106 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
107 [v, ier, nfun, err] = quad ("f", 0, 3)
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
108 @result{} 1.9819
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
109 @result{} 1
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
110 @result{} 5061
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
111 @result{} 1.1522e-07
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
112 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
113 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
114
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
115 Although @code{quad} returns a nonzero value for @var{ier}, the result
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
116 is reasonably accurate (to see why, examine what happens to the result
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
117 if you move the lower bound to 0.1, then 0.01, then 0.001, etc.).
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
118
6502
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 4167
diff changeset
119 @DOCSTRING(quadl)
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 4167
diff changeset
120
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 4167
diff changeset
121 @DOCSTRING(trapz)
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 4167
diff changeset
122
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 4167
diff changeset
123 @DOCSTRING(cumtrapz)
6ab0a8767780 [project @ 2007-04-06 03:32:06 by jwe]
jwe
parents: 4167
diff changeset
124
4167
aae05d51353c [project @ 2002-11-12 02:52:50 by jwe]
jwe
parents: 3368
diff changeset
125 @node Orthogonal Collocation
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
126 @section Orthogonal Collocation
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
127
3368
a4cd1e9d9962 [project @ 1999-11-20 17:22:48 by jwe]
jwe
parents: 3294
diff changeset
128 @DOCSTRING(colloc)
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
129
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
130 Here is an example of using @code{colloc} to generate weight matrices
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
131 for solving the second order differential equation
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
132 @iftex
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
133 @tex
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
134 $u^\prime - \alpha u^{\prime\prime} = 0$ with the boundary conditions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
135 $u(0) = 0$ and $u(1) = 1$.
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
136 @end tex
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
137 @end iftex
6741
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
138 @ifnottex
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
139 @var{u}' - @var{alpha} * @var{u}'' = 0 with the boundary conditions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
140 @var{u}(0) = 0 and @var{u}(1) = 1.
6741
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
141 @end ifnottex
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
142
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
143 First, we can generate the weight matrices for @var{n} points (including
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
144 the endpoints of the interval), and incorporate the boundary conditions
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
145 in the right hand side (for a specific value of
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
146 @iftex
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
147 @tex
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
148 $\alpha$).
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
149 @end tex
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
150 @end iftex
6741
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
151 @ifnottex
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
152 @var{alpha}).
6741
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
153 @end ifnottex
3294
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
154
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
155 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
156 @group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
157 n = 7;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
158 alpha = 0.1;
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
159 [r, a, b] = colloc (n-2, "left", "right");
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
160 at = a(2:n-1,2:n-1);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
161 bt = b(2:n-1,2:n-1);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
162 rhs = alpha * b(2:n-1,n) - a(2:n-1,n);
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
163 @end group
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
164 @end example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
165
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
166 Then the solution at the roots @var{r} is
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
167
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
168 @example
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
169 u = [ 0; (at - alpha * bt) \ rhs; 1]
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
170 @result{} [ 0.00; 0.004; 0.01 0.00; 0.12; 0.62; 1.00 ]
bfe1573bd2ae [project @ 1999-10-19 10:06:07 by jwe]
jwe
parents:
diff changeset
171 @end example
6741
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
172
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
173 @node Functions of Multiple Variables
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
174 @section Functions of Multiple Variables
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
175
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
176 Octave does not have built-in functions for computing the integral
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
177 of functions of multiple variables. It is however possible to compute
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
178 the integral of a function of multiple variables using the functions
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
179 for one-dimensional integrals.
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
180
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
181 To illustrate how the integration can be performed, we will integrate
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
182 the function
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
183 @iftex
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
184 @tex
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
185 $$
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
186 f(x, y) = \sin(\pi x y)\sqrt{x y}
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
187 $$
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
188 @end tex
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
189 @end iftex
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
190 @ifnottex
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
191 @example
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
192 f(x, y) = sin(pi*x*y)*sqrt(x*y)
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
193 @end example
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
194 @end ifnottex
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
195 for @math{x} and @math{y} between 0 and 1.
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
196
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
197 The first approach creates a function that integrates @math{f} with
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
198 respect to @math{x}, and then integrates that function with respect to
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
199 @math{y}. Since @code{quad} is written in Fortran it cannot be called
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
200 recursively. This means that @code{quad} cannot integrate a function
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
201 that calls @code{quad}, and hence cannot be used to perform the double
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
202 integration. It is however possible with @code{quadl}, which is what
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
203 the following code does.
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
204
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
205 @example
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
206 function I = g(y)
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
207 I = ones(1, length(y));
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
208 for i = 1:length(y)
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
209 f = @@(x) sin(pi.*x.*y(i)).*sqrt(x.*y(i));
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
210 I(i) = quadl(f, 0, 1);
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
211 endfor
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
212 endfunction
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
213
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
214 I = quadl("g", 0, 1)
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
215 @result{} 0.30022
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
216 @end example
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
217
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
218 The above mentioned approach works but is fairly slow, and that problem
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
219 increases exponentially with the dimensionality the problem. Another
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
220 possible solution is to use Orthogonal Collocation as described in the
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
221 previous section. The integral of a function @math{f(x,y)} for
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
222 @math{x} and @math{y} between 0 and 1 can be approximated using @math{n}
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
223 points by
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
224 @iftex
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
225 @tex
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
226 $$
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
227 \int_0^1 \int_0^1 f(x,y) d x d y \approx \sum_{i=1}^n \sum_{j=1}^n q_i q_j f(r_i, r_j),
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
228 $$
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
229 @end tex
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
230 @end iftex
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
231 @ifnottex
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
232 the sum over @code{i=1:n} and @code{j=1:n} of @code{q(i)*q(j)*f(r(i),r(j))},
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
233 @end ifnottex
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
234 where @math{q} and @math{r} is as returned by @code{colloc(n)}. The
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
235 generalisation to more than two variables is straight forward. The
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
236 following code computes the studied integral using @math{n=7} points.
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
237
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
238 @example
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
239 f = @@(x,y) sin(pi*x*y').*sqrt(x*y');
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
240 n = 7;
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
241 [t, A, B, q] = colloc(n);
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
242 I = q'*f(t,t)*q;
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
243 @result{} 0.30022
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
244 @end example
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
245
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
246 @noindent
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
247 It should be noted that the number of points determines the quality
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
248 of the approximation. If the integration needs to be performed between
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
249 @math{a} and @math{b} instead of 0 and 1, a change of variables is needed.
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
250
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
251
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
252
00116015904d [project @ 2007-06-18 16:07:14 by jwe]
jwe
parents: 6502
diff changeset
253