0% found this document useful (0 votes)
97 views3 pages

DS 288 (AUG) 3:0 Numerical Methods Homework-3 Soln3 Code

Repeat problem (2) with a natural cubic spline. Also report all four coefficients for each of the cubics which comprise the interpolants for both f(t) and g(t). How does your letter compare with that produced in problem (2)?

Uploaded by

Ankit Pagaria
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views3 pages

DS 288 (AUG) 3:0 Numerical Methods Homework-3 Soln3 Code

Repeat problem (2) with a natural cubic spline. Also report all four coefficients for each of the cubics which comprise the interpolants for both f(t) and g(t). How does your letter compare with that produced in problem (2)?

Uploaded by

Ankit Pagaria
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

%SOLUTION 3

t=[0 1 2 3 4 5 6 7 8 9 10 11 12];
y=[2.25 1.77 1.61 1.75 2.30 2.76 2.91 2.76 2.25 1.37 0.56 0.08 0.25];
x=[0.70 1.22 2.11 3.07 3.25 2.80 2.11 1.30 0.70 0.45 0.88 2.00 3.25];
y=y';
t=t';

g=zeros(12,4);
f=zeros(12,4);

step=0.05;
dt=[0:step:12];
dt=dt';
k=size(dt,1);
new_x=zeros(k,1);
new_y=zeros(k,1);

h=zeros(13,1);
alpha=zeros(13,1);
u=zeros(13,1);
l=zeros(13,1);
z=zeros(13,1);
a=zeros(13,1);
a=x';
d=zeros(13,1);
b=zeros(13,1);
c=zeros(13,1);
coeff=zeros(12,4);

for i=1:12
h(i)=t(i+1)-t(i);
end

for i=2:12
alpha(i)=(3*(a(i+1)-a(i)))/h(i)-(3*(a(i)-a(i-1))/h(i-1));
end

l(1)=1;
u(1)=0;
z(1)=0;

for i=2:12
l(i)=2*(t(i+1)-t(i-1))-h(i-1)*u(i-1);
u(i)=h(i)/l(i);
z(i)=(alpha(i)-h(i-1)*z(i-1))/l(i);
end

l(13)=1;
z(13)=0;
c(13)=0;

for j=12:-1:1
c(j)=z(j)-u(j)*c(j+1);
b(j)=(a(j+1)-a(j))/h(j)-(h(j)*(c(j+1)+2*c(j)))/3;
d(j)=(c(j+1)-c(j))/(3*h(j));
end
f=[a(1:12,:) b(1:12,:) c(1:12,:) d(1:12,:)];

for i=1:12
for j=1:k
if (dt(j)>=t(i)) && (dt(j)<=t(i+1))
new_x(j)=a(i)+b(i)*(dt(j)-t(i))+(c(i)*(dt(j)-t(i))^2)+(d(i)*(dt(j)-
t(i))^3);
end
end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for i=1:12
h(i)=t(i+1)-t(i);
end

for i=2:12
alpha(i)=(3*(y(i+1)-y(i)))/h(i)-(3*(y(i)-y(i-1))/h(i-1));
end

l(1)=1;
u(1)=0;
z(1)=0;

for i=2:12
l(i)=2*(t(i+1)-t(i-1))-h(i-1)*u(i-1);
u(i)=h(i)/l(i);
z(i)=(alpha(i)-h(i-1)*z(i-1))/l(i);
end

l(13)=1;
z(13)=0;
c(13)=0;

for j=12:-1:1
c(j)=z(j)-u(j)*c(j+1);
b(j)=(y(j+1)-y(j))/h(j)-(h(j)*(c(j+1)+2*c(j)))/3;
d(j)=(c(j+1)-c(j))/(3*h(j));
end

g=[y(1:12,:) b(1:12,:) c(1:12,:) d(1:12,:)];

for i=1:12
for j=1:k
if (dt(j)>=t(i)) && (dt(j)<=t(i+1))
new_y(j)=y(i)+b(i)*(dt(j)-t(i))+(c(i)*(dt(j)-t(i))^2)+(d(i)*(dt(j)-
t(i))^3);
end
end
end

plot(new_x,new_y)
disp("\nf(t) :\n")
disp(f)
disp("\ng(t) :\n")
disp(g)

You might also like