a 2-dimension function example 1
DESCRIPTION
A 2-dimension function Example 1. fa_2d.m. fstr=input('input a 2D function: x1.^2+x2.^2+cos(x1) :','s'); fx=inline(fstr); range=2*pi; x1=-range:0.1:range; x2=x1; for i=1:length(x1) C(:,i)=fx(x1(i),x2); end mesh(x1,x2,C);. Sampling training data. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: A 2-dimension function Example 1](https://reader036.vdocuments.us/reader036/viewer/2022070415/56814f60550346895dbd15c9/html5/thumbnails/1.jpg)
A 2-dimension functionExample 1
fstr=input('input a 2D function: x1.^2+x2.^2+cos(x1) :','s');fx=inline(fstr);range=2*pi;x1=-range:0.1:range;x2=x1;for i=1:length(x1) C(:,i)=fx(x1(i),x2);endmesh(x1,x2,C);
fa_2d.m22
2121 ),( xxxxf
![Page 2: A 2-dimension function Example 1](https://reader036.vdocuments.us/reader036/viewer/2022070415/56814f60550346895dbd15c9/html5/thumbnails/2.jpg)
Sampling training dataN=input('input the number of sample:');x=rand(2,N)*4*pi-2*pi;y=fx(x(1,:),x(2,:));y=y+rand(1,N)*3-1.5;N=length(x); ind=randperm(N); N2=floor(N/2); X_TRAIN=x(:,ind(1:N2)); Y_TRAIN=y(ind(1:N2)); figureplot3(X_TRAIN(1,:), X_TRAIN(2,:), Y_TRAIN,'.')
![Page 3: A 2-dimension function Example 1](https://reader036.vdocuments.us/reader036/viewer/2022070415/56814f60550346895dbd15c9/html5/thumbnails/3.jpg)
Step 1 Searching means
K=20;figureplot(X_TRAIN(1,:),X_TRAIN(2,:),'b.')[cind,center]=kmeans(X_TRAIN',K);hold onplot(center(:,1),center(:,2),'rP')
• Kmeans
![Page 4: A 2-dimension function Example 1](https://reader036.vdocuments.us/reader036/viewer/2022070415/56814f60550346895dbd15c9/html5/thumbnails/4.jpg)
Step 2 Cross Distances
Cross distances between centers and data points
D(i,j) stores the distance between the ith data point and the jth center
D = cross_distance(X_TRAIN',center);
![Page 5: A 2-dimension function Example 1](https://reader036.vdocuments.us/reader036/viewer/2022070415/56814f60550346895dbd15c9/html5/thumbnails/5.jpg)
Step 3 Posterior weights Radial basis function
![Page 6: A 2-dimension function Example 1](https://reader036.vdocuments.us/reader036/viewer/2022070415/56814f60550346895dbd15c9/html5/thumbnails/6.jpg)
Approximating function
)/exp(
)/:),(:),(exp(
:)),(()(
point dataith theS
1
2
1
hda
hkcenterixa
ixfiy
ubstitute
ik
K
kk
K
kk
![Page 7: A 2-dimension function Example 1](https://reader036.vdocuments.us/reader036/viewer/2022070415/56814f60550346895dbd15c9/html5/thumbnails/7.jpg)
![Page 8: A 2-dimension function Example 1](https://reader036.vdocuments.us/reader036/viewer/2022070415/56814f60550346895dbd15c9/html5/thumbnails/8.jpg)
![Page 9: A 2-dimension function Example 1](https://reader036.vdocuments.us/reader036/viewer/2022070415/56814f60550346895dbd15c9/html5/thumbnails/9.jpg)
h=40;a=pinv(exp(-D/h))*Y_TRAIN';
![Page 10: A 2-dimension function Example 1](https://reader036.vdocuments.us/reader036/viewer/2022070415/56814f60550346895dbd15c9/html5/thumbnails/10.jpg)
Approximating functionX_TRAN
D = cross_distance(X_TRAIN',center);
Y_HAT=exp(-D/h)*a;
Y_HAT
centers
a, h
figureplot3(X_TRAIN(1,:),X_TRAIN(2,:),Y_TRAIN,'.')hold onplot3(X_TRAIN(1,:),X_TRAIN(2,:),Y_HAT,'rx')
![Page 11: A 2-dimension function Example 1](https://reader036.vdocuments.us/reader036/viewer/2022070415/56814f60550346895dbd15c9/html5/thumbnails/11.jpg)
Form X_testrange=2*pi;x1=-range:0.5:range;x2=x1;X_test=[ ];for i=1:length(x1) xx = [x1(i)*ones(1,length(x2));x2]; X_test=[X_test xx];endfigureplot(X_test(1,:),X_test(2,:),'.')
![Page 12: A 2-dimension function Example 1](https://reader036.vdocuments.us/reader036/viewer/2022070415/56814f60550346895dbd15c9/html5/thumbnails/12.jpg)
X_test
D = cross_distance(X_test',center);
Y_test=exp(-D/h)*a;
Y_test
centers
a, h
figureCC=reshape(Y_test,length(x1),length(x1));mesh(x1,x2,CC);
![Page 13: A 2-dimension function Example 1](https://reader036.vdocuments.us/reader036/viewer/2022070415/56814f60550346895dbd15c9/html5/thumbnails/13.jpg)
A 2-dimension functionExample 2
fstr=input('input a 2D function: x1.^2+x2.^2+cos(x1) :','s');fx=inline(fstr);range=2*pi;x1=-range:0.1:range;x2=x1;for i=1:length(x1) C(i,:)=fx(x1(i),x2);endmesh(x1,x2,C);
22
2121 ),( xxxxf
![Page 14: A 2-dimension function Example 1](https://reader036.vdocuments.us/reader036/viewer/2022070415/56814f60550346895dbd15c9/html5/thumbnails/14.jpg)
K=20,h=40
Y_HAT=exp(-D/h)*a;
Input X_TRAIN, K, h
D = cross_distance(X_TRAIN',center);a=pinv(exp(-D/h))*Y_TRAIN';
[cind,center]=kmeans(X_TRAIN',K);
![Page 15: A 2-dimension function Example 1](https://reader036.vdocuments.us/reader036/viewer/2022070415/56814f60550346895dbd15c9/html5/thumbnails/15.jpg)
Form X_testrange=2*pi;x1=-range:0.5:range;x2=x1;X_test=[ ];for i=1:length(x1) xx = [x1(i)*ones(1,length(x2));x2]; X_test=[X_test xx];endfigureplot(X_test(1,:),X_test(2,:),'.')
![Page 16: A 2-dimension function Example 1](https://reader036.vdocuments.us/reader036/viewer/2022070415/56814f60550346895dbd15c9/html5/thumbnails/16.jpg)
figureY_test=exp(-D/h)*a;CC=reshape(Y_test,length(x1),length(x1));mesh(x1,x2,CC);
Input X_test, a, h D = cross_distance(X_test',center);