代码修正,求出插值曲线和原图的对比:x0=zeros(100,1)'; z0=zeros(100,1)'; x0= linspace(-4, 4, 100); for i = 1:100 z0(i) = sin(pix0(i)/2) + cos(pix0(i)/3); end figure(1); plot(x0,z0,'k-'); k=1 %RBF sigma=0.2; x_g=zeros(100,1)'; for j=1:100 dist=sqrt((x0(j))^2); x_g(j)=exp(-(dist)/2sigma^2);%gauss %x_g(j,k)=((dist)^2+1)^0.5; end w=x_g\z0'; x_hat = linspace(-2, 2, 100); z_g=zeros(length(x_hat),1); for p=1:length(x_hat) for m=1:100 dist2=sqrt((x0(m)-x_hat(p))^2); f=w(m)exp(-(dist2)/2sigma^2);%gauss %f=w(m)((dist2)^2+1)^0.5; z_g(p)=z_g(p)+f; end z_real=sin(pix_hat(p)/2) + cos(pix_hat(p)/3); end figure(2) plot(x_hat,z_g'); %set(gcf, 'Renderer', 'ZBuffer'); axis([-4 4 -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;
时间: 2023-08-12 22:04:32 浏览: 113
好的,您可以使用以下代码来画出插值曲线和原图的对比:
```
% 绘制原图
figure(1);
plot(x0, z0, 'k-');
title('原图');
xlabel('x');
ylabel('y');
% 绘制插值曲线
figure(2);
plot(x_hat, z_g, 'r-', x_hat, z_real, 'b-');
title('插值曲线和原图对比');
xlabel('x');
ylabel('y');
legend('插值曲线', '原图');
```
以上代码会生成两张图,第一张图是原图,第二张图是插值曲线和原图的对比。希望能够帮助到您。
相关问题
将代码转化为画二维图形的代码: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;
好的,以下是将代码转化为画二维图形的代码:
```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;
```
clc,clear format long X=-5:5 n=10; m=length(X); Y=zeros(1,m); syms x f(x)=18/(1+2*x^2) for i=1:m Y(i)=f(X(i)) end x0=X lagrange(X,Y,x0); Y_f=f(X); Y1=double(Y_f); figure (1) plot(X,Y); hold on fplot(@(x)18/(1+2*x^2),[-5,5],'-r') legend('拉格朗日插值图像','函数图像')的相似代码
这段代码是用 MATLAB 实现的拉格朗日插值,并将插值结果与原函数图像作对比。以下是相似的 MATLAB 代码:
```matlab
clc; clear; format long;
X = -5:5;
n = 10;
m = length(X);
Y = zeros(1, m);
syms x;
f(x) = 18 / (1 + 2 * x^2);
for i = 1:m
Y(i) = f(X(i));
end
x0 = X;
Y_lagrange = lagrange(X, Y, x0);
Y_f = f(X);
Y1 = double(Y_f);
figure(1);
plot(X, Y_lagrange);
hold on;
fplot(@(x)18/(1+2*x^2),[-5,5],'-r');
legend('拉格朗日插值图像','函数图像');
function y = lagrange(X, Y, x)
% 拉格朗日插值
n = length(X);
y = 0;
for i = 1:n
p = 1;
for j = 1:n
if (j ~= i)
p = p * (x - X(j)) / (X(i) - X(j));
end
end
y = y + Y(i) * p;
end
end
```
其中 `lagrange` 函数是实现拉格朗日插值的核心代码。
阅读全文