function z = zernpol(n,m,r,nflag) bAxTLIf
%ZERNPOL Radial Zernike polynomials of order N and frequency M. ydWtvFuS
% Z = ZERNPOL(N,M,R) returns the radial Zernike polynomials of I.}1JJF*
% order N and frequency M, evaluated at R. N is a vector of T#:F]=
% positive integers (including 0), and M is a vector with the &;H{cv`
% same number of elements as N. Each element k of M must be a e1
*__'
% positive integer, with possible values M(k) = 0,2,4,...,N(k) iZ[tHw||
% for N(k) even, and M(k) = 1,3,5,...,N(k) for N(k) odd. R is Y ?]G}5
% a vector of numbers between 0 and 1. The output Z is a matrix 9Z\z96O-
% with one column for every (N,M) pair, and one row for every guN4-gGDr<
% element in R. Kn+=lCk
% #`tD1T{;
% Z = ZERNPOL(N,M,R,'norm') returns the normalized Zernike poly- `NwdbKX
% nomials. The normalization factor Nnm = sqrt(2*(n+1)) is _J?SIm
% chosen so that the integral of (r * [Znm(r)]^2) from r=0 to >Pe:I
% r=1 is unity. For the non-normalized polynomials, Znm(r=1)=1 E(+T*
% for all [n,m]. {g/wY%u=
% o}8{Bh^
% The radial Zernike polynomials are the radial portion of the `0BdMKjA
% Zernike functions, which are an orthogonal basis on the unit
B{,
Bno
% circle. The series representation of the radial Zernike DOD6Liau{Q
% polynomials is &yH#s
8^8
% g jG2
% (n-m)/2 RagiV6c
% __ 67<CbQZoN3
% m \ s n-2s ~] =?b)B
% Z(r) = /__ (-1) [(n-s)!/(s!((n-m)/2-s)!((n+m)/2-s)!)] * r V*B0lI7`B
% n s=0 &jts:^N>
% Oo%!>!Lt,
% The following table shows the first 12 polynomials. 24@^{
}
% Wb=Jj 9;
% n m Zernike polynomial Normalization :q2YBa
% --------------------------------------------- _[E \=
% 0 0 1 sqrt(2) f[/.I,9U^
% 1 1 r 2 H$!-f>Rxa
% 2 0 2*r^2 - 1 sqrt(6) !Cj(A"uqY
% 2 2 r^2 sqrt(6) GXb47_b^
% 3 1 3*r^3 - 2*r sqrt(8) 5ouQQ)vA
% 3 3 r^3 sqrt(8) O4+F^+qN
% 4 0 6*r^4 - 6*r^2 + 1 sqrt(10) +80 2`eax
% 4 2 4*r^4 - 3*r^2 sqrt(10) QJ4AL3
^6
% 4 4 r^4 sqrt(10) gn5% F5W
% 5 1 10*r^5 - 12*r^3 + 3*r sqrt(12) (:?&G9k
"
% 5 3 5*r^5 - 4*r^3 sqrt(12) < tQc_
% 5 5 r^5 sqrt(12) *8!w&ME+.
% --------------------------------------------- 'XfgBJF=
% kJ8vKcc
% Example: KVijs1q
% >iy^$bqF
% % Display three example Zernike radial polynomials Vq}r_#!Q
% r = 0:0.01:1; Z*bC#s?
% n = [3 2 5]; yKDZ+3xK]
% m = [1 2 1]; \y*j4 0
% z = zernpol(n,m,r); ;/Q6i
% figure oa0X5}D
% plot(r,z) _@prmSc
% grid on NiTJ}1 l
% legend('Z_3^1(r)','Z_2^2(r)','Z_5^1(r)','Location','NorthWest') {'IFWD. 5
% u( 9X
% See also ZERNFUN, ZERNFUN2. GoeIjuELR
}'`xu9<
% A note on the algorithm. 3_J>y
% ------------------------ ="lI i$>O
% The radial Zernike polynomials are computed using the series $9i9s4u^
% representation shown in the Help section above. For many special T'R,vxP)\
% functions, direct evaluation using the series representation can =x4a~=HX
% produce poor numerical results (floating point errors), because 2Guvze_bU
% the summation often involves computing small differences between a;&}zcc*
% large successive terms in the series. (In such cases, the functions #{>uC&jD
% are often evaluated using alternative methods such as recurrence t{ H1u
% relations: see the Legendre functions, for example). For the Zernike 6$z'wy/*
% polynomials, however, this problem does not arise, because the @^wpAQfd4
% polynomials are evaluated over the finite domain r = (0,1), and "A7<XN<
% because the coefficients for a given polynomial are generally all `cO|RhD@
% of similar magnitude. [aNhP;<
% l:z};
% ZERNPOL has been written using a vectorized implementation: multiple h2&y<Eg >
% Zernike polynomials can be computed (i.e., multiple sets of [N,M] Doj(.wm~
% values can be passed as inputs) for a vector of points R. To achieve P!>g7X
% this vectorization most efficiently, the algorithm in ZERNPOL T&4fBMBp,%
% involves pre-determining all the powers p of R that are required to IozNjII$:.
% compute the outputs, and then compiling the {R^p} into a single CgoXZX
% matrix. This avoids any redundant computation of the R^p, and w -dI<s
% minimizes the sizes of certain intermediate variables. ;Xh5oB\)W
% BK*UR+,
% Paul Fricker 11/13/2006 ]-EN/V
@H4wHlb
<{ #<5 8
% Check and prepare the inputs: vrO%XvXW
% ----------------------------- w06gY
if ( ~any(size(n)==1) ) || ( ~any(size(m)==1) ) bZXlJa`'S
error('zernpol:NMvectors','N and M must be vectors.') Wbd_aR
(
end <)J55++
&z QWIv
if length(n)~=length(m) 9/Wn!Ld
error('zernpol:NMlength','N and M must be the same length.') +WdL
end * 2%oZXF
rT<1S?jR
n = n(:); wNR=?Z~
m = m(:); |DGCdB|`G
length_n = length(n); d1lH[r!Z
gQ,4xTX
if any(mod(n-m,2)) ]wR6bEm7
error('zernpol:NMmultiplesof2','All N and M must differ by multiples of 2 (including 0).')
T"B8;|
end }Oh5Nm)
E]?2!)mgce
if any(m<0) VHj*aBHB
error('zernpol:Mpositive','All M must be positive.') _UaPwJ
end LiF.w:}
!&6-(q9
if any(m>n) G-sa
L*
error('zernpol:MlessthanN','Each M must be less than or equal to its corresponding N.') .:SfMr;G
end MKe *f%
"|\94
if any( r>1 | r<0 ) qWkx:-g]
error('zernpol:Rlessthan1','All R must be between 0 and 1.') CCy.
end lvG3<ls0K$
DSTx#*
if ~any(size(r)==1) qm~Kw!kV
error('zernpol:Rvector','R must be a vector.') R<t&F\>
end HK?Foo?
fA;x{0CAMX
r = r(:); np= J:v4
length_r = length(r); Zq9>VqGe
KM E XT$p
if nargin==4 @!Y.935/0
isnorm = ischar(nflag) & strcmpi(nflag,'norm'); z{AM2Z
if ~isnorm l; */M.B
error('zernpol:normalization','Unrecognized normalization flag.') EyzY2>"^
end x[Hhj'
else xvHOY:
isnorm = false; ;,R[]B01u
end zabw!@]
>&g2 IvDS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% i^~sn `o
% Compute the Zernike Polynomials La@\q[U{@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% A<]&JbIt
t%
<pbZO
% Determine the required powers of r: /Lj%A
% ----------------------------------- T$f:[ye]Z
rpowers = []; PZ~`O
for j = 1:length(n) F1zT )wW
rpowers = [rpowers m(j):2:n(j)]; rUGZjLIGqz
end "Fo
rpowers = unique(rpowers); rGGS]^
elNB7%Y/
% Pre-compute the values of r raised to the required powers, :A,O(
% and compile them in a matrix: `NTM%# w
% ----------------------------- 3#7ENV`
if rpowers(1)==0 wbn^R'
rpowern = arrayfun(@(p)r.^p,rpowers(2:end),'UniformOutput',false); ZE{aS4c
rpowern = cat(2,rpowern{:}); ccIDMJ=2
rpowern = [ones(length_r,1) rpowern]; `4se7{'UK`
else eUi> Mp
rpowern = arrayfun(@(p)r.^p,rpowers,'UniformOutput',false); NU BpIx&
rpowern = cat(2,rpowern{:}); z&\Il#'\m+
end nYo&x'
xn0s`I[
% Compute the values of the polynomials: Ka[Sm|-q
% -------------------------------------- c8H9_6
z = zeros(length_r,length_n); )zo#1$C-
for j = 1:length_n ^U R-#WaQ
s = 0:(n(j)-m(j))/2; oNh68ON:c
pows = n(j):-2:m(j); 9;}L{yve
for k = length(s):-1:1 ]t8{)r
p = (1-2*mod(s(k),2))* ... m4wPuW
prod(2:(n(j)-s(k)))/ ... ly9x1`?$
prod(2:s(k))/ ... yd\5Z[iEp
prod(2:((n(j)-m(j))/2-s(k)))/ ... f$~ _FX
prod(2:((n(j)+m(j))/2-s(k))); cg>!<T*
idx = (pows(k)==rpowers); 3RBpbTNWp
z(:,j) = z(:,j) + p*rpowern(:,idx); 7O;BS}Lv=
end =ip~J<sw&
jAD+:@
if isnorm ]b5%?^Z#
z(:,j) = z(:,j)*sqrt(2*(n(j)+1)); #RCZA4>
end &ryl$!!3H
end 6{Krw\0
fU\k?'x_
% EOF zernpol