Write The Matlab Code For The Following Data Points Using Natural and Clamped Spines

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Question 1:

Write the Matlab code for the following data points using natural
and clamped spines.

Code for Natural Splines:


clc
clear all
%values of a(i)'s
s=[1.3 1.5 1.85 2.1 2.6 2.7 2.4 2.15 2.05 2.1 2.25 2.3 2.25 1.95 1.4 0.9 0.7
0.6 0.5 0.4 0.25];
x=[0.9 1.3 1.9 2.1 2.6 3.0 3.9 4.4 4.7 5.0 6.0 7.0 8.0 9.2 10.5 11.3 11.6 12
12.6 13 13.3];
n=20;
for i=1:n
h(i)=x(i+1)-x(i);
end
%constructing matrix B
b(1,1)=0;
b(n+1,1)=0;
for i=2:n
b(i,1)=(3/h(i))*(s(i+1)-s(i))-(3/h(i-1))*(s(i)-s(i-1))
end
%constructing matrix A
a(1,1)=1;
a(n+1,n+1)=1;
for i=2:n
for j=1:n+1
if i==j
a(i,j)=2*(h(i-1)+h(i));
else if i==(j-1)
a(i,j)=h(i);
else if i==j+1
a(i,j)=h(i-1);
end
end
end
end
end
%constructing matrix c
C=inv(a)*b;
%values of b(i)'s and d(i)'s
for i=1:n
b(i)=(1/h(i))*(s(i+1)-s(i))-(h(i)/3)*(2*C(i)+C(i+1))
d(i)=(C(i+1)-C(i))/(3*h(i));
end
for i=1:n
Z=[x(i):0.1:x(i+1)];
%constucting spline
D=s(i)+b(i)*(Z-x(i))+C(i)*(Z-x(i)).^2+d(i)*(Z-x(i)).^3;
%for plotting
plot (Z,D)
hold on
end
axis([0 15 -6 9])

Graph:

-2

-4

-6
0 5 10 15

Q-32

x1 = [1,2,5,6,7,8,10,13,17];
x2 = [17,20,23,24,25,27,27.7];
x3 = [27.7,28,29,30];
l1 = length(x1)-1;
l2 = length(x2)-1;
l3 = length(x3)-1;

a1 = [3,3.7,3.9,4.2,5.7,6.6,7.1,6.7,4.5];
a2 = [4.5,7,6.1,5.6,5.8,5.2,4.1];
a3 = [4.1,4.3,4.1,3];

for j=1:l1
h1(j) = x1(j+1) - x1(j);
end
for j=1:l2
h2(j) = x2(j+1) - x2(j);
end
for j=1:l3
h3(j) = x3(j+1) - x3(j);
end

AA(1,1) = 1;
AA(1,2:l1+1) = 0;
AA(2,1) = h1(1);
AA(2,2) = 2*(h1(1)+h1(2));
AA(2,3) = h1(2);
AA(2,4:l1+1) = 0;
for j=3:l1-1
AA(j,1:j-2) = 0;
AA(j,j-1) = h1(j-1);
AA(j,j) = 2*(h1(j-1)+h1(j));
AA(j,j+1) = h1(j);
AA(j,j+2:l1-1) = 0;
end
AA(8,1:6) = 0;
AA(8,7) = h1(7);
AA(8,8) = 2*(h1(7)+h1(8));
AA(8,9) = h1(8);
AA(9,1:8) = 0;
AA(9,9) = 1;

BB(1,1) = 1;
BB(1,2:l2+1) = 0;
BB(2,1) = h2(1);
BB(2,2) = 2*(h2(1)+h2(2));
BB(2,3) = h2(2);
BB(2,4:l2+1) = 0;
for j=3:l2-1
BB(j,1:j-2) = 0;
BB(j,j-1) = h2(j-1);
BB(j,j) = 2*(h2(j-1)+h2(j));
BB(j,j+1) = h2(j);
BB(j,j+2:l2-1) = 0;
end
BB(6,1:4) = 0;
BB(6,5) = h2(5);
BB(6,6) = 2*(h2(5)+h2(6));
BB(6,7) = h2(6);
BB(7,1:6) = 0;
BB(7,7) = 1;
CC(1,1) = 1;
CC(1,2:l3+1) = 0;
CC(2,1) = h3(1);
CC(2,2) = 2*(h3(1)+h3(2));
CC(2,3) = h3(2);
CC(2,4) = 0;
CC(3,1) = 0;
CC(3,2) = h3(2);
CC(3,3) = 2*(h3(2)+h3(3));
CC(3,4) = h3(3);
CC(4,1:3) = 0;
CC(4,4) = 1;

