function z = zernpol(n,m,r,nflag) (/N`Wu
%ZERNPOL Radial Zernike polynomials of order N and frequency M. _c #P
% Z = ZERNPOL(N,M,R) returns the radial Zernike polynomials of 9(B)
% order N and frequency M, evaluated at R. N is a vector of =0v{+#}
% positive integers (including 0), and M is a vector with the [S9n F
% same number of elements as N. Each element k of M must be a s&tr84u|
% positive integer, with possible values M(k) = 0,2,4,...,N(k) ?Ts
Z_
% for N(k) even, and M(k) = 1,3,5,...,N(k) for N(k) odd. R is =+"XV8Fi,
% a vector of numbers between 0 and 1. The output Z is a matrix [hiOFmMJZ-
% with one column for every (N,M) pair, and one row for every ___+5r21\
% element in R. hpw;w}m
% dkVVvK
% Z = ZERNPOL(N,M,R,'norm') returns the normalized Zernike poly- xbmOch}j6
% nomials. The normalization factor Nnm = sqrt(2*(n+1)) is R'80 {
% chosen so that the integral of (r * [Znm(r)]^2) from r=0 to y\%4Dir
% r=1 is unity. For the non-normalized polynomials, Znm(r=1)=1 5N[Y2
% for all [n,m]. 1-b,X]i
% FEP\5d>
% The radial Zernike polynomials are the radial portion of the a<HM|dcst
% Zernike functions, which are an orthogonal basis on the unit y24 0 +;a
% circle. The series representation of the radial Zernike 3yZ@i<rfH
% polynomials is dA_s7),
% /evh .S
% (n-m)/2 oF3#]6`;/
% __ %8$wod6
% m \ s n-2s |Rab'9U^
% Z(r) = /__ (-1) [(n-s)!/(s!((n-m)/2-s)!((n+m)/2-s)!)] * r hz/5k%%UX
% n s=0 =!{dKz-&
% !}vz_6)
% The following table shows the first 12 polynomials. i\PN
% lOEbh
% n m Zernike polynomial Normalization ,qr)}s-
% --------------------------------------------- Cf10 ud
% 0 0 1 sqrt(2) |epe;/
% 1 1 r 2 T8RQM1D_s
% 2 0 2*r^2 - 1 sqrt(6) B)c.`cfr*\
% 2 2 r^2 sqrt(6) VX- f~
% 3 1 3*r^3 - 2*r sqrt(8) %b_zUFHPp
% 3 3 r^3 sqrt(8) lvFHr}W
% 4 0 6*r^4 - 6*r^2 + 1 sqrt(10) g:*yjj
% 4 2 4*r^4 - 3*r^2 sqrt(10) /Ia#udkNMp
% 4 4 r^4 sqrt(10) *F9uv)[kz
% 5 1 10*r^5 - 12*r^3 + 3*r sqrt(12) U}{r.MryFG
% 5 3 5*r^5 - 4*r^3 sqrt(12) .rMGI"
% 5 5 r^5 sqrt(12) +V0uHpm
% --------------------------------------------- TRQva8d?
% +-{HT+W
% Example: DLz~$TF^
% 0_j! t
% % Display three example Zernike radial polynomials g;*~xo
% r = 0:0.01:1; c5]1aFKz
% n = [3 2 5]; f"PApV9[
% m = [1 2 1]; =izB :
% z = zernpol(n,m,r); <2R=!n@b\
% figure z?K+LTf8
% plot(r,z) iKdC2m
% grid on M9i u#6P
% legend('Z_3^1(r)','Z_2^2(r)','Z_5^1(r)','Location','NorthWest') PgxU;N7Y
% Lu<'A4Q1
% See also ZERNFUN, ZERNFUN2. B,A/
-B\
0/*z]2
% A note on the algorithm. 0phGn+"R
% ------------------------ . Bv;Zv
% The radial Zernike polynomials are computed using the series &yP9vp="
% representation shown in the Help section above. For many special |m?0h.O,
% functions, direct evaluation using the series representation can bS0LjvY9g
% produce poor numerical results (floating point errors), because rv\<Q-uQ8
% the summation often involves computing small differences between UyvFR@
% large successive terms in the series. (In such cases, the functions _@HMk"A
% are often evaluated using alternative methods such as recurrence Q#vur o
% relations: see the Legendre functions, for example). For the Zernike he!e~5<@y
% polynomials, however, this problem does not arise, because the .m4K ]^m
% polynomials are evaluated over the finite domain r = (0,1), and 0BBWuNF.
% because the coefficients for a given polynomial are generally all ZOU$do>O
% of similar magnitude. {Ynr(J.
% z43 H]
% ZERNPOL has been written using a vectorized implementation: multiple x2tx{Z
% Zernike polynomials can be computed (i.e., multiple sets of [N,M] WJhI6lu
% values can be passed as inputs) for a vector of points R. To achieve 4sG^bZ,
% this vectorization most efficiently, the algorithm in ZERNPOL qf'uXH
% involves pre-determining all the powers p of R that are required to O! ;!amvz
% compute the outputs, and then compiling the {R^p} into a single ]ErAa"?
% matrix. This avoids any redundant computation of the R^p, and A}W&=m8!
% minimizes the sizes of certain intermediate variables. ;Cv x48
% ?}O\'Fa8
% Paul Fricker 11/13/2006 o^lKM?t
i)eub`uMy
S=o Ab&
% Check and prepare the inputs: F_@PSA+
% ----------------------------- sl`\g1<{`
if ( ~any(size(n)==1) ) || ( ~any(size(m)==1) ) zg "<N
error('zernpol:NMvectors','N and M must be vectors.') Vw+U?
end 5B"j\TwQ
( vgoG5
if length(n)~=length(m) #IgY'L
error('zernpol:NMlength','N and M must be the same length.') 9.>v
;:vL
end XN??^1{J}]
M$|^?U>cm
n = n(:); S _1R]n1/
m = m(:); ^e)KEkh
length_n = length(n); |wWBV{^
yn`H }@`k
if any(mod(n-m,2)) bluhiiATd
error('zernpol:NMmultiplesof2','All N and M must differ by multiples of 2 (including 0).') ~6E
`6;`
end #dU-*wmJ
3>c<E1
if any(m<0) Gi?"
error('zernpol:Mpositive','All M must be positive.') `WX @1]m
end tP7l
;EX4
0~)cAKus
if any(m>n) L%I@HB9-Q0
error('zernpol:MlessthanN','Each M must be less than or equal to its corresponding N.') "1WwSh}Z
end c]#F^(-A`
\M<C6m5
if any( r>1 | r<0 ) e=Kf<ZQt
error('zernpol:Rlessthan1','All R must be between 0 and 1.') 4E<iIA\x
end D&: