Hi Nick,
As per my understanding, a possible cause of the issue is that fitting both amplitudes and wavenumbers for a sum of 2D sine and cosine modes leads to an overcomplicated and ill-conditioned problem, especially when the wavenumbers are already known. In such cases, it is both more robust and efficient to fit only the amplitudes, while keeping the wavenumbers fixed.
If the wavenumbers that create the surface are known, the amplitudes can be found out using MATLAB's 'fit' function.This makes the fitting easier and more accurate.
Suppose you have a surface defined as:
V = Asin(kxx + kyy) + Bcos(kxx + kyy);
%where A, B, kx, and ky are constants, and x, y are grid coordinates.
To fit for amplitudes A and B (with known kx and ky):
% Prepare data
xData = X(:);
yData = Y(:);
zData = V(:);
% Define the fit type with fixed wavenumbers
ft = fittype('A*sin((2*pi/L)*kx*x + (2*pi/L)*ky*y) + B*cos((2*pi/L)*kx*x + (2*pi/L)*ky*y)', ...
'independent', {'x', 'y'}, 'dependent', 'z', ...
'problem', {'kx', 'ky', 'L'});
% Fit the model
[fitresult, gof] = fit([xData, yData], zData, ft, 'problem', {3, 2, 10}, 'StartPoint', [1, 1]);
% Display results
disp(fitresult);
By specifying kx, ky, and L as known values (the wavenumbers and domain size), the fit only estimates A and B. This approach is accurate and avoids the instability of trying to fit for wavenumbers.
For a better understanding of the above solution, refer to the following MATLAB documentations:
3.non-linear least squares: https://www.mathworks.com/help/optim/nonlinear-least-squares-curve-fitting.html
