2537
|
1 ## Copyright (C) 1995, 1996 Kurt Hornik |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2, or (at your option) |
|
6 ## any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, but |
|
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
11 ## General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this file. If not, write to the Free Software Foundation, |
|
15 ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
16 |
|
17 ## usage: erfinv (x) |
|
18 ## |
|
19 ## Computes the inverse of the error function erf. |
|
20 |
|
21 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
22 ## Created: 27 September 1994 |
|
23 ## Adapted-By: jwe |
|
24 |
|
25 function y = erfinv (x) |
|
26 |
|
27 if (nargin != 1) |
|
28 usage ("erfinv (x)"); |
|
29 endif |
|
30 |
|
31 [m, n] = size (x); |
|
32 x = reshape (x, m * n, 1); |
|
33 y = zeros (m * n, 1); |
|
34 |
|
35 i = find ((x < -1) | (x > 1)); |
|
36 if any (i) |
|
37 y(i) = NaN * ones (length (i), 1); |
|
38 endif |
|
39 |
2621
|
40 t = find (x == -1); |
2663
|
41 y (t) = (-Inf) * ones (size (t)); |
2621
|
42 |
|
43 t = find (x == 1); |
|
44 y (t) = Inf * ones (size (t)); |
2537
|
45 |
|
46 i = find ((x > -1) & (x < 1)); |
|
47 if any (i) |
|
48 z_old = ones (length (i), 1); |
|
49 z_new = zeros (length (i), 1); |
|
50 while (any (any (abs (z_new - z_old) > eps))) |
|
51 z_old = z_new; |
|
52 z_new = z_old - (erf (z_old) - x(i)) .* exp (z_old.^2); |
|
53 endwhile |
|
54 y(i) = z_new; |
|
55 endif |
|
56 |
|
57 y = reshape (y, m, n); |
|
58 |
|
59 endfunction |