function z = zernpol(n,m,r,nflag) x?-kt.M
%ZERNPOL Radial Zernike polynomials of order N and frequency M. EN2/3~syO-
% Z = ZERNPOL(N,M,R) returns the radial Zernike polynomials of >U'gQS?\]
% order N and frequency M, evaluated at R. N is a vector of {FNq&)#`
% positive integers (including 0), and M is a vector with the uze5u\
% same number of elements as N. Each element k of M must be a ;"DI)hdz
% positive integer, with possible values M(k) = 0,2,4,...,N(k) *Mr'/qp,
% for N(k) even, and M(k) = 1,3,5,...,N(k) for N(k) odd. R is !5h@uar
% a vector of numbers between 0 and 1. The output Z is a matrix `}&}2k
% with one column for every (N,M) pair, and one row for every 1jQlwT(:
% element in R. yM*<BV
% \dc*!Es
% Z = ZERNPOL(N,M,R,'norm') returns the normalized Zernike poly- ^Dw18gqr=@
% nomials. The normalization factor Nnm = sqrt(2*(n+1)) is _8nT$!\\
% chosen so that the integral of (r * [Znm(r)]^2) from r=0 to +^@6{1
% r=1 is unity. For the non-normalized polynomials, Znm(r=1)=1 /kK:{
% for all [n,m]. 3D"?|rd~
% g|V0[Hnq6
% The radial Zernike polynomials are the radial portion of the .2:S0=xt<
% Zernike functions, which are an orthogonal basis on the unit I=Xj;\b
% circle. The series representation of the radial Zernike |+(Hia,X
% polynomials is >>HC|
% SB2Ij',
% (n-m)/2 #`{L_n$c
% __ b R;Wf5
% m \ s n-2s CaqMLi%
% Z(r) = /__ (-1) [(n-s)!/(s!((n-m)/2-s)!((n+m)/2-s)!)] * r qz/d6-0"
% n s=0 b&Go'C{p
% Y!L<&
sl
% The following table shows the first 12 polynomials. p*S;4+>#
% : yC|Q)
% n m Zernike polynomial Normalization 07tSXl5!
% --------------------------------------------- 0}y-DCuQ
% 0 0 1 sqrt(2) Hg;; >
% 1 1 r 2 ?e+$?8l[3
% 2 0 2*r^2 - 1 sqrt(6) /0I=?+QSo
% 2 2 r^2 sqrt(6) /N82h`\n
% 3 1 3*r^3 - 2*r sqrt(8) AT]Ty
% 3 3 r^3 sqrt(8) iKN800^u
% 4 0 6*r^4 - 6*r^2 + 1 sqrt(10) BY^5z<^.
% 4 2 4*r^4 - 3*r^2 sqrt(10) GLIP;)h1
% 4 4 r^4 sqrt(10) G@;I^_gN
% 5 1 10*r^5 - 12*r^3 + 3*r sqrt(12) o@g/,V $
% 5 3 5*r^5 - 4*r^3 sqrt(12) Kw^tvRt'*
% 5 5 r^5 sqrt(12) 9,zM.g9Qv
% ---------------------------------------------
9
]W4o"
% oc3}L^aD
% Example: 3teanU`
% z ''-AH,
% % Display three example Zernike radial polynomials 5.e.
BT
% r = 0:0.01:1; mrz@Y0mgL
% n = [3 2 5]; y?s8UEC
% m = [1 2 1]; C2 ] x
% z = zernpol(n,m,r); ,HM~Zs
% figure $1$T2'C~+
% plot(r,z) \9t6#8
% grid on 86,$ I+
% legend('Z_3^1(r)','Z_2^2(r)','Z_5^1(r)','Location','NorthWest') FI$#x%A
% ,"W.A
% See also ZERNFUN, ZERNFUN2. .}l&lj@#
^ M4-O~
% A note on the algorithm. vAMr&[
% ------------------------ [5Dg%?x
% The radial Zernike polynomials are computed using the series AE"E($S`
% representation shown in the Help section above. For many special j,/t<@S>
% functions, direct evaluation using the series representation can 8fwM)DKS
% produce poor numerical results (floating point errors), because #Qp.O@e
% the summation often involves computing small differences between M :Aik&
% large successive terms in the series. (In such cases, the functions W=k%aB?p
% are often evaluated using alternative methods such as recurrence /Aq):T T
% relations: see the Legendre functions, for example). For the Zernike ?hQ,'M2
% polynomials, however, this problem does not arise, because the GxIw4m9
% polynomials are evaluated over the finite domain r = (0,1), and [d_sd
% because the coefficients for a given polynomial are generally all GI:$(<
% of similar magnitude. cOr@dUSL
% `b{.K,
% ZERNPOL has been written using a vectorized implementation: multiple uF=x o`=|
% Zernike polynomials can be computed (i.e., multiple sets of [N,M] _~ipO1*
% values can be passed as inputs) for a vector of points R. To achieve %g>{m2o
% this vectorization most efficiently, the algorithm in ZERNPOL %aszZP
% involves pre-determining all the powers p of R that are required to An%V>a-[
% compute the outputs, and then compiling the {R^p} into a single @Sl!p)
% matrix. This avoids any redundant computation of the R^p, and =abth6#)
% minimizes the sizes of certain intermediate variables. P00pSRQHD
% 3[jk}2R';p
% Paul Fricker 11/13/2006 D@ji1$K
,T|%vqbmw
Y%V|M0 0`
% Check and prepare the inputs: HGgw<Os-k
% ----------------------------- m9Uoq[1
if ( ~any(size(n)==1) ) || ( ~any(size(m)==1) ) Y{8L ~U:
error('zernpol:NMvectors','N and M must be vectors.') c[Mz#BWG
end (1vmtg.O
ZREAEGi{
if length(n)~=length(m) ^gdg0y!5~
error('zernpol:NMlength','N and M must be the same length.') X&<