Here I give an example about the function approximation technique in MATLAB, than can be used in many areas, e.g., machine learning, robotics, artificial intelligence, optimization. The function approximation in this tutorial is accomplished using Radial Basis Networks. There are 5 simple steps that I explain individually.
1- first we make a sine wave with variable domain that will be the target of function approximation. (This is similar to MATLAB documentation)
X=-1:0.1:1; T1 = [-.9602 -.5770 -.0729 .3771 .6405 .6600 .4609 ... .1336 -.2013 -.4344 -.5000 -.3930 -.1647 .0988 ... .3072 .3960 .3449 .1816 -.0312 -.2189 -.3201];
2- to make it different from the MATLAB example, I add the same wave in reverse to the data. This makes the function approximation task more interesting.
X=-1:0.1:3.1; T2 = T1(1,end:-1:1); T = [T1 T2];
3- I want to start from a number of points, not all of them (say the 10 first points), accomplish the function approximation and then add a new point each time and update the function approximation. This would help to understand how we can use function approximation inside an algorithm at each iteration. So we have to define the followings:
eg = 0.02; % sum-squared error goal sc = 1; % spread constant o = 10; % number of points to start from iii = 1; % counter-1 jjj = 2; % counter-2 c = hsv(size(T,2)); h = figure;
4- Now the main loop is as follows:
for ii=1:size(T,2)-o+1 TT = T(1,1:o+ii); % add one point at each iteration XX = X(1,1:o+ii); % add one point at each iteration figure(h);plot(XX,TT,'+','color',c(ii,:));hold on saveas(h,['x' num2str(iii) '.png']); % save figure iii = iii + 1; net = newrb(XX,TT,eg,sc); % train a radial basis network Y = net(XX); % use the trained network figure(h);plot(XX,Y,'color',c(ii,:));hold on saveas(h,['x' num2str(jjj) '.png']); jjj = jjj + 1; end
5- the result can be seen in the following animated Gif: