
% load your data file here

% plot data to get an idea of how the function looks; it's best to use the "structured"
% representation for this
mesh(id.X{1}, id.X{2}, id.Y');  
% note the transpose in Y, this is because of the way that mesh expects to receive the data

% the RBF approximator creation takes as inputs: 
% a) the limits of the domain as an input; one way to compute the limits from the ID data is:
limx = minmax(id.Xflat);
% (the validation data will stay within the same limits for the examples in the lab) 
% b) the number of basis functions on each dimension
% say we want 6 RBFs on each dimension (all examples are two-dimensional)
N = [6 6];
    
app = rbfapprox(limx, N);
    
% you need to create a matrix of regressors (RBF values) and a vector of output values, see the lecture

% to get the vector of RBF values at input point k, you can write:
phi = app.phi(app, id.Xflat(:, k));

% once you have the regressors matrix and the output vector, solve the linear system using \

% assuming your parameter vector is called theta, you can now evaluate the approximator at any
% validation point
% e.g. using the "flat" representation at point k:
yhat = app.eval(app, val.Xflat(:, k), theta);
% or using the "structured" representation at point (i, j):
yhat = app.eval(app, [val.X{1}(i); val.X{2}(j)], theta);
    
% now compute an MSE, plot the approximate values, and compare them on the graph with the true
% function values

% if you have computed a flat vector of approximate outputs Yhat for the validation data, the
% following instruction gets it into an appropriate shape for using mesh
Yhat = reshape(Yhat, val.dims);     % val.dims stores the "structured" size of the data