function z = zernpol(n,m,r,nflag) |gfG\fL3V
%ZERNPOL Radial Zernike polynomials of order N and frequency M. |
% Z = ZERNPOL(N,M,R) returns the radial Zernike polynomials of Z
jXn,W]~
% order N and frequency M, evaluated at R. N is a vector of T~d_?UAw$
% positive integers (including 0), and M is a vector with the y!~ }7=
% same number of elements as N. Each element k of M must be a |sAl k,8s
% positive integer, with possible values M(k) = 0,2,4,...,N(k) 6<YAoo
% for N(k) even, and M(k) = 1,3,5,...,N(k) for N(k) odd. R is 9ol&p>
% a vector of numbers between 0 and 1. The output Z is a matrix F 2Mxcs*M
% with one column for every (N,M) pair, and one row for every =V:Al
% element in R. 7<LCX{Uw
% /7WdG)'
% Z = ZERNPOL(N,M,R,'norm') returns the normalized Zernike poly- +_ $!9m
% nomials. The normalization factor Nnm = sqrt(2*(n+1)) is N \woFrG
% chosen so that the integral of (r * [Znm(r)]^2) from r=0 to Crezo?
% r=1 is unity. For the non-normalized polynomials, Znm(r=1)=1 26=G%F6
% for all [n,m]. )p{,5"0u
% 5#dJga/88
% The radial Zernike polynomials are the radial portion of the _I0=a@3
% Zernike functions, which are an orthogonal basis on the unit :VX2&*
% circle. The series representation of the radial Zernike (b7',:_U7
% polynomials is sLc,Dx"+
% 0*KL*Gn
% (n-m)/2 kXOc)
% __ 9{-H/YS\_s
% m \ s n-2s ".kH5(:
% Z(r) = /__ (-1) [(n-s)!/(s!((n-m)/2-s)!((n+m)/2-s)!)] * r D*g
K, `
% n s=0 u/J1Z>0
% C-^8;xd
% The following table shows the first 12 polynomials. c7]0>nU;
% <lRjh7
% n m Zernike polynomial Normalization @={
qy}
% --------------------------------------------- {gB9EGY
% 0 0 1 sqrt(2) s6Il3Kf
% 1 1 r 2 bj@f<f`
% 2 0 2*r^2 - 1 sqrt(6) ~eXI}KhBw6
% 2 2 r^2 sqrt(6) x}O J~Yk]
% 3 1 3*r^3 - 2*r sqrt(8) FW3uq^
% 3 3 r^3 sqrt(8) q<cxmo0S
% 4 0 6*r^4 - 6*r^2 + 1 sqrt(10) nHQWO
% 4 2 4*r^4 - 3*r^2 sqrt(10) !HF<fn
% 4 4 r^4 sqrt(10) %kuUQ%W1
% 5 1 10*r^5 - 12*r^3 + 3*r sqrt(12) AH^ud*3F
% 5 3 5*r^5 - 4*r^3 sqrt(12) `6v24?z
% 5 5 r^5 sqrt(12) K}p0$Lc
% --------------------------------------------- n9V8A[QJ
% cEK#5
% Example: "71Y{WQ
% 7s0pH+
% % Display three example Zernike radial polynomials }5}#QHF
% r = 0:0.01:1; U[hokwZ
% n = [3 2 5]; gj4ONmY
% m = [1 2 1]; PVrNS7 Rk/
% z = zernpol(n,m,r); X*`b}^T
% figure 4XSq\.@G
% plot(r,z) !y3XIbdS"
% grid on fjm3X$tR
% legend('Z_3^1(r)','Z_2^2(r)','Z_5^1(r)','Location','NorthWest') :DFtH13qO
% ,v#3A7"yW
% See also ZERNFUN, ZERNFUN2. vg5fMH9ZZ
07>D G#
% A note on the algorithm. "j=E8Dd}
% ------------------------ w=[ITQ|W%
% The radial Zernike polynomials are computed using the series |wyua@2
% representation shown in the Help section above. For many special 5IbCE.>iU
% functions, direct evaluation using the series representation can L8KaK
% produce poor numerical results (floating point errors), because
y/"CWD/ i
% the summation often involves computing small differences between A9z3SJ\vXl
% large successive terms in the series. (In such cases, the functions y)3OQ24
% are often evaluated using alternative methods such as recurrence gj82qy\:
% relations: see the Legendre functions, for example). For the Zernike =P,pW
% polynomials, however, this problem does not arise, because the [2ri=lf,
% polynomials are evaluated over the finite domain r = (0,1), and a@#<qf8g
% because the coefficients for a given polynomial are generally all )#,a'~w
% of similar magnitude. f+2mX"Z[F
% 6dYa07
% ZERNPOL has been written using a vectorized implementation: multiple [o.#$(
% Zernike polynomials can be computed (i.e., multiple sets of [N,M] 9!6u Yf+
% values can be passed as inputs) for a vector of points R. To achieve DN;$->>
% this vectorization most efficiently, the algorithm in ZERNPOL
&0OH:P%
% involves pre-determining all the powers p of R that are required to q'%!qa+
% compute the outputs, and then compiling the {R^p} into a single U :8cz=#
% matrix. This avoids any redundant computation of the R^p, and m[Qr>= "
% minimizes the sizes of certain intermediate variables. o)H|
#9h5
% @Suww@<
% Paul Fricker 11/13/2006 *]ME]2qP
G_xql_QR
VgMuX3=
% Check and prepare the inputs: )a$sx}
% ----------------------------- /km0[M
if ( ~any(size(n)==1) ) || ( ~any(size(m)==1) ) >dwY(a
error('zernpol:NMvectors','N and M must be vectors.') h2
>a_0"
end [V0%=q+ R
*\^(-p~M
if length(n)~=length(m) TtgsM}Fm
error('zernpol:NMlength','N and M must be the same length.') ;>Z0e`=
end ZsGJ[
\$,8aRT>#U
n = n(:); ~2QD.(
m = m(:); rC6EgWt<V
length_n = length(n); TZarI-A
r`PD}6\
if any(mod(n-m,2)) T|uG1
error('zernpol:NMmultiplesof2','All N and M must differ by multiples of 2 (including 0).') #W/ATsDt
end "}:SXAZ5`
>eX 9dA3X
if any(m<0) HyIyrU rYW
error('zernpol:Mpositive','All M must be positive.') B8zc#0!1
end }q:4Zh'l!
7 M$cIWe$
if any(m>n) >&Vz/0
error('zernpol:MlessthanN','Each M must be less than or equal to its corresponding N.') D*ZswHT{y
end yRt7&,}zL
/&yc?Ui
if any( r>1 | r<0 ) `=2p6<#z
error('zernpol:Rlessthan1','All R must be between 0 and 1.') 3/8<dc
end _hEr,IX=J
j_SUR)5
if ~any(size(r)==1) e,l-}=5*P
error('zernpol:Rvector','R must be a vector.') @[]#[7
end P -X2A2
l<yYfGO
r = r(:); |^OK@KdL1
length_r = length(r); LVJn2t^
I~,b ZA
if nargin==4 4 Z&KR<2Z
isnorm = ischar(nflag) & strcmpi(nflag,'norm'); >&Y\g?Z6G
if ~isnorm Z5[g[Q
error('zernpol:normalization','Unrecognized normalization flag.') {}BAQ9|q
end B\+uRiD8w
else Eopb##o
isnorm = false; }`]Et99Q5
end F:LrQu
Am#Pa,g
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% euET)Ccq
% Compute the Zernike Polynomials ^O&&QR