annotate scripts/specfun/legendre.m @ 7508:f501b22c0394

doc fixes for legendre
author John W. Eaton <jwe@octave.org>
date Wed, 20 Feb 2008 21:58:11 -0500
parents bc6573d2fa40
children f88b720bf254
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
1 ## Copyright (C) 2000, 2006, 2007 Kai Habel
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
2 ## Copyright (C) 2008 Marco Caliari
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
3 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
4 ## This file is part of Octave.
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
5 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
7 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
9 ## your option) any later version.
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
10 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
14 ## General Public License for more details.
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
15 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
16 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 7001
diff changeset
18 ## <http://www.gnu.org/licenses/>.
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
19
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
20 ## -*- texinfo -*-
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
21 ## @deftypefn {Function File} {@var{l} =} legendre (@var{n}, @var{x})
7508
f501b22c0394 doc fixes for legendre
John W. Eaton <jwe@octave.org>
parents: 7507
diff changeset
22 ## @deftypefnx {Function File} {@var{l} =} legendre (@var{n}, @var{x}, @var{normalization})
f501b22c0394 doc fixes for legendre
John W. Eaton <jwe@octave.org>
parents: 7507
diff changeset
23 ## Compute the Legendre function of degree @var{n} and order @var{m}.
f501b22c0394 doc fixes for legendre
John W. Eaton <jwe@octave.org>
parents: 7507
diff changeset
24 ## The optional argument my be one of @code{"unnorm"}, @code{"sch"}, or
f501b22c0394 doc fixes for legendre
John W. Eaton <jwe@octave.org>
parents: 7507
diff changeset
25 ## @code{"norm"}. The default is @code{"unnorm"}.
f501b22c0394 doc fixes for legendre
John W. Eaton <jwe@octave.org>
parents: 7507
diff changeset
26 ## The value of @var{n} must be a non-negative scalar integer.
f501b22c0394 doc fixes for legendre
John W. Eaton <jwe@octave.org>
parents: 7507
diff changeset
27 ##
f501b22c0394 doc fixes for legendre
John W. Eaton <jwe@octave.org>
parents: 7507
diff changeset
28 ## If the optional argument @var{normalization} is missing or is
f501b22c0394 doc fixes for legendre
John W. Eaton <jwe@octave.org>
parents: 7507
diff changeset
29 ## @code{"unnorm"}, compute the Legendre function of degree @var{n} and
f501b22c0394 doc fixes for legendre
John W. Eaton <jwe@octave.org>
parents: 7507
diff changeset
30 ## order @var{m} and return all values for @var{m} = 0 @dots{} @var{n}.
f501b22c0394 doc fixes for legendre
John W. Eaton <jwe@octave.org>
parents: 7507
diff changeset
31 ## The return value has one dimension more than @var{x}.
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
32 ##
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
33 ## The Legendre Function of degree @var{n} and order @var{m}:
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
34 ##
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
35 ## @example
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
36 ## @group
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
37 ## m m 2 m/2 d^m
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
38 ## P(x) = (-1) * (1-x ) * ---- P (x)
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
39 ## n dx^m n
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
40 ## @end group
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
41 ## @end example
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
42 ##
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
43 ## @noindent
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
44 ## with Legendre polynomial of degree @var{n}:
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
45 ##
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
46 ## @example
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
47 ## @group
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
48 ## 1 d^n 2 n
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
49 ## P (x) = ------ [----(x - 1) ]
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
50 ## n 2^n n! dx^n
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
51 ## @end group
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
52 ## @end example
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
53 ##
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
54 ## @noindent
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
55 ## @code{legendre (3, [-1.0, -0.9, -0.8])} returns the matrix:
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
56 ##
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
57 ## @example
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
58 ## @group
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
59 ## x | -1.0 | -0.9 | -0.8
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
60 ## ------------------------------------
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
61 ## m=0 | -1.00000 | -0.47250 | -0.08000
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
62 ## m=1 | 0.00000 | -1.99420 | -1.98000
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
63 ## m=2 | 0.00000 | -2.56500 | -4.32000
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
64 ## m=3 | 0.00000 | -1.24229 | -3.24000
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
65 ## @end group
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
66 ## @end example
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
67 ##
7508
f501b22c0394 doc fixes for legendre
John W. Eaton <jwe@octave.org>
parents: 7507
diff changeset
68 ## If the optional argument @code{normalization} is @code{"sch"},
f501b22c0394 doc fixes for legendre
John W. Eaton <jwe@octave.org>
parents: 7507
diff changeset
69 ## compute the Schmidt semi-normalized associated Legendre function.
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
70 ## The Schmidt semi-normalized associated Legendre function is related
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
71 ## to the unnormalized Legendre functions by the following:
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
72 ##
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
73 ## For Legendre functions of degree n and order 0:
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
74 ##
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
75 ## @example
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
76 ## @group
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
77 ## 0 0
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
78 ## SP (x) = P (x)
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
79 ## n n
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
80 ## @end group
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
81 ## @end example
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
82 ##
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
83 ## For Legendre functions of degree n and order m:
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
84 ##
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
85 ## @example
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
86 ## @group
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
87 ## m m m 2(n-m)! 0.5
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
88 ## SP (x) = P (x) * (-1) * [-------]
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
89 ## n n (n+m)!
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
90 ## @end group
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
91 ## @end example
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
92 ##
7508
f501b22c0394 doc fixes for legendre
John W. Eaton <jwe@octave.org>
parents: 7507
diff changeset
93 ## If the optional argument @var{normalization} is @code{"norm"},
f501b22c0394 doc fixes for legendre
John W. Eaton <jwe@octave.org>
parents: 7507
diff changeset
94 ## compute the fully normalized associated Legendre function.
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
95 ## The fully normalized associated Legendre function is related
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
96 ## to the unnormalized Legendre functions by the following:
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
97 ##
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
98 ## For Legendre functions of degree @var{n} and order @var{m}
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
99 ##
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
100 ## @example
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
101 ## @group
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
102 ## m m m (n+0.5)(n-m)! 0.5
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
103 ## NP (x) = P (x) * (-1) * [-------------]
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
104 ## n n (n+m)!
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
105 ## @end group
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
106 ## @end example
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
107 ## @end deftypefn
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
108
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
109 ## Author: Marco Caliari <marco.caliari@univr.it>
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
110
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
111 function retval = legendre (n, x, normalization)
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
112
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
113 if (nargin < 2 || nargin > 3)
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
114 print_usage ();
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
115 endif
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
116
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
117 if (nargin == 3)
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
118 normalization = lower (normalization);
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
119 else
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
120 normalization = "unnorm";
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
121 endif
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
122
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
123 if (! isscalar (n) || n < 0 || n != fix (n))
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
124 error ("legendre: n must be a non-negative scalar integer");
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
125 endif
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
126
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
127 if (! isvector (x) || any (x < -1 || x > 1))
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
128 error ("legendre: x must be vector in range -1 <= x <= 1");
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
129 endif
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
130
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
131 switch (normalization)
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
132 case "norm"
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
133 scale = sqrt (n+0.5);
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
134 case "sch"
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
135 scale = sqrt (2);
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
136 case "unnorm"
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
137 scale = 1;
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
138 otherwise
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
139 error ("legendre: expecting normalization option to be \"norm\", \"sch\", or \"unnorm\"");
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
140 endswitch
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
141
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
142 ## Based on the recurrence relation below
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
143 ## m m m
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
144 ## (n-m+1) * P (x) = (2*n+1)*x*P (x) - (n+1)*P (x)
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
145 ## n+1 n n-1
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
146 ## http://en.wikipedia.org/wiki/Associated_Legendre_function
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
147
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
148 for m = 1:n
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
149 lpm1 = scale;
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
150 lpm2 = (2*m-1) .* x .* scale;
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
151 lpm3 = lpm2;
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
152 for k = m+1:n
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
153 lpm3 = ((2*k-1) .* x .* lpm2 - (k+m-2) .* lpm1)/(k-m+1);
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
154 lpm1 = lpm2;
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
155 lpm2 = lpm3;
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
156 endfor
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
157 retval(m,:) = lpm3;
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
158 if (strcmp (normalization, "unnorm"))
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
159 scale = -scale * (2*m-1);
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
160 else
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
161 ## normalization == "sch" or normalization == "norm"
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
162 scale = scale / sqrt ((n-m+1)*(n+m))*(2*m-1);
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
163 endif
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
164 scale = scale .* sqrt(1-x.^2);
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
165 endfor
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
166
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
167 retval(n+1,:) = scale;
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
168
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
169 if (strcmp (normalization, "sch"))
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
170 retval(1,:) = retval(1,:) / sqrt (2);
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
171 endif
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
172
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
173 endfunction
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
174
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
175 %!test
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
176 %! result = legendre (3, [-1.0 -0.9 -0.8]);
5827
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
177 %! expected = [
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
178 %! -1.00000 -0.47250 -0.08000
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
179 %! 0.00000 -1.99420 -1.98000
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
180 %! 0.00000 -2.56500 -4.32000
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
181 %! 0.00000 -1.24229 -3.24000
1fe78adb91bc [project @ 2006-05-22 06:25:14 by jwe]
jwe
parents:
diff changeset
182 %! ];
7507
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
183 %! assert (result, expected, 1e-5);
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
184
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
185 %!test
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
186 %! result = legendre (3, [-1.0 -0.9 -0.8], "sch");
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
187 %! expected = [
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
188 %! -1.00000 -0.47250 -0.08000
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
189 %! 0.00000 0.81413 0.80833
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
190 %! -0.00000 -0.33114 -0.55771
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
191 %! 0.00000 0.06547 0.17076
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
192 %! ];
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
193 %! assert (result, expected, 1e-5);
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
194
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
195 %!test
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
196 %! result = legendre (3, [-1.0 -0.9 -0.8], "norm");
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
197 %! expected = [
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
198 %! -1.87083 -0.88397 -0.14967
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
199 %! 0.00000 1.07699 1.06932
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
200 %! -0.00000 -0.43806 -0.73778
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
201 %! 0.00000 0.08661 0.22590
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
202 %! ];
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
203 %! assert (result, expected, 1e-5);
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
204
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
205 %!test
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
206 %! result = legendre (151, 0);
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
207 %! ## Don't compare to "-Inf" since it would fail on 64 bit systems.
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
208 %! assert (result(end) < -1.7976e308 && all (isfinite (result(1:end-1))));
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
209
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
210 %!test
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
211 %! result = legendre (150, 0);
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
212 %! ## This agrees with Matlab's result.
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
213 %! assert (result(end), 3.7532741115719e+306, 0.0000000000001e+306)
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
214
bc6573d2fa40 legendre.m: Added normalization and improved stability.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
215