function z = zernpol(n,m,r,nflag) `"<} B"s
%ZERNPOL Radial Zernike polynomials of order N and frequency M. idf~"a
% Z = ZERNPOL(N,M,R) returns the radial Zernike polynomials of (f 0p
% order N and frequency M, evaluated at R. N is a vector of bS+by'Ea1W
% positive integers (including 0), and M is a vector with the : qKxm(
% same number of elements as N. Each element k of M must be a \Om<
FH}
% positive integer, with possible values M(k) = 0,2,4,...,N(k) I
=t{ u;
% for N(k) even, and M(k) = 1,3,5,...,N(k) for N(k) odd. R is xrK%3nA4s"
% a vector of numbers between 0 and 1. The output Z is a matrix tndtwM*B'
% with one column for every (N,M) pair, and one row for every I T)rhi:
% element in R. K bY5
qou
% :o}Ju}t
% Z = ZERNPOL(N,M,R,'norm') returns the normalized Zernike poly- N.|Zh+!
% nomials. The normalization factor Nnm = sqrt(2*(n+1)) is 7B$iM,}.b
% chosen so that the integral of (r * [Znm(r)]^2) from r=0 to Elt=/,v`!
% r=1 is unity. For the non-normalized polynomials, Znm(r=1)=1 >Rw[ x
% for all [n,m]. "$?f&*
% Y" s1z<?
% The radial Zernike polynomials are the radial portion of the r[zxb0YA
% Zernike functions, which are an orthogonal basis on the unit \d0R&vFHQ
% circle. The series representation of the radial Zernike $up.<qzj
% polynomials is D5]sf>~
% 9d4PH
% (n-m)/2 ;/W;M> ^
% __ }Lx?RU+@=
% m \ s n-2s 9}FWO&LiB
% Z(r) = /__ (-1) [(n-s)!/(s!((n-m)/2-s)!((n+m)/2-s)!)] * r ~O~c^fLH(B
% n s=0 2B7X~t>8a
% Z@=1-l
% The following table shows the first 12 polynomials. }!\ZJo a
% cjU*
% n m Zernike polynomial Normalization =Uta5$\a)
% --------------------------------------------- hbhh
m
% 0 0 1 sqrt(2) 8?4j-
% 1 1 r 2 K+D`U6&
% 2 0 2*r^2 - 1 sqrt(6) 5Cd>p<
% 2 2 r^2 sqrt(6) k~u$&a
% 3 1 3*r^3 - 2*r sqrt(8) I_J;/!l=
% 3 3 r^3 sqrt(8) ?5nF` [rx
% 4 0 6*r^4 - 6*r^2 + 1 sqrt(10) ZD] ^Y}
% 4 2 4*r^4 - 3*r^2 sqrt(10) ,V3P.ni]
% 4 4 r^4 sqrt(10) ^0R.U+?+
% 5 1 10*r^5 - 12*r^3 + 3*r sqrt(12) k+2~=#
% 5 3 5*r^5 - 4*r^3 sqrt(12) |b{XnD_g
% 5 5 r^5 sqrt(12) z!5^UD8"W
% --------------------------------------------- vBUx)l
% {Q[{H'Oa
% Example: u=feR0|8
% a3
<D1"
% % Display three example Zernike radial polynomials
4G&E?
% r = 0:0.01:1; 5C/W_H+9iK
% n = [3 2 5]; <8p53*a
% m = [1 2 1]; , gk49z9
% z = zernpol(n,m,r); Y25S:XHk9
% figure [K;J#0V+&L
% plot(r,z) gk\IivPb
% grid on {%xwoMVc+
% legend('Z_3^1(r)','Z_2^2(r)','Z_5^1(r)','Location','NorthWest') o&1ewE(O]
% HcA;'L?Dw
% See also ZERNFUN, ZERNFUN2. )_OKw?Zi
]'=]=o~4
% A note on the algorithm. 1`N q
K
% ------------------------ dJM)~Ay-
% The radial Zernike polynomials are computed using the series Zf |%t
% representation shown in the Help section above. For many special ~`c?&YixU
% functions, direct evaluation using the series representation can xSZgQF~
% produce poor numerical results (floating point errors), because v!T%xUb0
% the summation often involves computing small differences between ;1&%Wj"d
% large successive terms in the series. (In such cases, the functions =vMFCp;mv
% are often evaluated using alternative methods such as recurrence &Vfdq6Y]
% relations: see the Legendre functions, for example). For the Zernike LFob1HH*8
% polynomials, however, this problem does not arise, because the mOr>*uR
% polynomials are evaluated over the finite domain r = (0,1), and k DS
% because the coefficients for a given polynomial are generally all hm >JBc:n-
% of similar magnitude. Z 9mY*}:U~
% C3Q[L}X\
% ZERNPOL has been written using a vectorized implementation: multiple .{x-A{l
% Zernike polynomials can be computed (i.e., multiple sets of [N,M] m'k.R
j
% values can be passed as inputs) for a vector of points R. To achieve tvynl;Y/
% this vectorization most efficiently, the algorithm in ZERNPOL
XeDiiI
% involves pre-determining all the powers p of R that are required to NS@j`6/U
% compute the outputs, and then compiling the {R^p} into a single PI(;t9]b
% matrix. This avoids any redundant computation of the R^p, and =4uL1[0'
% minimizes the sizes of certain intermediate variables. BpZE
% +0\BI<aG
% Paul Fricker 11/13/2006 R)d1]k8
LO<R<zz
+uKh]RP
% Check and prepare the inputs: RUUV"y
% ----------------------------- fEE[huG
if ( ~any(size(n)==1) ) || ( ~any(size(m)==1) ) NL
3ri7n
error('zernpol:NMvectors','N and M must be vectors.') f4)fa yAVp
end ya3A^&:
H=[eO
if length(n)~=length(m) w~hO)1c],:
error('zernpol:NMlength','N and M must be the same length.') =.O8G=;DOA
end m07=
_4
`z%f@/:fG
n = n(:); 0]=|3-n
m = m(:); wl H6
length_n = length(n); =#dW^?p
13wO6tS
k
if any(mod(n-m,2)) M_*"g>Z
error('zernpol:NMmultiplesof2','All N and M must differ by multiples of 2 (including 0).') XQ]no aU
end ?U.+SQ
hAtf)
if any(m<0) J7q^4M+o:
error('zernpol:Mpositive','All M must be positive.') L,c@Z@
end x9q?^\x
U/9xO"b{.
if any(m>n) Aon3G
error('zernpol:MlessthanN','Each M must be less than or equal to its corresponding N.') p;cNmMm
end O4J <u-E$
pA7&
if any( r>1 | r<0 ) >Q#h,x~vu
error('zernpol:Rlessthan1','All R must be between 0 and 1.') 7p*PDoM6`
end ?aOx
b
<5(P4cm9
if ~any(size(r)==1) l
Os91+.%
error('zernpol:Rvector','R must be a vector.') 2#LTd{
end dPZrX{ c
4\
R2\
r = r(:); <