Error: Matrix dimensions must agree.
Afficher commentaires plus anciens
% 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
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Interpolation dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
