My plot is empty .
8 次查看(过去 30 天)
显示 更早的评论
Hy, I'm trying to code a hermite polynomial by using Lagrange polynomial and Lagrange derivation as shown in the photos below. However, my plot is showing up empty when i run the function in a new file (in here, I have addded it at the end of the Hermite function)
I think i have a mistake in the function itsself but cant figure it out . i have attached the photos of the lagrange and hermite ( I used N instead of k for the order so no need to use k so that its same as for H)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1556217/image.png)
Here is my code :
function interpolated_values = hermite_interpolation5(x_values, x_j, f_j, f_prime_j)
% x_values are the interpolation points
% f_j are the function values at x_j
% f_prime_j are the derivatives at x_j
% Calculate the degree of the polynomial
N = length(x_j) - 1;
% Initialize the interpolated values
interpolated_values = zeros(size(x_values));
for j = 1:N+1
% Compute Lagrange polynomial and its derivative
L = 1;
L_prime = 0;
for m = 1:N+1
if m ~= j
% Compute Lagrange polynomial
L = L .* (x_values - x_j(m)) ./ (x_j(j) - x_j(m));
% Compute derivative of Lagrange polynomial
product_term = 1;
for i =1:N+1
if m ~= j && m~=i
%L_prime = L.* 1 ./ (x_values - x_j(i));
%if k ~= i && k ~= j
%product_term = product_term .* 1 ./ (x_values - x_j(i));
product_term = product_term .* (x_values - x_j(m)) ./ (x_j(j) - x_j(m));
end
end
%L_prime = L.*product_term;
L_prime = L_prime + 1 ./ (x_j(j) - x_j(i)) .* product_term;
end
end
% Compute Hermite polynomials
H = (1 - 2 .* L_prime .* (x_values - x_j(j))) .* L.^2;
tilde_H = (x_values - 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
%-------------------------------------------------------------------------------------------
% Example usage
%x_j = [0, 1, 2];
%f_j = [1, 2, 0];
%f_prime_j = [2, 1, -1];
clear;clc
Np=5;
x_j = (0:Np)./Np;
%x_j = 0.5-0.5*cos((0:Np)*pi/Np);
%x_j = 0.5 + (1./pi).*asin((2.*(0:Np)./Np)-1);
f_j = 1./ (1+ (x_j.^2));
f_prime_j =-(2*x_j)./(x_j.^2 + 1).^2;
% Generate points for interpolation
%x_values = x_j;
x_values = linspace(0, 1, 5);
% Perform Hermite interpolation
interpolated_values = hermite_interpolation5(x_values, x_j, f_j, f_prime_j);
% Plot the results
figure;
%plot(x_j, f_j, '*', '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;
2 个评论
Dyuman Joshi
2023-11-30
Because the data you have is NaN.
When inner most loop variable i is equal to outer most loop variable j, x(i) is equal to x(j). And this term
L_prime = L_prime + 1 ./ (x_j(j) - x_j(i)) .* product_term;
becomes NaN.
And NaN data does not plot, thus you get an empty figure.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!