Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a MATLAB function to calculate the coefficients of Hermite interpolating polynomial, H (using divided difference) a MATLAB function to evaluate the Hermite interpolating polynomial.

Write
a MATLAB function to calculate the coefficients of Hermite interpolating polynomial,
H
(using divided difference)
a MATLAB function to evaluate the Hermite interpolating polynomial.
a MATLAB script to compare the error of using Hermite polynomial which fits the data at =0,0.5,1
x
=
0
,
0.5
,
1
to approximate the function
e
x
at =0.3,0.7,0.9
x
=
0.3
,
0.7
,
0.9
and plot a graph of the exact function
e
x
and the Hermite polynomial
H
.
First function:
Input: x, y, dy where x is a vector consisting of
x
i
,
y
is a vector consisting of ()
f
(
x
i
)
,
d
y
is a vector consisting of ()
f
(
x
i
)
,
Output: c where c is a vector consisting of the coefficients of the Hermite Polynomial, that is,
()=1+2(1)+2(1)2++2+2=1().
H
(
x
)
=
c
1
+
c
2
(
x
x
1
)
+
c
2
(
x
x
1
)
2
+
+
c
2
n
+
2
i
=
1
k
(
x
z
i
)
.
where 21=2=,
z
2
i
1
=
z
2
i
=
x
i
,
for =1,,
i
=
1
,
,
n
.
function [c] = Divided_difference_Hermite_NetID(x,y,dy)
%% Step 1: F = zeros(2n,2n);
F(1:2:end,1) = y; F(2:2:end,1) = y; F(2:2:end,2) = dy;
z = zeros(2*n,1); z(1:2:end) = x; z(2:2:end) = x;
%% Step 2: for j = 1, ..., n-1
F(2*j+1,2) = (F(2*j+1,1)-F(2*j,1))/(z(2*j+1)-z(2*j));
%% Step 3: for i = 3, ..., 2n
for j = 1, ..., i-1
(Using the Divided_differences to calculate the coefficient)
%% Step 4: set c = diag(F);
end
Second function:
Input: c, x, X, where c is a vector consisting of the coefficients of the Hermite Polynomial and x is a vector consisting of
x
i
.
Output: HX which is the function value of
H
at point X
function [HX] = Hermite_polynomial_NetID(c,x,X)
%% Step 1: PX = c(1); S = 1; z = zeros(2*n,1); z(1:2:end) = x; z(2:2:end) = x;
%% Step 2: for i = 2,3,..., 2*n
S = S*(X-z(i-1))
PX = PX + c(i)*S;
end
Main script:
%% Step 1: define the function
f
and
f
f = @(x) exp(x);
df = @(x) exp(x);
%% Step 2: define the vector x
%%% type your code here
%% Step 3: define the test points X
%%% type your code here
%% Step 4: calculate the vector y and dy
y = f(x);
dy = df(x);
%% Step 5: calculate the coefficients of the Hermite Polynomial
%%% type your code here
%% Step 6: evaluate the polynomial and the function at X
%%% type your code here
%% Step 7: compute the error
%%% type your code here
%% Step 8: plot the graph
N = 100;
HX = zeros(N+1,1);
X_plot = (0:1/N:1)';
for i = 1:N+1
HX(i)= Hermite_polynomial_NetID(c,x,X_plot(i));
end
plot(X_plot,HX,'.-')
hold on
plot(X_plot,f(X_plot),'r-.-')
Assuming the main script is saved as "main_NetID.m". We can use the following Matlab comment to check your code:
Command Output
x = [-1,0,1]';
y = [-1,0,1]';
dy = [3,0,3]';
c = Divided_difference_Hermite_NetID(x,y,dy)
X = 1/2;
HX = Hermite_polynomial_NetID(c,x,X)
c =
-1
3
-2
1
0
0
HX =
0.1250
main_NetID
error =
1.0e-05 *
0.3962
0.4195
0.3174

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions