Write The Matlab Code For The Following Data Points Using Natural and Clamped Spines
Write The Matlab Code For The Following Data Points Using Natural and Clamped Spines
Write The Matlab Code For The Following Data Points Using Natural and Clamped Spines
Write the Matlab code for the following data points using natural
and clamped spines.
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);
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);