x=-5:1:5;
y=sin(x);
N=my_newton(x,y);
t =linspace(-5,5,1001);
s=sin(t);
r=polyval(N,t);
figure, plot(t,s,'m:');
hold on, plot(t,r,'-b');
plot(x,y,'r+');
legend('sin(x)','Newton','point');

%

%%2
clc,clear;
%X=[2 2.5 4 1];
%Y=[0.5 0.4 0.25 1];
%Lagrange(X,Y);
 
X=-5:1:5;
Y=sin(X);
L=Lagrange(X,Y);
t=linspace(-5,5,1001);
GroundTruth=sin(t);
 
x=t;
P=eval(L);
 
figure,plot(t,GroundTruth,'m:');
hold on, plot(t,P,'-b');
plot(X,Y,'r+');
legend('sin(x)','Lagrange','point');

%

%%3
clc,clear;
%X=[2 2.5 4 1];
%Y=[0.5 0.4 0.25 1];
%Lagrange(X,Y);
 
X1=-5:0.2:5;
X2=-5:0.2:5;
X3=-5:0.2:5;
 
 
Y1=sin(X1);
Y2=sin(X2);
Y3=sin(X3);
 
L1=Lagrange(X1,Y1);
N=my_newton(X2,Y2)
 
t=linspace(-5.2,5.2,1001);
GroundTruth=sin(t);
 
x=t;
 
P1=eval(L1);
P2=polyval(N,t);
 
figure,plot(t,GroundTruth,'m-')
hold on, plot(t,P1,':b')
hold on, plot(t,P2,':r')
plot(X1,Y1,'r+')
legend('sin(x)','Lagrange','newton','point')


%
% 发现插值居然

clc,clear;
X1=-pi:0.1:pi;;
Y1=sin(X1);
L1=Lagrange(X1,Y1);
x=X1
P1=eval(L1);
bias=P1-Y1

%

x=pi
sin(x)
eval(L1)

%

%%3
clc,clear;
%X=[2 2.5 4 1];
%Y=[0.5 0.4 0.25 1];
%Lagrange(X,Y);
 
X1=5:0.2:10;
X2=-5:0.2:5;
X3=-5:0.2:5;
 
 
Y1=sin(X1);
Y2=sin(X2);
Y3=sin(X3);
 
L1=Lagrange(X1,Y1);
N=my_newton(X2,Y2)
 
t=linspace(5,9.5,1001);
GroundTruth=sin(t);
 
x=t;
 
P1=eval(L1);
P2=polyval(N,t);
 
figure,plot(t,GroundTruth,'m-')
hold on, plot(t,P1,':b')
hold on, plot(t,P2,':r')
plot(X1,Y1,'r+')
xmin=-5;
xmax=10
ymin=-1
ymax=1
axis([xmin xmax ymin ymax])
legend('sin(x)','Lagrange','newton','point')
x=X1;
biasL=eval(L1)-Y1

% 这个结果证明L插值在x取大时直接暴毙,接下来给个更加极端的结果

%%3
clc,clear;
%X=[2 2.5 4 1];
%Y=[0.5 0.4 0.25 1];
%Lagrange(X,Y);
 
X1=115:0.2:120;
X2=-5:0.2:5;
X3=-5:0.2:5;
 
 
Y1=sin(X1);
Y2=sin(X2);
Y3=sin(X3);
 
L1=Lagrange(X1,Y1);
N=my_newton(X2,Y2)
 
t=linspace(115,120,1001);
GroundTruth=sin(t);
 
x=t;
 
P1=eval(L1);
P2=polyval(N,t);
 
figure,plot(t,GroundTruth,'m-')
hold on, plot(t,P1,':b')
hold on, plot(t,P2,':r')
plot(X1,Y1,'r+')
xmin=115;
xmax=120;
ymin=-1
ymax=1
axis([xmin xmax ymin ymax])
legend('sin(x)','Lagrange','newton','point')
x=X1;
biasL=eval(L1)-Y1

%

%%3
clc,clear;
%X=[2 2.5 4 1];
%Y=[0.5 0.4 0.25 1];
%Lagrange(X,Y);
 
X1=115:0.2:120;
X2=-5:0.2:5;
X3=-5:0.2:5;
 
 
Y1=(X1);
Y2=(X2);
Y3=(X3);
 
L1=Lagrange(X1,Y1);
N=my_newton(X2,Y2)
 
t=linspace(115,120,1001);
GroundTruth=(t);
 
x=t;
 
P1=eval(L1);
P2=polyval(N,t);
 
figure,plot(t,GroundTruth,'m-')
hold on, plot(t,P1,':b')
hold on, plot(t,P2,':r')
plot(X1,Y1,'r+')
xmin=115;
xmax=120;
ymin=-1
ymax=1
%axis([xmin xmax ymin ymax])
legend('sin(x)','Lagrange','newton','point')

xlim([115.00 120.00])
ylim([-159999999999999992620202531233938276352 19999999999999987271609109230129250304])
x=X1;
biasL=eval(L1)-Y1