【发布时间】:2013-10-08 20:40:03
【问题描述】:
我正在尝试使用 MATLAB 函数 spline 插入以下函数,
在等距点xi = i./n、i = 0,1,...,n 和n = 2^j、j = 4,5,...,14。
对于每个计算,我记录点 x = 0:0.001:1 的最大误差,并使用 loglog 绘图将这些误差与 n 进行对比。
下面是代码,
index=1
for j = 4:1:14;
n = 2^j;
i = 0:1:n;
xi = i./n;
yi = ((exp(3*xi))*sin(200.*(xi.^2))) ./(1+20.*(xi.^2));
x = 0:.001:1;
ye = ((exp(3*x))*sin(200*x.^2)) ./(1+20*x.^2);
yp = spline(x,xi,yi);
err = ye - yp;
merr(index) = max(err);
index = index+1;
end
n1 = 10:10:170;
loglog(n1, merr,'.')
xlabel('n');
ylabel('errors');
title('Cubic Splines');
但是当我运行代码时,我得到了以下错误:
错误使用 * 内矩阵尺寸必须一致。
(第 9 行)错误 yi = ((exp(3*xi))sin(200.(xi.^2))) ./(1+20.*(xi.^2));
我刚开始学习 MatLab,有人可以帮忙吗?
【问题讨论】:
-
请注意,通常使用mean squared error 或至少使用
abs函数计算错误。在您的代码中,err = ye - yp和err = abs(ye - yp)给出不同的结果。 -
你的'loglog'命令会抛出一个错误,因为
numel(n1)~=numel(merr),但除此之外,我认为我的回答解释了你得到的错误信息和你会得到的spline错误。
标签: matlab interpolation