m1 = zeros(9,1);
m2 = zeros(7,1);
m3 = zeros(4,1);

for j=3:l1+1
m1(j-1) = (3/h1(j-1))*(a1(j) - a1(j-1)) - (3/h1(j-2))*(a1(j-1) - a1(j-2));
end
for j=3:l2+1
m2(j-1) = (3/h2(j-1))*(a2(j) - a2(j-1)) - (3/h2(j-2))*(a2(j-1) - a2(j-2));
end
for j=3:l3+1
m3(j-1) = (3/h3(j-1))*(a3(j) - a3(j-1)) - (3/h3(j-2))*(a3(j-1) - a3(j-2));
end
c1 = inv(AA)*m1;
c2 = inv(BB)*m2;
c3 = inv(CC)*m3;
for j=2:l1+1
b1(j-1) = (a1(j)-a1(j-1))/h1(j-1) - (h1(j-1)*(c1(j)+2*c1(j-1)))/3;
d1(j-1) = (c1(j)-c1(j-1))/(3*h1(j-1));
end
for j=2:l2+1
b2(j-1) = (a2(j)-a2(j-1))/h2(j-1) - (h2(j-1)*(c2(j)+2*c2(j-1)))/3;
d2(j-1) = (c2(j)-c2(j-1))/(3*h2(j-1));
end
for j=2:l3+1
b3(j-1) = (a3(j)-a3(j-1))/h3(j-1) - (h3(j-1)*(c3(j)+2*c3(j-1)))/3;
d3(j-1) = (c3(j)-c3(j-1))/(3*h3(j-1));
end

for j=1:l1
fplot(@(y) a1(j)+b1(j)*(y-x1(j))+c1(j)*(y-x1(j)).^2+d1(j)*(y-x1(j)).^3,
[x1(j) x1(j+1)]);
hold on;
end
for j=1:l2
fplot(@(y) a2(j)+b2(j)*(y-x2(j))+c2(j)*(y-x2(j)).^2+d2(j)*(y-x2(j)).^3,
[x2(j) x2(j+1)]);
hold on;
end
for j=1:l3
fplot(@(y) a3(j)+b3(j)*(y-x3(j))+c3(j)*(y-x3(j)).^2+d3(j)*(y-x3(j)).^3,
[x3(j) x3(j+1)]);
hold on;
end
hold off;
xlabel('x');
ylabel('f(x)');
str = {'Curve 1', 'Curve 2','Curve 3'};
text([7 24 26],[5 5 4],str);

Q32-clamped

x1 = [1,2,5,6,7,8,10,13,17];
x2 = [17,20,23,24,25,27,27.7];
x3 = [27.7,28,29,30];
l1 = length(x1)-1;
l2 = length(x2)-1;
l3 = length(x3)-1;
a1 = [3,3.7,3.9,4.2,5.7,6.6,7.1,6.7,4.5];
a2 = [4.5,7,6.1,5.6,5.8,5.2,4.1];
a3 = [4.1,4.3,4.1,3];

for j=1:l1
h1(j) = x1(j+1) - x1(j);
end
for j=1:l2
h2(j) = x2(j+1) - x2(j);
end
for j=1:l3
h3(j) = x3(j+1) - x3(j);
end

AA(1,1) = 2*h1(1);
AA(1,2) = h1(1);
AA(1,3:l1+1) = 0;
AA(2,1) = h1(1);
AA(2,2) = 2*(h1(1)+h1(2));
AA(2,3) = h1(2);
AA(2,4:l1+1) = 0;
for j=3:l1-1
AA(j,1:j-2) = 0;
AA(j,j-1) = h1(j-1);
AA(j,j) = 2*(h1(j-1)+h1(j));
AA(j,j+1) = h1(j);
AA(j,j+2:l1-1) = 0;
end
AA(8,1:6) = 0;
AA(8,7) = h1(7);
AA(8,8) = 2*(h1(7)+h1(8));
AA(8,9) = h1(8);
AA(9,1:7) = 0;
AA(9,8) = h1(8);
AA(9,9) = 2*h1(8);

