3266
|
1 ## Copyright (C) 1995, 1996 Kurt Hornik |
2537
|
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 |
3266
|
25 function [y, iterations] = erfinv (x) |
2537
|
26 |
|
27 if (nargin != 1) |
|
28 usage ("erfinv (x)"); |
|
29 endif |
3266
|
30 |
|
31 maxit = 100; |
|
32 tol = eps; |
|
33 |
|
34 iterations = 0; |
|
35 |
2537
|
36 [m, n] = size (x); |
|
37 x = reshape (x, m * n, 1); |
|
38 y = zeros (m * n, 1); |
3266
|
39 |
2537
|
40 i = find ((x < -1) | (x > 1)); |
|
41 if any (i) |
|
42 y(i) = NaN * ones (length (i), 1); |
|
43 endif |
|
44 |
2621
|
45 t = find (x == -1); |
2663
|
46 y (t) = (-Inf) * ones (size (t)); |
2621
|
47 |
|
48 t = find (x == 1); |
|
49 y (t) = Inf * ones (size (t)); |
3266
|
50 |
2537
|
51 i = find ((x > -1) & (x < 1)); |
|
52 if any (i) |
2968
|
53 s = sqrt (pi) / 2; |
2537
|
54 z_old = ones (length (i), 1); |
3266
|
55 z_new = sqrt (-log (1 - abs (x(i)))) .* sign (x(i)); |
|
56 while (any (abs (erf (z_old) - x(i)) > tol * abs (x(i)))) |
2537
|
57 z_old = z_new; |
2813
|
58 z_new = z_old - (erf (z_old) - x(i)) .* exp (z_old.^2) * s; |
3266
|
59 if (++iterations > maxit) |
|
60 warning ("erfinv: iteration limit exceeded"); |
|
61 break; |
|
62 endif |
2537
|
63 endwhile |
|
64 y(i) = z_new; |
|
65 endif |
|
66 |
|
67 y = reshape (y, m, n); |
|
68 |
|
69 endfunction |