# MA 5324 Problem 1.7

basic meshfree interpolation with Gaussian RBFs

## (a) interpolation

% constructing the interpolant
f = @(x) 4*x.*(1-x);
phi = @(r) exp(-(r).^2);
% psi = @(r) exp(-1./(1-(min(r,1).^2)));
ep = 1;
N = 5;
X = linspace(0,1,N)';
y = f(X);
D = phi(ep*distm(X,X));
c = D\y;
% evaluating it
M = 50;
Xt = linspace(0,1,M)';
Dt = phi(ep*distm(Xt,X));
yt = Dt*c;
% plotting the function and its interpolant
clf; plot(X,0,'bo',Xt,f(Xt)); hold on
plot(Xt,yt,'rx-'); xlabel('$x$'); ylabel('$f, P_f$ ')


## (b) comparison with euclidean basis functions

ep = 12;
M = 1000;
Xt = linspace(0,1,M)';
N = logspace(1,2.8,20);
for k=1:20,
X = linspace(0,1,N(k))';
y = f(X);
D1 = distm(X,X); c1 = D1\y;
D2 = phi(ep*distm(X,X)); c2 = D2\y;
%    D3 = psi(ep*distm(X,X)); c3 = D3\y;
D_eval1 = distm(Xt,X);
D_eval2 = phi(ep*distm(Xt,X));
%    D_eval3 = psi(ep*distm(Xt,X));
err_1(k) = norm(D_eval1*c1 - f(Xt))/sqrt(M);
err_2(k) = norm(D_eval2*c2 - f(Xt))/sqrt(M);
%    err_3(k) = norm(D_eval3*c3 - f(Xt))/sqrt(M);
end
clf;
loglog(N,err_1,'o-',N,err_2,'ro-');
%loglog(N,err_1,'o-',N,err_2,'ro-',N,err_3,'go-');
xlabel('$N$'); ylabel('errors'); legend('euclidean distance RBFs','Gaussian RBFs')


## (c) error of Gaussian RBF interpolant in dependence of the shape parameter

N = 2^4;
X = linspace(0,1,N)';
Dp = distm(X,X);
y = f(X);
Xt = linspace(0,1,200)';
Dpt = distm(Xt,X);
ep = logspace(-1,2,40);
for k = 1:40,
D = phi(ep(k)*Dp);
c = D\y;
Dt = phi(ep(k)*Dpt);
err(k) = norm(Dt*c - f(Xt))/sqrt(M);
end
clf; loglog(ep,err,'o-');
xlabel('$\epsilon$'); ylabel('error');