3266
|
1 ## Copyright (C) 1995, 1996 Kurt Hornik |
3426
|
2 ## |
3922
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
2537
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
13 ## General Public License for more details. |
|
14 ## |
2537
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
2537
|
18 |
3321
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Mapping Function} {} erfinv (@var{z}) |
3599
|
21 ## Computes the inverse of the error function. |
5642
|
22 ## @seealso{erf, erfc} |
3321
|
23 ## @end deftypefn |
2537
|
24 |
5428
|
25 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
2537
|
26 ## Created: 27 September 1994 |
|
27 ## Adapted-By: jwe |
|
28 |
3266
|
29 function [y, iterations] = erfinv (x) |
3426
|
30 |
2537
|
31 if (nargin != 1) |
6046
|
32 print_usage (); |
2537
|
33 endif |
3266
|
34 |
|
35 maxit = 100; |
|
36 tol = eps; |
|
37 |
|
38 iterations = 0; |
|
39 |
4844
|
40 sz = size (x); |
|
41 nel = numel (x); |
|
42 |
|
43 x = reshape (x, nel, 1); |
|
44 y = zeros (nel, 1); |
3266
|
45 |
4077
|
46 i = find ((x < -1) | (x > 1) | isnan(x)); |
2537
|
47 if any (i) |
|
48 y(i) = NaN * ones (length (i), 1); |
|
49 endif |
|
50 |
2621
|
51 t = find (x == -1); |
2663
|
52 y (t) = (-Inf) * ones (size (t)); |
2621
|
53 |
|
54 t = find (x == 1); |
|
55 y (t) = Inf * ones (size (t)); |
3266
|
56 |
2537
|
57 i = find ((x > -1) & (x < 1)); |
|
58 if any (i) |
2968
|
59 s = sqrt (pi) / 2; |
2537
|
60 z_old = ones (length (i), 1); |
3266
|
61 z_new = sqrt (-log (1 - abs (x(i)))) .* sign (x(i)); |
3599
|
62 while (any (abs (erf (z_new) - x(i)) > tol * abs (x(i)))) |
2537
|
63 z_old = z_new; |
2813
|
64 z_new = z_old - (erf (z_old) - x(i)) .* exp (z_old.^2) * s; |
3266
|
65 if (++iterations > maxit) |
3426
|
66 warning ("erfinv: iteration limit exceeded"); |
|
67 break; |
3266
|
68 endif |
2537
|
69 endwhile |
|
70 y(i) = z_new; |
|
71 endif |
3426
|
72 |
4844
|
73 y = reshape (y, sz); |
3426
|
74 |
2537
|
75 endfunction |