I am trying to interpolate the following function using the MATLAB function spline
,
at equidistant points xi = i./n
, i = 0,1,...,n
, and for n = 2^j
, j = 4,5,...,14
.
For each calculation, I record the maximum error at the points x = 0:0.001:1
and plot these errors against n
using a loglog
plot.
Below is the code,
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');
but when I run the code, I got the following error:
Error using * Inner matrix dimensions must agree.
Error in (line 9) yi = ((exp(3*xi))sin(200.(xi.^2))) ./(1+20.*(xi.^2));
I'm just starting learning MatLab, can anyone help?
abs
function. In your code,err = ye - yp
anderr = abs(ye - yp)
give different results. – marseinumel(n1)~=numel(merr)
but other than that, I think my answer explains the error message you get and aspline
error you will get. – chappjc