Effacer les filtres
Effacer les filtres

Index exceeds the number of array elements. Index must not exceed 1.

1 vue (au cours des 30 derniers jours)
Panda05
Panda05 le 29 Nov 2023
Commenté : Dyuman Joshi le 29 Nov 2023
Hy. I'm trying to code a Hermite interpolation function in MATLAB. However , i'm getting an error of "Index exceeds the number of array elements. Index must not exceed 1." Below is my code . I need to run it using the codes commented at the end of the function. Your suggestions are highly appreciated.
%%-----------------------------------------------------------------------------------------
% Example usage. Please run in command window or new editor
Np=10;
x_j = 0.5 + (1./pi).*asin((2.*(0:Np)./Np)-1);
f_j = 1./ (1+ (x_j.*x_j));
f_prime_j =-(2*x_j)/(x_j.^2 + 1).^2;
% Generate points for interpolation
x_values = linspace(min(x_j), max(x_j), 1000);
% Perform Hermite interpolation
interpolated_values = hermite_interpolation2(x_values, x_j, f_j, f_prime_j);
Index exceeds the number of array elements. Index must not exceed 1.

Error in solution>hermite_interpolation2 (line 44)
interpolated_values = interpolated_values + f_j(j) * H + f_prime_j(j) * tilde_H;
% Plot the results
figure;
plot(x_j, f_j, 'o', 'MarkerSize', 10, 'DisplayName', 'Data Points');
hold on;
plot(x_values, interpolated_values, '-', 'LineWidth', 2, 'DisplayName', 'Hermite Interpolation');
xlabel('x');
ylabel('f(x)');
legend('Location', 'Best');
title('Hermite Interpolation');
grid on;
function interpolated_values = hermite_interpolation2(x, x_j, f_j, f_prime_j)
n = length(f_j) - 1; % Degree of the polynomial
% Initialize the interpolated values
interpolated_values = zeros(size(x));
for j = 1:n+1
% Compute Lagrange polynomial and its derivative
L = 1;
L_prime = 0;
for i = 1:n+1
if i ~= j
L = L .* (x - x_j(i)) / (x_j(j) - x_j(i));
L_prime = L_prime + 1 / (x_j(j) - x_j(i));
end
end
% Compute Hermite polynomials
H = (1 - 2 * L_prime * (x - x_j(j))) .* L.^2;
tilde_H = (x - x_j(j)) .* L.^2;
% Add contributions to the interpolated values
interpolated_values = interpolated_values + f_j(j) * H + f_prime_j(j) * tilde_H;
end
end

Réponse acceptée

Dyuman Joshi
Dyuman Joshi le 29 Nov 2023
There was a missing element-wise division sign in the definition of f_prime_j, causing the said variable to be defined as a scalar.
When you called the function, it tried to access the data in f_prime_j via indices for which values did not exist, thus you got the error.
See the correction below -
% Example usage. Please run in command window or new editor
Np=10;
x_j = 0.5 + (1./pi).*asin((2.*(0:Np)./Np)-1);
f_j = 1./ (1+ (x_j.*x_j));
%% Missing sign
%% v
f_prime_j =-(2*x_j)./(x_j.^2 + 1).^2;
% Generate points for interpolation
x_values = linspace(min(x_j), max(x_j), 1000);
% Perform Hermite interpolation
interpolated_values = hermite_interpolation2(x_values, x_j, f_j, f_prime_j);
% Plot the results
figure;
plot(x_j, f_j, 'o', 'MarkerSize', 10, 'DisplayName', 'Data Points');
hold on;
plot(x_values, interpolated_values, '-', 'LineWidth', 2, 'DisplayName', 'Hermite Interpolation');
xlabel('x');
ylabel('f(x)');
legend('Location', 'Best');
title('Hermite Interpolation');
grid on;
function interpolated_values = hermite_interpolation2(x, x_j, f_j, f_prime_j)
n = length(f_j) - 1; % Degree of the polynomial
% Initialize the interpolated values
interpolated_values = zeros(size(x));
for j = 1:n+1
% Compute Lagrange polynomial and its derivative
L = 1;
L_prime = 0;
for i = 1:n+1
if i ~= j
L = L .* (x - x_j(i)) / (x_j(j) - x_j(i));
L_prime = L_prime + 1 / (x_j(j) - x_j(i));
end
end
% Compute Hermite polynomials
H = (1 - 2 * L_prime * (x - x_j(j))) .* L.^2;
tilde_H = (x - x_j(j)) .* L.^2;
% Add contributions to the interpolated values
interpolated_values = interpolated_values + f_j(j) * H + f_prime_j(j) * tilde_H;
end
end
  2 commentaires
Panda05
Panda05 le 29 Nov 2023
Modifié(e) : Panda05 le 29 Nov 2023
Thank you so much Joshi. May God bless you ! I spent 2 hours looking for a simple ''.''
Dyuman Joshi
Dyuman Joshi le 29 Nov 2023
Thank you and you're welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Interpolation dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by