필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Panda05
Panda05 2023년 11월 29일
댓글: Dyuman Joshi 2023년 11월 29일
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

채택된 답변

Dyuman Joshi
Dyuman Joshi 2023년 11월 29일
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
Panda05
Panda05 2023년 11월 29일
편집: Panda05 2023년 11월 29일
Thank you so much Joshi. May God bless you ! I spent 2 hours looking for a simple ''.''
Dyuman Joshi
Dyuman Joshi 2023년 11월 29일
Thank you and you're welcome!

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

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by