BB(1,1) = 2*h2(1);
BB(1,2) = h2(1);
BB(1,3:l2+1) = 0;
BB(2,1) = h2(1);
BB(2,2) = 2*(h2(1)+h2(2));
BB(2,3) = h2(2);
BB(2,4:l2+1) = 0;
for j=3:l2-1
BB(j,1:j-2) = 0;
BB(j,j-1) = h2(j-1);
BB(j,j) = 2*(h2(j-1)+h2(j));
BB(j,j+1) = h2(j);
BB(j,j+2:l2-1) = 0;
end
BB(6,1:4) = 0;
BB(6,5) = h2(5);
BB(6,6) = 2*(h2(5)+h2(6));
BB(6,7) = h2(6);
BB(7,1:5) = 0;
BB(7,6) = h2(6);
BB(7,7) = 2*h2(6);

CC(1,1) = 2*h3(1);
CC(1,2) = h3(1);
CC(1,3:l3+1) = 0;
CC(2,1) = h3(1);
CC(2,2) = 2*(h3(1)+h3(2));
CC(2,3) = h3(2);
CC(2,4) = 0;
CC(3,1) = 0;
CC(3,2) = h3(2);
CC(3,3) = 2*(h3(2)+h3(3));
CC(3,4) = h3(3);
CC(4,1:2) = 0;
CC(4,3) = h3(3);
CC(4,4) = 2*h3(3);

m1 = zeros(9,1);
m2 = zeros(7,1);
m3 = zeros(4,1);

m1(1) = 3*(a1(2)-a1(1))/h1(1) - 3*1.0;


for j=3:l1
m1(j-1) = (3/h1(j-1))*(a1(j) - a1(j-1)) - (3/h1(j-2))*(a1(j-1) - a1(j-2));
end
m1(9) = 3*(-0.67)-3*(a1(9)-a1(8))/h1(8);
m2(1) = 3*(a2(2)-a2(1))/h2(1) - 3*3.0;
for j=3:l2
m2(j-1) = (3/h2(j-1))*(a2(j) - a2(j-1)) - (3/h2(j-2))*(a2(j-1) - a2(j-2));
end
m2(7) = 3*(-4.0)-3*(a2(7)-a2(6))/h2(6);
m3(1) = 3*(a3(2)-a3(1))/h3(1) - 3*0.33;
for j=3:l3
m3(j-1) = (3/h3(j-1))*(a3(j) - a3(j-1)) - (3/h3(j-2))*(a3(j-1) - a3(j-2));
end
m3(4) = 3*(-1.5)-3*(a3(4)-a3(3))/h3(3);

c1 = inv(AA)*m1;
c2 = inv(BB)*m2;
c3 = inv(CC)*m3;
for j=2:l1+1
b1(j-1) = (a1(j)-a1(j-1))/h1(j-1) - (h1(j-1)*(c1(j)+2*c1(j-1)))/3;
d1(j-1) = (c1(j)-c1(j-1))/(3*h1(j-1));
end
for j=2:l2+1
b2(j-1) = (a2(j)-a2(j-1))/h2(j-1) - (h2(j-1)*(c2(j)+2*c2(j-1)))/3;
d2(j-1) = (c2(j)-c2(j-1))/(3*h2(j-1));
end
for j=2:l3+1
b3(j-1) = (a3(j)-a3(j-1))/h3(j-1) - (h3(j-1)*(c3(j)+2*c3(j-1)))/3;
d3(j-1) = (c3(j)-c3(j-1))/(3*h3(j-1));
end
for j=1:l1
fplot(@(y) a1(j)+b1(j)*(y-x1(j))+c1(j)*(y-x1(j)).^2+d1(j)*(y-x1(j)).^3,
[x1(j) x1(j+1)]);
hold on;
end
for j=1:l2
fplot(@(y) a2(j)+b2(j)*(y-x2(j))+c2(j)*(y-x2(j)).^2+d2(j)*(y-x2(j)).^3,
[x2(j) x2(j+1)]);
hold on;
end
for j=1:l3
fplot(@(y) a3(j)+b3(j)*(y-x3(j))+c3(j)*(y-x3(j)).^2+d3(j)*(y-x3(j)).^3,
[x3(j) x3(j+1)]);
hold on;
end
hold off;
xlabel('x');
ylabel('f(x)');
str = {'Curve 1', 'Curve 2','Curve 3'};
text([7 24 26],[5 5 4],str);

You might also like