Hi Laura,
I understand that you are trying to fit a surface to your X, Y, Z data points using a polynomial of degree 2 in both X and Y, and you want to ensure that this fitted surface is contained within a cube with edges ranging from 0 to 1 in all three directions. To achieve this, you should normalize your data points so that they fit within this cube before applying the fitting process. Here is how you can do it in MATLAB:
% Your original data
X = [0.1; 0.05; 0.5; 0.5; 0.05; 0.4];
Y = [2.8; 5.5; 5.5; 1.4; 1.4; 2];
Z = [0.9; 0.3; 0; 0.2; 0.6; 1];
% Normalize the X, Y, and Z data to the range [0, 1]
X_norm = (X - min(X)) / (max(X) - min(X));
Y_norm = (Y - min(Y)) / (max(Y) - min(Y));
Z_norm = (Z - min(Z)) / (max(Z) - min(Z));
% Fit the surface to the normalized data
fs_norm = fit([X_norm, Y_norm], Z_norm, 'poly22');
% Plotting the normalized data and the fitted surface
figure;
plot3(X_norm, Y_norm, Z_norm, 'or'); % Plot the normalized data points
hold on;
% Plot the fitted surface
fs_plot = plot(fs_norm, [X_norm, Y_norm], Z_norm);
zlim([0 1]); % Ensure the Z-axis is limited to [0, 1]
xlabel('Normalized X');
ylabel('Normalized Y');
zlabel('Normalized Z');
title('Normalized Fitted Surface');
This code normalizes your X, Y, and Z data to be within the range of 0 to 1, fits a polynomial surface to the normalized data, and then plots both the normalized data points and the fitted surface. The normalization ensures that your fitted surface will be contained within a cube with edges from 0 to 1 in the X, Y, and Z directions as desired.
Hope this helps.
Regards,
Nipun