3294
|
1 @c Copyright (C) 1996, 1997 John W. Eaton |
|
2 @c This is part of the Octave manual. |
|
3 @c For copying conditions, see the file gpl.texi. |
|
4 |
|
5 @node Quadrature, Differential Equations, Nonlinear Equations, Top |
|
6 @chapter Quadrature |
|
7 |
|
8 @menu |
|
9 * Functions of One Variable:: |
|
10 * Orthogonal Collocation:: |
|
11 @end menu |
|
12 |
|
13 @node Functions of One Variable, Orthogonal Collocation, Quadrature, Quadrature |
|
14 @section Functions of One Variable |
|
15 |
3368
|
16 @DOCSTRING(quad) |
3294
|
17 |
3368
|
18 @DOCSTRING(quad_options) |
3294
|
19 |
|
20 Here is an example of using @code{quad} to integrate the function |
|
21 @iftex |
|
22 @tex |
|
23 $$ |
|
24 f(x) = x \sin (1/x) \sqrt {|1 - x|} |
|
25 $$ |
|
26 from $x = 0$ to $x = 3$. |
|
27 @end tex |
|
28 @end iftex |
|
29 @ifinfo |
|
30 |
|
31 @example |
|
32 @var{f}(@var{x}) = @var{x} * sin (1/@var{x}) * sqrt (abs (1 - @var{x})) |
|
33 @end example |
|
34 |
|
35 @noindent |
|
36 from @var{x} = 0 to @var{x} = 3. |
|
37 @end ifinfo |
|
38 |
|
39 This is a fairly difficult integration (plot the function over the range |
|
40 of integration to see why). |
|
41 |
|
42 The first step is to define the function: |
|
43 |
|
44 @example |
|
45 @group |
|
46 function y = f (x) |
|
47 y = x .* sin (1 ./ x) .* sqrt (abs (1 - x)); |
|
48 endfunction |
|
49 @end group |
|
50 @end example |
|
51 |
|
52 Note the use of the `dot' forms of the operators. This is not necessary |
|
53 for the call to @code{quad}, but it makes it much easier to generate a |
|
54 set of points for plotting (because it makes it possible to call the |
|
55 function with a vector argument to produce a vector result). |
|
56 |
|
57 Then we simply call quad: |
|
58 |
|
59 @example |
|
60 @group |
|
61 [v, ier, nfun, err] = quad ("f", 0, 3) |
|
62 @result{} 1.9819 |
|
63 @result{} 1 |
|
64 @result{} 5061 |
|
65 @result{} 1.1522e-07 |
|
66 @end group |
|
67 @end example |
|
68 |
|
69 Although @code{quad} returns a nonzero value for @var{ier}, the result |
|
70 is reasonably accurate (to see why, examine what happens to the result |
|
71 if you move the lower bound to 0.1, then 0.01, then 0.001, etc.). |
|
72 |
|
73 @node Orthogonal Collocation, , Functions of One Variable, Quadrature |
|
74 @section Orthogonal Collocation |
|
75 |
3368
|
76 @DOCSTRING(colloc) |
3294
|
77 |
|
78 Here is an example of using @code{colloc} to generate weight matrices |
|
79 for solving the second order differential equation |
|
80 @iftex |
|
81 @tex |
|
82 $u^\prime - \alpha u^{\prime\prime} = 0$ with the boundary conditions |
|
83 $u(0) = 0$ and $u(1) = 1$. |
|
84 @end tex |
|
85 @end iftex |
|
86 @ifinfo |
|
87 @var{u}' - @var{alpha} * @var{u}'' = 0 with the boundary conditions |
|
88 @var{u}(0) = 0 and @var{u}(1) = 1. |
|
89 @end ifinfo |
|
90 |
|
91 First, we can generate the weight matrices for @var{n} points (including |
|
92 the endpoints of the interval), and incorporate the boundary conditions |
|
93 in the right hand side (for a specific value of |
|
94 @iftex |
|
95 @tex |
|
96 $\alpha$). |
|
97 @end tex |
|
98 @end iftex |
|
99 @ifinfo |
|
100 @var{alpha}). |
|
101 @end ifinfo |
|
102 |
|
103 @example |
|
104 @group |
|
105 n = 7; |
|
106 alpha = 0.1; |
|
107 [r, a, b] = colloc (n-2, "left", "right"); |
|
108 at = a(2:n-1,2:n-1); |
|
109 bt = b(2:n-1,2:n-1); |
|
110 rhs = alpha * b(2:n-1,n) - a(2:n-1,n); |
|
111 @end group |
|
112 @end example |
|
113 |
|
114 Then the solution at the roots @var{r} is |
|
115 |
|
116 @example |
|
117 u = [ 0; (at - alpha * bt) \ rhs; 1] |
|
118 @result{} [ 0.00; 0.004; 0.01 0.00; 0.12; 0.62; 1.00 ] |
|
119 @end example |