function z = zernpol(n,m,r,nflag) E^w2IIw
%ZERNPOL Radial Zernike polynomials of order N and frequency M. I|69|^
% Z = ZERNPOL(N,M,R) returns the radial Zernike polynomials of lxb+0fiN
% order N and frequency M, evaluated at R. N is a vector of ,T@+QXh
% positive integers (including 0), and M is a vector with the &5puGnTZ
% same number of elements as N. Each element k of M must be a %jz]s4u$5j
% positive integer, with possible values M(k) = 0,2,4,...,N(k) 0+MNu8t
% for N(k) even, and M(k) = 1,3,5,...,N(k) for N(k) odd. R is k#Qav1_
% a vector of numbers between 0 and 1. The output Z is a matrix >QO^h<.>
% with one column for every (N,M) pair, and one row for every Jb~$Vrdy
% element in R. 0Jz H dz
% O 4zD
>O
% Z = ZERNPOL(N,M,R,'norm') returns the normalized Zernike poly- |U{9Yy6p
% nomials. The normalization factor Nnm = sqrt(2*(n+1)) is li'h&!|]
% chosen so that the integral of (r * [Znm(r)]^2) from r=0 to G2
A#&86J{
% r=1 is unity. For the non-normalized polynomials, Znm(r=1)=1
0$)s? \
% for all [n,m]. FsQeyh>
% .j?`U[V%a
% The radial Zernike polynomials are the radial portion of the 873$EiyXR
% Zernike functions, which are an orthogonal basis on the unit O
]o7
% circle. The series representation of the radial Zernike p=%Vo@*]
% polynomials is XN9s!5A<L)
% |,3s]b`
% (n-m)/2 M)S(:Il6Xx
% __ &
$E[l'
% m \ s n-2s F.5'5%
% Z(r) = /__ (-1) [(n-s)!/(s!((n-m)/2-s)!((n+m)/2-s)!)] * r e??tp]PLn
% n s=0 Zjqa n
% x`T
% The following table shows the first 12 polynomials. xC N6?
% '%Og9Bgd+
% n m Zernike polynomial Normalization e RY2.!
% --------------------------------------------- _8t5rF
% 0 0 1 sqrt(2) 9U[Gh97Sf
% 1 1 r 2 rR`'l=,t
% 2 0 2*r^2 - 1 sqrt(6) *D`]7I~}
% 2 2 r^2 sqrt(6) a&:1W83
% 3 1 3*r^3 - 2*r sqrt(8) Gk_%WY*
% 3 3 r^3 sqrt(8) &"HxAK)f
% 4 0 6*r^4 - 6*r^2 + 1 sqrt(10) Mx9#YJ?t~
% 4 2 4*r^4 - 3*r^2 sqrt(10) DUH\/<^g
% 4 4 r^4 sqrt(10) ?bFP'.
% 5 1 10*r^5 - 12*r^3 + 3*r sqrt(12) cUW>`F(S
% 5 3 5*r^5 - 4*r^3 sqrt(12) ?LJ$:u
% 5 5 r^5 sqrt(12) *+(t2!yFmE
% --------------------------------------------- UNLmnj;-Q
% CTawXHM
% Example: kc*zP=
% ^n8ioL\*i
% % Display three example Zernike radial polynomials |OW/-&)
% r = 0:0.01:1; !ieMhJ5r
% n = [3 2 5]; N>h/!#
ZC
% m = [1 2 1]; C]S~DK1
% z = zernpol(n,m,r); @ig'CF%(
% figure
[/dGOl+
% plot(r,z) ?%RAX CK
% grid on fP 1V1ao
% legend('Z_3^1(r)','Z_2^2(r)','Z_5^1(r)','Location','NorthWest') c:#<g/-{wM
% 1{6 BU!
% See also ZERNFUN, ZERNFUN2. ]vj.s/F~
L{`S^'P<
% A note on the algorithm. "FuOWI{in
% ------------------------ U@t"o3E
% The radial Zernike polynomials are computed using the series 0$=Uhi
% representation shown in the Help section above. For many special EQQ/E!N8l
% functions, direct evaluation using the series representation can wVegr
% produce poor numerical results (floating point errors), because 5zk<s`h
% the summation often involves computing small differences between SCwAAE9s]
% large successive terms in the series. (In such cases, the functions ~ZrSoVP=
% are often evaluated using alternative methods such as recurrence ggluQGA
% relations: see the Legendre functions, for example). For the Zernike [3$L}m
% polynomials, however, this problem does not arise, because the H~Z$ pk%
% polynomials are evaluated over the finite domain r = (0,1), and y{&k`H
% because the coefficients for a given polynomial are generally all 4%! #=JCl
% of similar magnitude.
Zl,c+/
% 7
s+j)
% ZERNPOL has been written using a vectorized implementation: multiple X;2I'
Kg
% Zernike polynomials can be computed (i.e., multiple sets of [N,M] $~>3bik@
% values can be passed as inputs) for a vector of points R. To achieve '8%pEl^
% this vectorization most efficiently, the algorithm in ZERNPOL #+VH]7]
% involves pre-determining all the powers p of R that are required to 0!4;."S
% compute the outputs, and then compiling the {R^p} into a single @|I:A
% matrix. This avoids any redundant computation of the R^p, and V[9#+l~#
% minimizes the sizes of certain intermediate variables. }E
o\=>l7
% Ufx^@%v
% Paul Fricker 11/13/2006 2bJqZ,@
K)-Gv|*t
[^N8v;O
% Check and prepare the inputs: NxOiT#YH
% ----------------------------- 8]SJ=c"}Xf
if ( ~any(size(n)==1) ) || ( ~any(size(m)==1) ) [cJQ"G '
error('zernpol:NMvectors','N and M must be vectors.') Mn)>G36(
end ,/m@<NyK
!WT Z=|
if length(n)~=length(m) .`I;qF
error('zernpol:NMlength','N and M must be the same length.') =J@M,mbHg
end j@w+>h
=1!,A
n = n(:); Vgh;w-a
m = m(:); OO7sj@
length_n = length(n); 8`\^wG$W
25bbuhss
if any(mod(n-m,2)) "o| f
error('zernpol:NMmultiplesof2','All N and M must differ by multiples of 2 (including 0).') "hE/f~\
end /6?A#%hc
} kNbqwVP
if any(m<0) v~l_6V}
error('zernpol:Mpositive','All M must be positive.') n jfh4}g:
end /KL;%:7
{c
82bFiv
if any(m>n) os:/-A_m
error('zernpol:MlessthanN','Each M must be less than or equal to its corresponding N.') 6}V)\"u&
end {"^LUw8fd
,5Vc
if any( r>1 | r<0 ) ywSV4ZtM
error('zernpol:Rlessthan1','All R must be between 0 and 1.') Sio> QL Y
end '7'*+sgi$
su?{Cj6*
if ~any(size(r)==1) _oV;Y`_
error('zernpol:Rvector','R must be a vector.') qcNu9Ih
end s!lLdR[g
98c##NV(7|
r = r(:); qVHXZdGL
length_r = length(r); |igr3p5Fw
PZT]H?
if nargin==4 mYU7b8x_
isnorm = ischar(nflag) & strcmpi(nflag,'norm'); [RAzKzC\M
if ~isnorm *qX!
error('zernpol:normalization','Unrecognized normalization flag.') +%O_xqq
end t:NYsL
else G,{=sFX
isnorm = false; b`W2^/D
end |"K<
LnwI 7uvq
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2H,^i,
% Compute the Zernike Polynomials 6%jv|\>
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% d9j+==S
<
uG5RE
% Determine the required powers of r: O^Y}fo'
% ----------------------------------- %=ZN2)7{
rpowers = []; 8+7n"6GY2/
for j = 1:length(n) .C6wsmQ
rpowers = [rpowers m(j):2:n(j)]; I.4o9Z[?
end iY|zv|;]=
rpowers = unique(rpowers); LTn@OhC
(0wQ [(
% Pre-compute the values of r raised to the required powers, ^R g=*L
% and compile them in a matrix: wqB 5KxO
% ----------------------------- nnzfKn:J
if rpowers(1)==0 6w? l
I
rpowern = arrayfun(@(p)r.^p,rpowers(2:end),'UniformOutput',false); jX9{Ki"
rpowern = cat(2,rpowern{:}); gv6}GE
rpowern = [ones(length_r,1) rpowern]; ak SUk)}e
else k;7R3O@
rpowern = arrayfun(@(p)r.^p,rpowers,'UniformOutput',false); |9fvj6?Y
rpowern = cat(2,rpowern{:}); GlVb |O"
end >}uDQwX8
`4xnM`:L"
% Compute the values of the polynomials: =DL
|Q
% -------------------------------------- @4O;dFOQ)
z = zeros(length_r,length_n); I[x+7Y0k9
for j = 1:length_n .wdWs tQ
s = 0:(n(j)-m(j))/2;
E43Gk!/|(
pows = n(j):-2:m(j); Tz`O+fx&
for k = length(s):-1:1 TKwMgC}<[
p = (1-2*mod(s(k),2))* ... u|.c?fW'3
prod(2:(n(j)-s(k)))/ ... o+wG69
prod(2:s(k))/ ... O<*l"fw3
prod(2:((n(j)-m(j))/2-s(k)))/ ... <FkoWN
prod(2:((n(j)+m(j))/2-s(k))); qe/|u3I<lF
idx = (pows(k)==rpowers); u|G&CV#r
z(:,j) = z(:,j) + p*rpowern(:,idx); nfldj33*
end >~%EB?8
rfz\DvVd
if isnorm wU"0@^k]<
z(:,j) = z(:,j)*sqrt(2*(n(j)+1)); |}FK;@'I 6
end oP"X-I
end hja;d1yH
<[oPh(!V
% EOF zernpol