You are on page 1of 6

CUBIC APPROXIMATION x=a

The cubic approximation to f at x =a is a cubic, f3 (x), which has the same value,
derivative, second derivative and third derivative as f at x = a:

EXAMPLE
Taylor Series cubic approximation
Using Taylor's formula with a=0 and n=3, how would you find the cubic approximation of f(x)= 1/(1-x)
at x=0, given the upper bound for the magnitude of the error in the approximation when lxl< 0.1 ?

The derivatives are

The Taylor polynomial

and the Taylor

remainder

where c is between 0 and

we are trying to determine the maximum of


domain
maximum error would be

and so this would occur at

. Since

then

on the
and the

1. CODING NATURAL CUBIC SPLINE


%Natural cubic spline based on Cubic Hermite Formula
clear
clf reset
x=[.25 1 1.5 2 2.4 5]
y=[23.1 1.68 1 .84 .83 .26]
n=length(x);
for i=1:n-1,
h(i)=x(i+1)-x(i); delta(i)= (y(i+1)-y(i))/h(i);
end
%Construct the Tridiagonal System
hp=h(2:n-1); hm=h(1:n-2);
deltap=delta(2:n-1); deltam=delta(1:n-2);
U=[1,hm]; L=[hp,1]; D=2*[1,hp+hm,1];
B=3*[delta(1), hp.*deltam+hm.*deltap,delta(n-1)];
dy = tridiag(L,D,U,B);
%this is just the Cubic Hermite Graphing Code
t=[];
u=[];
for i=1:n-1,
r=h(i)/10; a=delta(i);
b(i)=(a-dy(i))/h(i); c=(dy(i+1)-a)/h(i); d(i)=(c-b(i))/h(i);
p=x(i):r:x(i+1);
s=y(i)+(p-x(i)).*(dy(i)+(p-x(i)).*(b(i)+(p-x(i+1)).*d(i)));
t=[t, p];
u=[u, s];
end
plot(t,u,'b',x,y,'*r')
grid on

2. NATURAL CUBIC SPLINES

% find out coefficent b


s=size(x,1);
j=zeros(s,1);
m=zeros(s,1);
n=zeros(s,1);
p=zeros(s,1);
q=zeros(s,1);
for i=1:s
j(i,1)=i-1;
if i==1
p(i,1)=0;
q(i,1)=0;
m(i,1)=x(i+1,1)-x(i,1);
n(i,1)=y(i+1,1)-y(i,1);
elseif i==s
m(i,1)=0;
n(i,1)=0;
p(i,1)=0;
q(i,1)=0;
else
m(i,1)=x(i+1,1)-x(i,1);
n(i,1)=y(i+1,1)-y(i,1);
p(i,1)=2*(m(i-1,1)+m(i,1));
q(i,1)=3*(n(i,1)/m(i,1)-(n(i-1,1)/m(i-1,1)));
end
end
A=[j,x,y,m,n,p,q];
A
P=zeros(s-2,s-2);
q1=zeros(s-2,1);
for i=1:s-2
for j=1:s-2
if i==j
P(i,j)=p(i+1,1);
elseif abs(i-j)==1
P(i,j)=m(i,1);
else
P(i,j)=0;
end
end
q1(i,1)=q(i+1,1);
end
b1=zeros(s-2,1);
a=zeros(s,1);
b=zeros(s,1);
c=zeros(s,1);
b1=P\q1;
for i=1:s-2
b(i+1,1)=b1(i,1);
end

for i=1:s
if i==s
a(i,1)=0;
c(i,1)=0;
else
a(i,1)=n(i,1)/m(i,1)-m(i,1)*(b(i+1,1)+2*b(i,1))/3;
c(i,1)=(b(i+1,1)-b(i))/(3*m(i,1));
end
end
B=[a,b,c];
B;
le=round((x(length(x),1)-x(1,1))*100);
Mat=zeros(le,1);
YTM=zeros(le,1);

% maturity
% yield

t=1;
for i=1:s
if i==s
else
k=round(x(i+1,1)*100)-round(x(i,1)*100);
%
%
%
%
%
%
%

if k==1
Mat(t,1)=x(i,1);
YTM(t,1)=y(i,1);
Mat(t+1,1)=x(i+1,1);
YTM(t+1,1)=y(i+1,1);
t=t+1;
else
for r=0:(k)
Mat(t+r,1)=x(i,1)+r/100;
if r==0
YTM(t+r,1)=y(i,1);
else

YTM(t+r,1)=y(i,1)+a(i,1)*(r/100)+b(i,1)*(r/100)^2+c(i,1)*(r/100)^3;
end
end
t=t+k;
% end
% YTM(x(length(x),1),1)=y(length(x),1);
end
end
[Mat,YTM];
plot(Mat,YTM,'-',x,y,'ro',dataT(:,3),dataT(:,2),'o');

%zy=zeros(x(length(x),1),1);
%fy=zeros(x(length(x),1),1);
%'zero coupon yield curve (zy)'

%for i=1:s
%
if coupon(i,1)==0
%
zy(i,1)=y(i,1);
% else
% end
%end
%for i=1:C(x(length(x),1))
% if zy(i,1)==0
%
d=0;
%
for j=1:i-1
%
d=d+C1(i,1)/((1+C1(j,1)/100)^(j));
% end
%
%zy(i,1)=(((100+C1(i,1))/(100-d))^(1/i)-1)*100;
%else
% end
%end
%for i=1:C(x(length(x),1))
%

if i==length(C)
%
fy(length(C),1)=zy(length(C),1);
% else
%
fy(i,1)=(((1+zy(i+1,1)/100)^2)/(1+zy(i,1)/100)-1)*100;
%end
%end

%[C,C1,zy,fy]
%plot(C,zy,C,C1,C,fy);

You might also like