function z = zernpol(n,m,r,nflag)
is'V%q
%ZERNPOL Radial Zernike polynomials of order N and frequency M. al2t\Iq90
% Z = ZERNPOL(N,M,R) returns the radial Zernike polynomials of RB 5SK#z
% order N and frequency M, evaluated at R. N is a vector of sV\_DP/l
% positive integers (including 0), and M is a vector with the oBzl=N3<
% same number of elements as N. Each element k of M must be a "y1Iu
% positive integer, with possible values M(k) = 0,2,4,...,N(k) @~3--
% for N(k) even, and M(k) = 1,3,5,...,N(k) for N(k) odd. R is W(, j2pU
% a vector of numbers between 0 and 1. The output Z is a matrix .tngN<f
% with one column for every (N,M) pair, and one row for every h>N}M}8
% element in R. );5o13h2
% z/@_?01T=
% Z = ZERNPOL(N,M,R,'norm') returns the normalized Zernike poly- 79\wjR!T
% nomials. The normalization factor Nnm = sqrt(2*(n+1)) is ]v+<K63@T
% chosen so that the integral of (r * [Znm(r)]^2) from r=0 to h9vcN#22D
% r=1 is unity. For the non-normalized polynomials, Znm(r=1)=1 i5,iJe0cA
% for all [n,m]. NGx3f3 9
% J2UQq 7-y
% The radial Zernike polynomials are the radial portion of the zM'eqo>!c>
% Zernike functions, which are an orthogonal basis on the unit } M#e\neii
% circle. The series representation of the radial Zernike /jbAf ]"F;
% polynomials is 5KCB^`|b>t
% Q;h.}N8W
% (n-m)/2 bO '\QtW9
% __ Sj9fq*
% m \ s n-2s )vp0X\3q`
% Z(r) = /__ (-1) [(n-s)!/(s!((n-m)/2-s)!((n+m)/2-s)!)] * r K_7pr~D]@r
% n s=0 ajYe?z
% gP^2GnjHL8
% The following table shows the first 12 polynomials. &#r+a'
% 8{ zX=
% n m Zernike polynomial Normalization 6{Wo5O{!\
% --------------------------------------------- -YRIe<}E -
% 0 0 1 sqrt(2) )2}R1K>
% 1 1 r 2 rIyH/=;
% 2 0 2*r^2 - 1 sqrt(6) pLMt2G
% 2 2 r^2 sqrt(6) qd`e:s*%
% 3 1 3*r^3 - 2*r sqrt(8) v^|U?
% 3 3 r^3 sqrt(8) i\R0+O{
% 4 0 6*r^4 - 6*r^2 + 1 sqrt(10) 5]xuU.w'
% 4 2 4*r^4 - 3*r^2 sqrt(10) 7|rH9Bc{U
% 4 4 r^4 sqrt(10) 3h@]cWp
% 5 1 10*r^5 - 12*r^3 + 3*r sqrt(12) .~z'm$s1o
% 5 3 5*r^5 - 4*r^3 sqrt(12) @^{Hq6_`
% 5 5 r^5 sqrt(12) ]hl*6
% --------------------------------------------- la!]Y-s)'4
% h Yu6PWK
% Example: 1{}p_"s>
% Jt~Ivn,
% % Display three example Zernike radial polynomials ZsmOn#`=^}
% r = 0:0.01:1; -<iP$,bq72
% n = [3 2 5];
-m@o\9Ic
% m = [1 2 1]; sNf& "C!;
% z = zernpol(n,m,r); >{#JIG.
% figure .RD<]BxJ
% plot(r,z) bIQ,=EA1
% grid on b#j:)PA0C
% legend('Z_3^1(r)','Z_2^2(r)','Z_5^1(r)','Location','NorthWest') Rr) 5[
% ]#!uke Q
% See also ZERNFUN, ZERNFUN2. #Z&/w.D2
'&>"`q
% A note on the algorithm. O
zAIz+`
% ------------------------ kZ]H[\Fs
% The radial Zernike polynomials are computed using the series %mI0*YRma
% representation shown in the Help section above. For many special 1S{Biqi+
% functions, direct evaluation using the series representation can j"W>fC/u
% produce poor numerical results (floating point errors), because x*7@b8J
% the summation often involves computing small differences between C]^Ep
% large successive terms in the series. (In such cases, the functions kY0HP a
% are often evaluated using alternative methods such as recurrence [%W'd9`>
% relations: see the Legendre functions, for example). For the Zernike Vl^(K_`(
% polynomials, however, this problem does not arise, because the #3uv^m LGa
% polynomials are evaluated over the finite domain r = (0,1), and NvK9L.K
% because the coefficients for a given polynomial are generally all iL/c^(1
% of similar magnitude. ycA<l"
% 0<M-asI?
% ZERNPOL has been written using a vectorized implementation: multiple 05UN
<l]
% Zernike polynomials can be computed (i.e., multiple sets of [N,M] i~B?p[
% values can be passed as inputs) for a vector of points R. To achieve -I< >Ab
% this vectorization most efficiently, the algorithm in ZERNPOL
-D^I;[j_
% involves pre-determining all the powers p of R that are required to 5Xy(za
% compute the outputs, and then compiling the {R^p} into a single ,67Q!/O
% matrix. This avoids any redundant computation of the R^p, and _nGx[1G( 5
% minimizes the sizes of certain intermediate variables. 7h'
C"rH
% ChBf:`e
% Paul Fricker 11/13/2006 F.s$Y+c!6
C{)1#<`
?hoOSur+
% Check and prepare the inputs: [8V;Q
% ----------------------------- Cq5.gkS<
if ( ~any(size(n)==1) ) || ( ~any(size(m)==1) ) ULx:2jz
error('zernpol:NMvectors','N and M must be vectors.') 'nmGHorp
end 0uy'Py@2<
e=amh
if length(n)~=length(m) kc'$4 J4Tw
error('zernpol:NMlength','N and M must be the same length.') X9>fE{)!
end I}$`gUXX8x
r&=ulg
n = n(:); s{^98*
m = m(:); cXweg;
length_n = length(n); q~{)
{t;
w\C1Bh!
if any(mod(n-m,2)) !z?
error('zernpol:NMmultiplesof2','All N and M must differ by multiples of 2 (including 0).') RB>=#03
end )W\)37=.
]4~Yi1]
if any(m<0)
3@Ndn
error('zernpol:Mpositive','All M must be positive.') 2- iY:r
end e02Hf{eOfw
HcRw9,I'
if any(m>n) 7w
)?s@CD
error('zernpol:MlessthanN','Each M must be less than or equal to its corresponding N.') S!K<kn`E3
end ,GOIg|51
tFU4%c7V
if any( r>1 | r<0 ) fe .=Z&
error('zernpol:Rlessthan1','All R must be between 0 and 1.') +%5 L2/n7
end =<\22d5L
,%!m%+K9a
if ~any(size(r)==1) XG#?fr}L
error('zernpol:Rvector','R must be a vector.') w4
yrAj
2
end T!/o^0w
A%w9Da?B
r = r(:); ,fjY|ip
length_r = length(r); B>{%$@4
qI'pjTMDY
if nargin==4 7cc^n\c?Y
isnorm = ischar(nflag) & strcmpi(nflag,'norm'); ;<