lsqcurvefit Surface Fitting Troubleshooting
Afficher commentaires plus anciens
I am trying to write a script that fits any given surface using 2D polynomials. I am following the answer given in:
I am testing this by creating a surface using known polynomial coefficients, and then I can see if my script comes up with the correct coefficients through fitting it. Right now I'm using 9 coefficients. It seems my fitting works for the first three coefficients, but it ignores coefficients four through nine, and I cannot figure out why.
%% Setup
x = linspace(-100,100);
y = linspace(-100,100);
[X, Y] = meshgrid(x,y);
XY(:,:,1) = X; XY(:,:,2) = Y;
C = [1 0 1 1 0 0 0 0 0]; %coefficients solution
Degree = 3;
Z = zeros(size(X)); %create original surface
idx = 1;
%define Z
for j = 1:Degree
for k = 0:j
Z = Z + C(idx) * (X.^(j-k)) .* (Y.^k);
idx = idx+1;
end
end
%% Surface fitting
% (from https://www.mathworks.com/matlabcentral/answers/119001-2d-data-fitting-surface)
poly_fun = @(c,XY) c(1)*XY(:,:,1).^1 + c(2)*XY(:,:,2).^1 + c(3)*XY(:,:,1).^2 + c(4)*XY(:,:,1).^1*XY(:,:,2).^1 + c(5)*XY(:,:,2).^2 + c(6)*XY(:,:,1).^3 + c(7)*XY(:,:,1).^2*XY(:,:,2).^1 + c(8)*XY(:,:,1).^1*XY(:,:,2).^2 + c(9)*XY(:,:,2).^3;
Cfit = lsqcurvefit(poly_fun, zeros(1,length(C)), XY, Z);
Zfit = zeros(size(X)); %Create fitted surface
idx = 1;
for j = 1:Degree
for k = 0:j
Zfit = Zfit + Cfit(idx) * (X.^(j-k)) .* (Y.^k);
idx = idx+1;
end
end
%% Plotting
figure
surf(Z,'LineStyle','none')
title('Original Surface')
figure
surf(Zfit,'LineStyle','none')
title('Polynomial Fit Surface')
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Fit Postprocessing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

