How to draw all three plots on the same graph to get the exact graph given in the image?

3 views (last 30 days)
Hi I am using this code to generate clamped splines for three data sets.
% no 27
x=[1 2 5 6 7 8 10 13 17]';
y=[3.0 3.7 3.9 4.2 5.7 6.6 7.1 6.7 4.5]';
% no 271
% x1=[17 20 23 24 25 27 27.7]';
% y1=[4.5 7.3 6.1 5.6 5.8 5.4 4.1]';
% no 272
% x2=[27.7 28 29 30]';
% y2=[4.1 4.3 4.1 3.0]';
clmp=[3.0 4.5]'; %clamped condition
l=length(x);
hx=x(2:l)-x(1:l-1);
hy=y(2:l)-y(1:l-1);
g=[3*(hy(1)/hx(1)-clmp(1)); 3*(hy(2:l-1)./hx(2:l-1)- ...
hy(1:l-2)./hx(1:l-2)); 3*(clmp(2)-hy(l-1)/hx(l-1))];
Z=zeros(l);
Z(2:l-1,2:l-1)=2*(diag(hx(1:l-2))+diag(hx(2:l-1)));
Z(2:l-1,1:l-2)=Z(2:l-1,1:l-2)+diag(hx(1:l-2));
Z(2:l-1,3:l)=Z(2:l-1,3:l)+diag(hx(2:l-1));
Z(1,1:2)=[2*hx(1) hx(1)];
Z(l,l-1:l)=[hx(l-1) 2*hx(l-1)];
c=Z\g; %gaussian elimination
b=hy(1:l-1)./hx(1:l-1)-hx(1:l-1).*(2*c(1:l-1)+c(2:l))/3;
d=(c(2:l)-c(1:l-1))./hx/3;
s=[(1:l-1)' y(1:l-1) b c(1:l-1) d];
pp=mkpp(x',s(:,5:-1:2));
Then I used following script to plot a single plot showing all three splines together.
no27;
xx=linspace(1,17,100);
yy=ppval(pp,xx);
plot(xx,yy,x,y,'r*')
axis equal; grid on; grid minor;
hold on;
%%
no271;
xx1=linspace(17,27.7,100);
yy1=ppval(pp,xx1);
plot(xx1,yy1,x1,y1,'k*')
%%
no272;
xx2=linspace(27.7,30,100);
yy2=ppval(pp,xx2);
plot(xx2,yy2,x2,y2,'b*')
hold off;
The above image is a text book image and my graph should have be the same because i am using the same data set. But for some reasons i cannot get the exact plot given in image with my code. Please some one help me with this. The graph i got is attached below.

Accepted Answer

DGM
DGM on 10 Oct 2021
I'm not sure if you're just after a working plot, or if you're trying to implement the spline interpolation itself. I'm going to assume you're just after a plot.
N = 100; % samples per segment
% no 27
x0 = [1 2 5 6 7 8 10 13 17]';
y0 = [3.0 3.7 3.9 4.2 5.7 6.6 7.1 6.7 4.5]';
es0 = [1 -2/3]; % endslopes
% no 271
x1 = [17 20 23 24 25 27 27.7]';
y1 = [4.5 7.3 6.1 5.6 5.8 5.4 4.1]';
es1 = [3 -4];
% no 272
x2 = [27.7 28 29 30]';
y2 = [4.1 4.3 4.1 3.0]';
es2 = [1/3 -1.5];
xf0 = linspace(x0(1),x0(end),N);
yf0 = spline(x0,[es0(1); y0; es0(2)],xf0);
xf1 = linspace(x1(1),x1(end),N);
yf1 = spline(x1,[es1(1); y1; es1(2)],xf1);
xf2 = linspace(x2(1),x2(end),N);
yf2 = spline(x2,[es2(1); y2; es2(2)],xf2);
% constrained
plot(xf0,yf0,'b'); hold on
plot(xf1,yf1,'r')
plot(xf2,yf2,'k')
% unconstrained
plot(xf0,spline(x0,y0,xf0),'b:')
plot(xf1,spline(x1,y1,xf1),'r:')
plot(xf2,spline(x2,y2,xf2),'k:')
grid on
If you're trying to debug your spline implementation, it would take me a while to wrap my head around it. I don't immediately see how you're enforcing the endslopes (I'm assuming they're supposed to be?).

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!