Amplitude of periodic orbit
function [data, y] = po_amp(prob, data, u)
hatr = data.hatr;
cind = data.cind;
dind = data.dind;
m = size(cind,2);
coeffs = data.coeffs;
nhatr = numel(hatr.val);
optdof = data.optdof;
ndofs = numel(optdof);
whatr = zeros(ndofs,nhatr);
isnonauto = data.isnonauto;
switch data.coordinates
case 'polar'
rho = u(1:2:2*m-1);
th = u(2:2:2*m);
for i=1:nhatr
idx = hatr.pos{i};
ci = cind(idx,:); di = dind(idx,:);
ang = (ci-di)*th;
rhopower = rho.^((ci+di)');
pdrho = prod(rhopower,1);
whatri = coeffs(:,idx).*(pdrho.*exp(1i*ang'));
whatr(:,i) = sum(whatri,2);
end
case 'cartesian'
zRe = u(1:2:2*m-1);
zIm = u(2:2:2*m);
zcomp = zRe+1i*zIm;
zconj = conj(zcomp);
for i=1:nhatr
idx = hatr.pos{i};
ci = cind(idx,:)'; di = dind(idx,:)';
zk = prod(zcomp.^ci.*zconj.^di,1);
whatri = coeffs(:,idx).*zk;
whatr(:,i) = sum(whatri,2);
end
otherwise
error('Optional coordinates: polar and cartesian');
end
if isnonauto
omega = u(2*m+1); epf = u(2*m+2);
x0 = (1i*data.kappa*omega*data.B-data.A)\data.Flead;
if data.isbaseForce; x0 = x0.*omega.^2; end
idplus = find(hatr.val==1);
whatr(:,idplus) = whatr(:,idplus)+epf*x0(optdof);
idminus = find(hatr.val==-1);
whatr(:,idminus) = whatr(:,idminus)+epf*conj(x0(optdof));
end
if data.isl2norm
y = sqrt(real(sum(sum(conj(whatr).*(data.Q*whatr)))));
else
omega = u(2*m+1);
t = u(end);
hatrval = full(hatr.val');
y = exp(1i*hatrval*omega*t);
y = sum(whatr.*y);
y = real(y);
end
end