修正代码:clear x= linspace(-2, 2, 100); y= linspace(-2, 2, 100); nx=length(x); ny=length(y); for i=1:nx for j=1:ny f(i,j)=franke([x(i) y(j)]); end end figure(1) mesh(x,y,f) title('原始曲面') k=1; r1=linspace(-2, 2, 10); s1=linspace(-2, 2, 10); for i=1:length(r1) for j=1:length(s1) M(k,:)=[r1(i) s1(j)];%M中是均匀采样点的坐标 k=k+1; end end m=size(M,1); for k=1:m MV(k)=franke(M(k,:));%采样点相应的函数值 end figure(4)%画出均匀采样点 for i=1:m plot(M(i,1),M(i,2),'.') hold on end title('均匀采样点') %计算系数矩阵 %RBF sigma=0.2; x_g=zeros(100,100); for j=1:100 for k=1:100 dist=sqrt((r1(j)-r1(k))^2+(s1(j)-s1(k))^2); x_g(j,k)=exp(-(dist)/2*sigma^2);%gauss %x_g(j,k)=((dist)^2+1)^0.5; end end d=x_g\MV';
时间: 2023-11-16 07:07:53 浏览: 81
Python入门教程优质程序填空阅读填空程序试题.doc
修正后的代码如下:
```matlab
clear
x = linspace(-2, 2, 100);
y = linspace(-2, 2, 100);
nx = length(x);
ny = length(y);
for i = 1:nx
for j = 1:ny
f(i,j) = franke([x(i) y(j)]);
end
end
figure(1)
mesh(x,y,f)
title('原始曲面')
k = 1;
r1 = linspace(-2, 2, 10);
s1 = linspace(-2, 2, 10);
for i = 1:length(r1)
for j = 1:length(s1)
M(k,:) = [r1(i) s1(j)]; % M中是均匀采样点的坐标
k = k+1;
end
end
m = size(M,1);
figure(2)
for i = 1:m
plot(M(i,1),M(i,2),'.')
hold on
end
title('均匀采样点')
% 计算系数矩阵
% RBF sigma=0.2;
x_g = zeros(m,m);
for j = 1:m
for k = 1:m
dist = sqrt((M(j,1)-M(k,1))^2 + (M(j,2)-M(k,2))^2);
x_g(j,k) = exp(-(dist)^2/(2*sigma^2)); % gauss
% x_g(j,k) = ((dist)^2+1)^0.5;
end
end
d = x_g\MV';
```
修正点:
1. 修正了计算系数矩阵时,距离计算公式的错误。
2. 修正了画均匀采样点的代码错误,将 figure(4) 改为 figure(2)。
阅读全文