surface fitting using a self-defined function
13 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am trying to use a self-defined function to fit a surface. This self-defined function is a zernike polynomial function. It's almost working. but I got the error messege saying "Expression zernike_surf_fit(xy, a, b, c, d, e) is not a valid MATLAB expression, has non-scalar coefficients, or cannot be evaluated:"
main function is:
[X, Y] = meshgrid(linspace(-0.8, 0.8, 10), linspace(-0.8, 0.8, 10));
xy=[(X(:)-5.5)/5.5 (Y(:)-5.5)/5.5];
ft = fittype('zernike_surf_fit(xy, a, b, c, d, e)',...
'independent', {'xy'}, 'dependent', {'Z'},...
'coefficients',{'a','b','c','d','e'});
the zernike_surf_fit function is saved under the same file. It looks like below:
function [Z] = zernike_surf_fit(xy,a,b,c,d,e)
Z = zeros(size(xy, 1),1);
X = reshape(xy(:,1),[sqrt(length(xy(:,1))),sqrt(length(xy(:,1)))]);
Y = reshape(xy(:,2),[sqrt(length(xy(:,2))),sqrt(length(xy(:,2)))]);
%[X,Y] = meshgrid(x,y);
[theta,r] = cart2pol(X,Y);
idx = r<=1;
p = 1:5;
z = nan(size(X));
C=[a b c d e];
y = zernfun2(p,r(idx),theta(idx));
z_sum=[];
for k = 1:length(p)
z(idx) = C(k)*y(:,k);
if k==1
z_sum=z;
else
z_sum=z_sum+z;
end
end
Z_temp=reshape(z_sum,[sqrt(length(xy(:,1)))*sqrt(length(xy(:,1))),1]);
Z=Z_temp;
end
The zernike function here can generate zernike surface perfectly. So I think the only problem is the variable a b c d e.
I am really appriciate your help.
2 commentaires
Torsten
le 22 Juin 2023
I suggest you try to call "zernike_surf_fit" with reasonable values for a,...,e and see what it returns for Z. MATLAB does not like the output.
Réponse acceptée
Matt J
le 22 Juin 2023
Modifié(e) : Matt J
le 22 Juin 2023
Your model function is not supposed to assume that the xy samples are drawn from a Cartesian grid lattice.
ft = fittype(@(a,b,c,d,e, x,y)zernike_surf_model([x,y], [a, b, c, d, e]),...
'independent', {'x','y'}, 'dependent', {'Z'},...
'coefficients',{'a','b','c','d','e'})
function [Z] = zernike_surf_model(xy,C)
X = xy(:,1);
Y = xy(:,2);
[theta,r] = cart2pol(X,Y);
idx = r<=1;
Z = nan(size(X));
Z(idx)=zernfun2(1:numel(C),r(idx),theta(idx)) * C(:);
end
1 commentaire
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Zernike Polynomials dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!