将代码转化为画二维图形的代码:x0=zeros(100,1)'; y0=zeros(100,1)'; z0=zeros(100,1)'; k=1 x1= linspace(-2, 2, 10); y1= linspace(-2, 2, 10); for i = 1:length(x1) for j = 1:length(y1) M(k,:) = [x1(i) y1(j)]; % M中是均匀采样点的坐标 k = k+1; end end x0 = M(:, 1); % 提取 M 的第一列,即 x1 列 y0 = M(:, 2); % 提取 M 的第二列,即 y1 列 for i = 1:100 z0(i) = sin(pix0(i)/2) + cos(pix0(i)/3); end figure(1); plot3(x0,y0,z0,''); %RBF sigma=0.2; x_g=zeros(100,100); for j=1:100 for k=1:100 dist=sqrt((x0(j)-x0(k))^2+(y0(j)-y0(k))^2); x_g(j,k)=exp(-(dist)/2sigma^2);%gauss %x_g(j,k)=((dist)^2+1)^0.5; end end w=x_g\z0'; x_hat = linspace(-2, 2, 100); y_hat = linspace(-2, 2, 100); z_g=zeros(length(x_hat),length(x_hat)); for p=1:length(x_hat) for n=1:length(x_hat) for m=1:100 dist2=sqrt((x0(m)-x_hat(p))^2+(y0(m)-y_hat(n))^2); f=w(m)exp(-(dist2)/2sigma^2);%gauss %f=w(m)*((dist2)^2+1)^0.5; z_g(p,n)=z_g(p,n)+f; end z_real=x_hat(p)exp(-x_hat(p)^2+y_hat(n)); end end figure(2) mesh(x_hat,y_hat,z_g'); %set(gcf, 'Renderer', 'ZBuffer'); axis([-2 2 -2 2 -2 2]); E_max=max(max(abs(z_g-z_real))); E_avg=mean(mean(abs(z_g-z_real))); fprintf('均方根误差最大值为 %.4f\n', E_max) fprintf('均方根误差平均值为 %.4f\n', E_avg) x = [1]; y1 = E_max; y2 =E_avg; % 绘制第一组数据的图形,使用红色实线 figure(3) plot(x, y2, ''); hold on;
时间: 2024-02-12 15:06:56 浏览: 81
这是我所做过绘图的代码
好的,以下是将代码转化为画二维图形的代码:
```matlab
% 均匀采样点
x0 = zeros(100, 1)';
y0 = zeros(100, 1)';
z0 = zeros(100, 1)';
k = 1;
x1 = linspace(-2, 2, 10);
y1 = linspace(-2, 2, 10);
for i = 1:length(x1)
for j = 1:length(y1)
M(k,:) = [x1(i) y1(j)]; % M中是均匀采样点的坐标
k = k + 1;
end
end
x0 = M(:, 1); % 提取 M 的第一列,即 x1 列
y0 = M(:, 2); % 提取 M 的第二列,即 y1 列
% 真实函数
for i = 1:100
z0(i) = sin(pi*x0(i)/2) + cos(pi*x0(i)/3);
end
% 绘制均匀采样点和真实函数的图像
figure(1);
plot3(x0, y0, z0, '');
% RBF插值
sigma = 0.2;
x_g = zeros(100, 100);
for j = 1:100
for k = 1:100
dist = sqrt((x0(j)-x0(k))^2 + (y0(j)-y0(k))^2);
x_g(j, k) = exp(-dist/2/sigma^2); % 高斯核函数
%x_g(j, k) = ((dist)^2 + 1)^0.5; % 多项式核函数
end
end
w = x_g \ z0';
x_hat = linspace(-2, 2, 100);
y_hat = linspace(-2, 2, 100);
z_g = zeros(length(x_hat), length(x_hat));
for p = 1:length(x_hat)
for n = 1:length(x_hat)
for m = 1:100
dist2 = sqrt((x0(m)-x_hat(p))^2 + (y0(m)-y_hat(n))^2);
f = w(m) * exp(-dist2/2/sigma^2); % 高斯核函数
%f = w(m) * ((dist2)^2 + 1)^0.5; % 多项式核函数
z_g(p, n) = z_g(p, n) + f;
end
z_real = x_hat(p) * exp(-x_hat(p)^2 + y_hat(n));
end
end
% 绘制插值结果的图像
figure(2)
mesh(x_hat, y_hat, z_g');
%set(gcf, 'Renderer', 'ZBuffer');
axis([-2 2 -2 2 -2 2]);
% 计算误差并输出
E_max = max(max(abs(z_g-z_real)));
E_avg = mean(mean(abs(z_g-z_real)));
fprintf('均方根误差最大值为 %.4f\n', E_max)
fprintf('均方根误差平均值为 %.4f\n', E_avg)
% 绘制误差图像
x = [1];
y1 = E_max;
y2 = E_avg;
figure(3)
plot(x, y2, '');
hold on;
```
阅读全文