% c=rbf_r2(x,f)
% input: x is a vector of 1D positions of data points
%        f is a vector of 1D data values to interpolate
% output: c is the vector of coefficients of the RBF interpolant

function c=rbf_r2(x,f)

n=length(x);

% form of the interpolant
% f(p) = c(1)*phi(p-x(1)) + ... + c(n)*phi(p-x(n)) + c(n+1)

% so the linear system is:
A=zeros(n+1,n+1);
for i=1:n
    for j=1:n
        A(i,j)=phi(x(i)-x(j));
    end
    A(i,n+1)=1;
end
A(n+1,1:n)=1;

b=zeros(n+1,1);
b(1:n)=f(:);

% solve for the coefficients
c=A\b;


% sub fucntion to define the RBF kernel
function y=phi(r)
y=r.^2;

        
