Error: Matrix dimensions must agree.

조회 수: 2 (최근 30일)
Kutlu Yigitturk
Kutlu Yigitturk 2020년 12월 26일
댓글: Cris LaPierre 2020년 12월 26일
% dening data points, vectors X and Y
X_VALUES=[0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5];
Y_VALUES=[0 0.247466462 0.881373587 1.550157957 2.094712547 2.532068064 2.893443986 3.200334942 3.466711038 3.70191108 3.912422766];
% dening x in range 0 to 5 with total 100 values
x = linspace(0,5,100);
ls = lagrange_self(X_VALUES,Y_VALUES,x);
sp = spline(X_VALUES,Y_VALUES,x);
plot(X_VALUES,Y_VALUES,'O',x,ls,'.',x,sp,'-')
title("Interpolating curves");
legend('Original','Cubic Polynomial Lagrange interpolating','Cubic Spline Interpolation')
% Calculate total error
% error for polynomal interpolation
ls = lagrange_self(X_VALUES,Y_VALUES,x);
% error
I get the 'Matrix dimensions must agree.' error on line 16
sp = sum(sqrt((Y_VALUES-ls).^2));
% displaying difference
fprintf("Total error for Cubic Spline Interpolation is %f\n",sp);
% error
s_cubic = sum(sqrt((Y_VALUES-y_cubic).^2));
% displaying difference
fprintf("Total error for Cubic Polynomial Lagrange interpolating is %f\n",s_cubic);
function v = lagrange_self(x,y,u)
n = 4;
v = zeros(size(u));
for k = 1:n
w = ones(size(u));
for j = [1:k-1 k+1:n]
w = (u-x(j))./(x(k)-x(j)).*w;
end
v = v+w*y(k)
end
end
Thank you in advance for your help

채택된 답변

Cris LaPierre
Cris LaPierre 2020년 12월 26일
편집: Cris LaPierre 2020년 12월 26일
Y_VALUES is a 1x11 matrix while ls is 1x100. In order to subtract them, they must either both have the same size, or one must be a scalar (single number). Because they are not, MATLAB can't subtract them, and you get this error message.
The simplest fix is create to have the same number of points as Y_VALUES.
x = linspace(0,5,length(Y_VALUES));
  댓글 수: 2
Kutlu Yigitturk
Kutlu Yigitturk 2020년 12월 26일
Things to do is indicated as follows.
  • Use Lagrange interpolating polynomial method and cubic spline interpolation to evaluate the function f(x) at 100 equally spaced points in the interval [0,5]. Use cubic polynomial for Lagrange’s method as well. The points to be used in interpolations should include first and last point (0 and 5), other two points should be chosen such that those points would represent significant change in f(x). Show the plots for Lagrange’s method, cubic spline and the given data in one graph. Discuss which method is better.
  • Calculate the total error for both methods by using the following formula. Here yi is the given y value for xi , and f(xi) is the value of the interpolating function at point xi.
So I cannot apply a solution like you explained. I must use ''x = linspace(0,5,100);'' like this. I would appreciate if there is any other solution you can suggest.
Cris LaPierre
Cris LaPierre 2020년 12월 26일
It looks like you don't compute the error using every point. Just the points that correspond to .

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interpolation에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by