In: Advanced Math
In this problem, you can use the Matlab program posted on course website and Canvas (also given in the lecture) that computes the interpolation polynomial. We want to see how well a given function can be approximated by the interpolation polynomials. Let f be a function. We divide the the interval [−0.6,0.6] into subintervals of the same length h = 0.02. The gridpoints are −0.6 = x1 < x2 < ... < x61 = 0.6. Take N = 61 points (x1,y1),...(xN,yN) on the graph of f.
(a) For f(x) = sinx, plot the graph of the interpolation P on the interval [−0.6,0.6]. Plot f and all of P on the same graph (for example, by using the command hold on). Does the interpolation polynomial approximate well the function f on the interval [−0.6, 0.6]?
(b) The same questions as in Part (a) but for f (x) = 1+x .
(c) We know that the error between f and P is estimated by
| f (x) − P (x)| ≤ max |f (n) | (∗)
n n−1 [a,b]
Let f (x) = 1 and [a, b] = [−0.6, 0.6]. Use Stirling approximation
m√m! ≈ 1 (for large
1+x me m) to show that the right hand side of (∗) goes to infinity as n → ∞.
clear all
close all
%x and y values for finding the
%function for which interpolation have to do
func=@(x)sin(x);
fprintf('function for which interpolation have to do f(x)=')
disp(func)
%all interpolating points
z1=-0.6:0.002:0.6;
figure(1)
hold on
plot(z1,func(z1));
hold on
%all node value
fprintf('\n')
x2=-0.6:0.02:0.6;
y2=func(x2);
%Function for Newton Divide difference
[Value]=NewtonForm(length(x2),x2,y2,z1);
zz=func(z1);
err=zz-Value;
err_max=max(err);
err_min=min(err);
err_rs=sqrt(mean((zz - Value).^2));
fprintf('For n=%d with given data point for
sin(x)\n',length(x2))
fprintf('\tMaximum error = %f\n',err_max)
fprintf('\tMinimum error = %f\n',err_min)
fprintf('\tRMSE error = %f\n',err_rs)
figure(1)
plot(z1,Value)
figure(1)
xlabel('x')
ylabel('f(x)')
title('Actual and Newton Interpolation')
legend('Actual
data','Interpolation','location','best')
box on; grid on
clear all
%function for which interpolation have to do
func=@(x) 1+x;
fprintf('function for which interpolation have to do f(x)=')
disp(func)
%all interpolating points
z1=-0.6:0.002:0.6;
figure(2)
hold on
plot(z1,func(z1));
%all node value
fprintf('\n')
x2=-0.6:0.02:0.6;
y2=func(x2);
%Function for Newton Divide difference
[Value]=NewtonForm(length(x2),x2,y2,z1);
zz=func(z1);
err=zz-Value;
err_max=max(err);
err_min=min(err);
err_rs=sqrt(mean((zz - Value).^2));
fprintf('For n=%d with given data point for
1+x\n',length(x2))
fprintf('\tMaximum error = %f\n',err_max)
fprintf('\tMinimum error = %f\n',err_min)
fprintf('\tRMSE error = %f\n',err_rs)
figure(2)
plot(z1,Value)
xlabel('x')
ylabel('f(x)')
title('Actual and Newton Interpolation')
legend('Actual
data','Interpolation','location','best')
box on; grid on
%Function for Newton Divided difference interpolation
function [Value]=NewtonForm(m,x1,y1,z1)
%Creatin the Newton Divided difference formulae
for variable x
syms x
y2=y1;
zz=zeros(m,m+1);
zz(:,1)=x1'; zz(:,2)=y1';
for i=1:m-1
n1=length(y2);
for j=1:n1-1
y3(j)=(y2(j+1)-y2(j))/(x1(i+j)-x1(j));
zz(j,i+2)=y3(j);
end
z(i)=y3(1);
y2=y3;
clear y3;
end
nn=length(zz);
%loop for creating the function
n=length(z);
for i=1:n
s1=1;
for j=1:i
s1=(x-x1(j))*s1;
end
zz1(i)=(s1)*z(i);
end
f_newton(x)=sum(zz1)+y1(1);
Value=double(f_newton(z1));
end
%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%