Newtonian interpolation polynomial that interpolates f twice (value and 1st derivative)
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Construct a Newtonian interpolation polynomial that interpolates f at all points x = −2, −1,0,1,2 twice (value and 1st derivative). What is the value of the interpolation polynomial at x = 1.5?
This is my code but I do not get the correct answer:
format long
function d = deljeneDif_P3_2xInterp(X,Y,DY)
M = zeros(4);
M(:,1) = [Y(1); Y(1); Y(2); Y(2)];
M(:,2) = [0; DY(1); (Y(2)-Y(1))/(X(2)-X(1)); DY(2)];
M(:,3) = [0; 0; (M(3,2)-M(2,2))/(X(2)-X(1)); (M(4,2)-M(3,2))/(X(2)-X(1))];
M(4,4) = (M(4,3)-M(3,3))/(X(2)-X(1));
d = diag(M);
end
a = 1./2.80;
f = @(x) (8.*a.^3)./(x.^2+4.*a.^2);
df = @(x) (-16.*a.^3.*x)./(x.^2+4.*a.^2).^2;
interpX = [-2 -1 0 1 2];
interpY = f(interpX);
interpDY = df(interpX);
d = deljeneDif_P3_2xInterp(interpX,interpY,interpDY);
vrednost_2 = polyval(d, 1.5);
0 commentaires
Réponses (1)
Davide Masiello
le 18 Mai 2022
If the problem is the error appearing in your question, i.e.
Function definitions in a script must appear at the end of the file.
then just change the script in the following way and it works.
format long
a = 1./2.80;
f = @(x) (8.*a.^3)./(x.^2+4.*a.^2);
df = @(x) (-16.*a.^3.*x)./(x.^2+4.*a.^2).^2;
interpX = [-2 -1 0 1 2];
interpY = f(interpX);
interpDY = df(interpX);
d = deljeneDif_P3_2xInterp(interpX,interpY,interpDY);
vrednost_2 = polyval(d, 1.5)
function d = deljeneDif_P3_2xInterp(X,Y,DY)
M = zeros(4);
M(:,1) = [Y(1); Y(1); Y(2); Y(2)];
M(:,2) = [0; DY(1); (Y(2)-Y(1))/(X(2)-X(1)); DY(2)];
M(:,3) = [0; 0; (M(3,2)-M(2,2))/(X(2)-X(1)); (M(4,2)-M(3,2))/(X(2)-X(1))];
M(4,4) = (M(4,3)-M(3,3))/(X(2)-X(1));
d = diag(M);
end
Voir également
Catégories
En savoir plus sur Interpolation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!