function z = zernpol(n,m,r,nflag) eKpH|S!xU
%ZERNPOL Radial Zernike polynomials of order N and frequency M. I ms?^`N
% Z = ZERNPOL(N,M,R) returns the radial Zernike polynomials of .x$+7$G
% order N and frequency M, evaluated at R. N is a vector of uk_?2?>-5
% positive integers (including 0), and M is a vector with the qt+vmi+~
% same number of elements as N. Each element k of M must be a J%EbJ5p<QF
% positive integer, with possible values M(k) = 0,2,4,...,N(k) gOa'o<
% for N(k) even, and M(k) = 1,3,5,...,N(k) for N(k) odd. R is M)6_Tal
% a vector of numbers between 0 and 1. The output Z is a matrix %MgQ.
% with one column for every (N,M) pair, and one row for every h<i.@&
% element in R. 3*64)Ol7t]
% YY]JjMkU
% Z = ZERNPOL(N,M,R,'norm') returns the normalized Zernike poly- r ,|T@|{
% nomials. The normalization factor Nnm = sqrt(2*(n+1)) is It!%/Y5
% chosen so that the integral of (r * [Znm(r)]^2) from r=0 to D0=D8P}H:
% r=1 is unity. For the non-normalized polynomials, Znm(r=1)=1 :*#AJV)
% for all [n,m]. #b []-L!
% L.lmbxn
% The radial Zernike polynomials are the radial portion of the ;PI=jp
% Zernike functions, which are an orthogonal basis on the unit 4p&qH igG
% circle. The series representation of the radial Zernike }S3m
wp<Y
% polynomials is I-4csw<Qy
% vn~DtTp/
% (n-m)/2 gSK
(BP|
% __ e{.2*>pH
% m \ s n-2s nX<!n\J T
% Z(r) = /__ (-1) [(n-s)!/(s!((n-m)/2-s)!((n+m)/2-s)!)] * r N%/Qc hu
% n s=0 l%.3hId-
% cnC&=6=a<
% The following table shows the first 12 polynomials. /K<Xr[z~y
% m C_v!nL.
% n m Zernike polynomial Normalization 5 |{0|mP
% --------------------------------------------- =El.uBz{
% 0 0 1 sqrt(2) :gVz}/C.@
% 1 1 r 2 Z<K[
% 2 0 2*r^2 - 1 sqrt(6) `4}zB#3
% 2 2 r^2 sqrt(6) vLJ<_&6
% 3 1 3*r^3 - 2*r sqrt(8) 8vz9o <I
% 3 3 r^3 sqrt(8) 8 wQV^G
% 4 0 6*r^4 - 6*r^2 + 1 sqrt(10) C78YHjy
% 4 2 4*r^4 - 3*r^2 sqrt(10) `,tv&siSA
% 4 4 r^4 sqrt(10) uu1-` !%
% 5 1 10*r^5 - 12*r^3 + 3*r sqrt(12) <_8\}!
% 5 3 5*r^5 - 4*r^3 sqrt(12) soA>&b!?
% 5 5 r^5 sqrt(12) {>fvyF
% --------------------------------------------- Wk$[;>NU3
% -7TT6+H)
% Example: 1\{0z3P
% b\"JXfw
% % Display three example Zernike radial polynomials yAG4W[
% r = 0:0.01:1; D$;mur'
% n = [3 2 5]; h|m h_T{+
% m = [1 2 1]; Fl]$ql
% z = zernpol(n,m,r); Yq4_ss'nB
% figure M' "S:
% plot(r,z) tx}{E<\>$
% grid on k]`I3>/L
% legend('Z_3^1(r)','Z_2^2(r)','Z_5^1(r)','Location','NorthWest') .Gh-T{\V'
% $TtCVR
% See also ZERNFUN, ZERNFUN2. GfD!Z3
ko@I]gi2
% A note on the algorithm. ~0>g 4
D.
% ------------------------ XB UO
% The radial Zernike polynomials are computed using the series
;B o 2$
% representation shown in the Help section above. For many special Hwklk9U
% functions, direct evaluation using the series representation can Q?L-6]pg
% produce poor numerical results (floating point errors), because Ui@Q&%b
% the summation often involves computing small differences between ;VeC(^-eh6
% large successive terms in the series. (In such cases, the functions |L.QIr,jCC
% are often evaluated using alternative methods such as recurrence *I(>[m!
% relations: see the Legendre functions, for example). For the Zernike @sav8]
% polynomials, however, this problem does not arise, because the {jcrTjmxe
% polynomials are evaluated over the finite domain r = (0,1), and UMpC2)5
% because the coefficients for a given polynomial are generally all YDyOhv
% of similar magnitude. ]MfT5#(6h
% eEb(TG~,Y
% ZERNPOL has been written using a vectorized implementation: multiple #n]js7
% Zernike polynomials can be computed (i.e., multiple sets of [N,M] NcZ6!wWdE
% values can be passed as inputs) for a vector of points R. To achieve iyc}a6g
% this vectorization most efficiently, the algorithm in ZERNPOL -@Mr!!t?N
% involves pre-determining all the powers p of R that are required to fMlxtj+5
% compute the outputs, and then compiling the {R^p} into a single j5|PQOK
% matrix. This avoids any redundant computation of the R^p, and ~zi6wu(3
% minimizes the sizes of certain intermediate variables. :-xp'_\L
% he8y
% Paul Fricker 11/13/2006 fJn;|'H!
I{;s.2
I?lQN$A.E
% Check and prepare the inputs: aU<0<Dx
% ----------------------------- GQ[:vX`
if ( ~any(size(n)==1) ) || ( ~any(size(m)==1) ) j0>S)Q
error('zernpol:NMvectors','N and M must be vectors.') %g^dB M#
end |t1D8){!
J)oa:Q
if length(n)~=length(m) V?kJYf(<
error('zernpol:NMlength','N and M must be the same length.') J~V`"uo
end i{I'+%~R
XG@_Lcv*
n = n(:); }at8b ^
m = m(:); 7h<B:~(K
length_n = length(n); T12?'JL^r
U O YM
if any(mod(n-m,2)) B%6>2S=E
error('zernpol:NMmultiplesof2','All N and M must differ by multiples of 2 (including 0).') o )GNV
end oil s;*q
X<Rh-1$8F
if any(m<0) ELk$lm&