MATLAB: Surface plot using fitrpg

fitrgpgaussian process regressionMATLABsurface plot

Dear all,
I tried to make a surface plot of the gaussian process function with the following code:
gprMdl = fitrgp(X,Y1,'KernelFunction','squaredexponential','OptimizeHyperparameters','auto','HyperparameterOptimizationOptions',struct('AcquisitionFunctionName','expected-improvement-plus'));
ypred = predict(gprMdl,X);
[X1,X2] = meshgrid(0.1:0.1:0.9,0.1:0.1:0.9);
surf(X1,X2,reshape(ypred,9,9));
hold on
plot3(X(:,1),X(:,2),Y1,'.r','DisplayName','Observations')
Where the result is presented below.
However, I would expect something more like this. Because the surface plot I have now doesn't fit the data properly and the contour lines are widespread. What am I doing wrong here?
I also tried the following with the help of another post (https://nl.mathworks.com/matlabcentral/answers/407736-plot-3d-hyperplane-from-fitcsvm-results#answer_326570)
But this doens't seem sufficient either.
[x,y]=meshgrid(0.1:0.01:0.9,0.1:0.01:0.9);
xGrid = [x(:),y(:)];
gprMdl = fitrgp(X,Y1,'KernelFunction','squaredexponential','OptimizeHyperparameters','auto','HyperparameterOptimizationOptions',struct('AcquisitionFunctionName','expected-improvement-plus'));
[~,f]=predict(gprMdl2,xGrid);
f=reshape(f(:,1),size(x));
figure
surf(x,y,f)
hold on
plot3(X(:,1),X(:,2),Y1,'.r','DisplayName','Observations')

Best Answer

Hello,
Take a look at the code below, unfortunately I can't find the link where I found an example I took the code from, but look for scatteredInterpolant function. I am not sure whether this is applicable, so, please verify.
clc;
clear;
close all;
load data.mat
gprMdl = fitrgp(X,Y1,'KernelFunction','squaredexponential','OptimizeHyperparameters','auto','HyperparameterOptimizationOptions',struct('AcquisitionFunctionName','expected-improvement-plus'));
ypred = predict(gprMdl,X);
% so I don't write indexes below
x_data = X(:,1);
y_data = X(:,2);
% Getting the 2d grid
Num_Points = 50;
X_Lin = linspace(min(x_data),max(x_data),Num_Points);
Y_Lin = linspace(min(y_data),max(y_data),Num_Points);
[X,Y] = meshgrid(X_Lin,Y_Lin);
% Interpolating the surface from ypred
f = scatteredInterpolant(x_data, y_data, ypred);
Z = f(X,Y);
surf(X,Y,Z);
% plotting the observed points
hold on;
plot3(x_data,y_data, Y1,'.r','DisplayName','Observations');