function z = zernpol(n,m,r,nflag) 4_$f"6
%ZERNPOL Radial Zernike polynomials of order N and frequency M. --FvE|I
% Z = ZERNPOL(N,M,R) returns the radial Zernike polynomials of ^-DK<jZ^
% order N and frequency M, evaluated at R. N is a vector of "xWC49
% positive integers (including 0), and M is a vector with the 4R6X"T9-
% same number of elements as N. Each element k of M must be a bbz86]AhY
% positive integer, with possible values M(k) = 0,2,4,...,N(k) m|!sY[!
% for N(k) even, and M(k) = 1,3,5,...,N(k) for N(k) odd. R is I)clGMS,
% a vector of numbers between 0 and 1. The output Z is a matrix 7\.5G4dr%
% with one column for every (N,M) pair, and one row for every epQ7@9,Q
% element in R. K.z@Vx.
% # aC}\
% Z = ZERNPOL(N,M,R,'norm') returns the normalized Zernike poly- -Jb
I7Le
% nomials. The normalization factor Nnm = sqrt(2*(n+1)) is i+OyBDkJM!
% chosen so that the integral of (r * [Znm(r)]^2) from r=0 to kY|<1Ht
% r=1 is unity. For the non-normalized polynomials, Znm(r=1)=1 #N*~Q
% for all [n,m]. 'SC`->F4D
% N7|ctO
% The radial Zernike polynomials are the radial portion of the t ,0~5>5
% Zernike functions, which are an orthogonal basis on the unit qu?D`29
% circle. The series representation of the radial Zernike !+i
% polynomials is f+rBIE
% "F=O
% (n-m)/2 W)KV"A3C
% __ \hg12],#:@
% m \ s n-2s ur;8uv2o
% Z(r) = /__ (-1) [(n-s)!/(s!((n-m)/2-s)!((n+m)/2-s)!)] * r STO6cNi
% n s=0 4]Krx
m`8
% %.]qkGZe#
% The following table shows the first 12 polynomials. 8kk$:8
% K1Uur>Pk%
% n m Zernike polynomial Normalization d35 ,[
% --------------------------------------------- S^ 3I" B
% 0 0 1 sqrt(2) zH.7!jeE
% 1 1 r 2 a4c~ThbI
% 2 0 2*r^2 - 1 sqrt(6) }psJ'aiG*
% 2 2 r^2 sqrt(6) U`xjau+
% 3 1 3*r^3 - 2*r sqrt(8) 'En 6h" {
% 3 3 r^3 sqrt(8) f"z96{zo
% 4 0 6*r^4 - 6*r^2 + 1 sqrt(10) Nx~8]h1(
% 4 2 4*r^4 - 3*r^2 sqrt(10) =YR/|9(
% 4 4 r^4 sqrt(10) (R{WJjj
% 5 1 10*r^5 - 12*r^3 + 3*r sqrt(12) tip\vS)
% 5 3 5*r^5 - 4*r^3 sqrt(12) =^NR(:SaaU
% 5 5 r^5 sqrt(12) g^=p)h3
% --------------------------------------------- >=wlS\:"
% KATt9ox@
% Example: 23zB@aE_?1
% QD<f)JZK
% % Display three example Zernike radial polynomials JBp^@j{_
% r = 0:0.01:1; OX I.>9
% n = [3 2 5]; rlgp1>89
% m = [1 2 1]; Ue!
&Vm
% z = zernpol(n,m,r); 0m!+gZ@
% figure JW
(.,Ztm
% plot(r,z) =}F &jl
% grid on 0:Xvch0
% legend('Z_3^1(r)','Z_2^2(r)','Z_5^1(r)','Location','NorthWest') awGI|d
% S-*4HV_l
% See also ZERNFUN, ZERNFUN2. L'.7V ~b{
LJ9^:U
% A note on the algorithm. +Uq$'2CT
% ------------------------ 0KE+RzrB
% The radial Zernike polynomials are computed using the series Ng2qu!F7
% representation shown in the Help section above. For many special \IIR2Xf,K
% functions, direct evaluation using the series representation can >k5nU^|B1
% produce poor numerical results (floating point errors), because YhRES]^
% the summation often involves computing small differences between CM_FF:<tn
% large successive terms in the series. (In such cases, the functions [?^,,.Dd
% are often evaluated using alternative methods such as recurrence `$7.(.#s
% relations: see the Legendre functions, for example). For the Zernike m\RU|Z
% polynomials, however, this problem does not arise, because the
\}Z5}~S
% polynomials are evaluated over the finite domain r = (0,1), and /{6PwlP5
% because the coefficients for a given polynomial are generally all JdF;*`_7*
% of similar magnitude. <`}Oi5nW
% j@
lHgis
% ZERNPOL has been written using a vectorized implementation: multiple e<#t]V
% Zernike polynomials can be computed (i.e., multiple sets of [N,M] unKi)v1
% values can be passed as inputs) for a vector of points R. To achieve vWc =^tT
% this vectorization most efficiently, the algorithm in ZERNPOL 8HDYA$L
% involves pre-determining all the powers p of R that are required to _SY4Qs`d
% compute the outputs, and then compiling the {R^p} into a single R5(<:]
% matrix. This avoids any redundant computation of the R^p, and yHsmX2s
% minimizes the sizes of certain intermediate variables. 9ePG-=5I
% gs7h`5[es
% Paul Fricker 11/13/2006 ~dg7c{o5
Cz` !j
Bvb.N$G
% Check and prepare the inputs: Dk[m)]w\
% ----------------------------- BIqZg$
if ( ~any(size(n)==1) ) || ( ~any(size(m)==1) ) Y[#EFM
error('zernpol:NMvectors','N and M must be vectors.') ;EDc1:
end ..'k+0u^
ge
%ytrst
if length(n)~=length(m) 5[suwaJQ
error('zernpol:NMlength','N and M must be the same length.') F%M4i`Vh
end 2iO AUo+
FxeDjAP
n = n(:);
+uZ,}J
m = m(:); o`,|{K$H
length_n = length(n); |*W_
d^p af
if any(mod(n-m,2)) bk^W]<:z`
error('zernpol:NMmultiplesof2','All N and M must differ by multiples of 2 (including 0).') B >2" O
end :p%G+q2
3c#oK
if any(m<0) iZm#
"}VG
error('zernpol:Mpositive','All M must be positive.') P@lDhzd
end J)tk<&X
}ya@*jH
if any(m>n) >ka*-8?
error('zernpol:MlessthanN','Each M must be less than or equal to its corresponding N.') 4IfOvAN%
end `<_A#@
P5-1z&9O
if any( r>1 | r<0 ) $v5)d J
error('zernpol:Rlessthan1','All R must be between 0 and 1.') [&y