dgp icon indicating copy to clipboard operation
dgp copied to clipboard

[1% Bonus] (1D) RBF Fitting in Matlab

Open taiya opened this issue 7 years ago • 5 comments

A 1% bonus to the the first person that replies to this message with a correct Matlab implementation (you can use the same sample data as for the other LS exercise) of what was described in class (and in the image below).

You should generate both of the highlighted images and paste them in your reply. You can embed images and code directly in the message, google to find out how.

If you post a solution that is not correct and a fellow student fixes it, it is considered fair game. Therefore, be absolutely sure your solution is correct.

screenshot 2016-09-30 12 01 47

taiya avatar Sep 30 '16 19:09 taiya

Figure 1: the function phi(x) has a peak value of 1. The centers (c_i) are varying from min(t) to max(t) by a step change of 0.2 . 1

Figure 2: The fitting 2

CODE:

% phi_x.m
function out = phi_x(x)
    flags = abs(x)<1;
    out = (ones(size(x)) - x.^2).^4;
    out = out.*single(flags);
end
% rbf.m
clc;clear;close all;
load lsopt.mat;
figure(1); 
plot(t,f,'.r'); 
axis equal; 
xlabel('t'); 
ylabel('f(t)');

sc = 0.2;
centers = [min(t):sc:max(t)];

A = repmat(t',1,size(centers,2));

for i=1:size(centers,2),
    A(:,i) = A(:,i)-centers(i)*ones(size(A(:,i)));
end

A = phi_x(A);
b = f';

x = A\b;

hold on;
for i=1:size(t,2),
    ft(i) = x'*phi_x(t(i)*ones(size(centers))-centers)';
end
plot(t,ft);
hold off;

figure(2);
plot(t,f,'.r'); 
axis equal; 
xlabel('t'); 
ylabel('f(t)');
hold on;

px = -max(t):0.01:max(t);
py = phi_x(px);

for i = 1:size(centers,2),
    plot(centers(i),0,'*k');
    plot([centers(i),centers(i)],[0,1],'-.r');
    plot(px+centers(i),py);
end
xlim([min(t) max(t)]);

r4ghu avatar Sep 30 '16 20:09 r4ghu

Shouldn't there be only 4 rbfs fitting all the data? Here's my solution:

The vertical scales are:
0.1002 0.3673 0.0271 0.5330

(The thick blue line is the fitting.)

rbf_fitting_2

Code:

clc, clear, close all;

load lsopt.mat;
figure(1); 
plot(t,f,'.r'); 
axis equal; 
xlabel('t'); 
ylabel('f(t)');
Row = @(t)([t.^3 t.^2 t.^1 1]);


A = zeros(size(t, 2), 4);
C = linspace(min(t), max(t), 4);
for i = 1:size(t, 2)
    for j = 1:4
        n = norm(t(i) - C(j));
        if n < 1
            A(i, j) =  (1 - n^2)^4;
        else
            A(i, j) = 0;
        end
    end
end

a = (inv(A'*A))*A' * f';

s = linspace(min(t), max(t), 1000);
V = zeros(1000, 1);
for j = 1:1000
    s1 = a(1)*rbf_f(norm(s(j) - C(1)));
    s2 = a(2)*rbf_f(norm(s(j) - C(2))); 
    s3 = a(3)*rbf_f(norm(s(j) - C(3)));
    s4 = a(4)*rbf_f(norm(s(j) - C(4)));
    V(j) = s1 + s2 + s3 + s4;
end
hold on;
plot(s, V, '-b', 'LineWidth', 5);
hold on;
%{
plot rbf1 centered at c1
%}
R1 = zeros(1000, 1)
for j = 1:1000
    R1(j) = rbf_f(norm(s(j) - C(1)));
end
hold on
plot(s, R1, '-y');
hold on
%{
plot rbf2 centered at c2
%}
R2 = zeros(1000, 1)
for j = 1:1000
    R2(j) = rbf_f(norm(s(j) - C(2)));
end
hold on
plot(s, R2, '-m');

hold on;
%{
plot rbf3 centered at c3
%}
R3 = zeros(1000, 1)
for j = 1:1000
    R3(j) = rbf_f(norm(s(j) - C(3)));
end
hold on
plot(s, R3, '-c');
hold on
%{
plot rbf4 centered at c4
%}
R4 = zeros(1000, 1)
for j = 1:1000
    R4(j) = rbf_f(norm(s(j) - C(4)));
end
hold on
plot(s, R4, '-g');

in a different file:

function r = rbf_f(t)
    if t < 1 && t > -1
        r = (1 - t^2)^4;
    else
        r = 0;
    end
end

rayhanrahman avatar Sep 30 '16 21:09 rayhanrahman

@rayhanrahman It depends on the value of your step change(variable 'sc' in my matlab code). In my case, I set sc = 0.2 which created 8 centers for finding the weights. In your case, the sc=0.4 which created 4 centers. With the increase in the number of centers, the accuracy of fitting increases. We just have to make sure that it is neither over-fitting nor under-fitting. In your case, the precision of the fitting isn't that much accurate if you observe the range t=[0.6,1].

r4ghu avatar Sep 30 '16 21:09 r4ghu

@r4ghu Of course, fitting would be more accurate with higher number of RBFs. Look at Andrea's pseudo code and his drawings -- exactly 4 RBFs!!! And, that's what I thought Andrea is asking ;)

rayhanrahman avatar Sep 30 '16 21:09 rayhanrahman

@ataiya The last subscript of the column of y in the matrix equation of the linear system should be "n" instead of what appears to be "3".

rayhanrahman avatar Sep 30 '16 21:09 rayhanrahman