splin - function splin(x,y) %Problem 16.1 %interior knots,...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: function splin(x,y) %Problem 16.1 %interior knots, i=2,3,4,.....n-2 %finite divided difference f[x1,x2]=(f(x1)-f(x2))/(x1-x2) %(a) Fit data with a cubic spline with natural end conditions plot(x,y,'o k'); hold on j=length(x); for i=1:j-1 h(i)=x(i+1)-x(i); end for i=2:j-1 f2(i-1)=(y(i)-y(i-1))/(x(i)-x(i-1)); f1(i-1)=(y(i+1)-y(i))/(x(i+1)-x(i)); b(i-1)= 3*(f1(i-1)-f2(i-1)); end A=[1 0 0 0 0 0;h(1) 2*(h(1)+h(2)) h(2) 0 0 0; 0 h(2) 2*(h(2)+h(3)) h(3) 0 0; 0 0 h(3) 2*(h(3)+h(4)) h(4) 0;0 0 0 h(3) 2*(h(3)+h(4)) h(4) ; 0 0 0 0 0 1]; % WHY 2*(h(1)+h(2)) and not 2*(h(1)-h(2)) as equation 16.25 suggest? B=[0 b(1) b(2) b(3) b(4) 0]'; c=A\B; for i=1:j-1 d(i)=(c(i+1)-c(i))/(3*h(i)); b(i)=(y(i+1)-y(i))/h(i)-(h(i)/3)*(2*c(i)+c(i+1)); end la=linspace(x(1),x(2),50); lb=linspace(x(2),x(3),50); lc=linspace(x(3),x(4),50); ld=linspace(x(4),x(5),50); le=linspace(x(5),x(6),50); s1=y(1)+b(1).*(la-x(1))+c(1).*(la-x(1)).^2+d(1).*(la-x(1)).^3; s2=y(2)+b(2).*(lb-x(2))+c(2).*(lb-x(2)).^2+d(2).*(lb-x(2)).^3; s3=y(3)+b(3).*(lc-x(3))+c(3).*(lc-x(3)).^2+d(3).*(lc-x(3)).^3; s4=y(4)+b(4).*(ld-x(4))+c(4).*(ld-x(4)).^2+d(4).*(ld-x(4)).^3; s5=y(5)+b(5).*(le-x(5))+c(5).*(le-x(5)).^2+d(5).*(le-x(5)).^3; plot(la,s1,'g',lb,s2,'g',lc,s3,'g',ld,s4,'g',le,s5,'g') title('(a) Cubic Spline with natural end conditions'); xlabel('x'); ylabel('f(x)'); legend('data points','fit using cubic spline'); hold off pause %------------------------------------------------------%Fit data with a cubic spline with not-a-knot end conditions %v = ppval(pp,xx) returns the value of the piecewise polynomial f, %contained in pp, at the entries of xx. %pp = spline(x,Y) returns the piecewise polynomial form of the cubic spline %interpolant for later use with ppval and the spline utility unmkpp. x must %be a vector. pp=spline(x,y); gg=linspace(min(x),max(x),101); yy=ppval(pp,gg); plot(x,y,'o',gg,yy); title('(b) cubic spline with not-a-knot end conditons'); xlabel('x'); ylabel('f(x)'); pause %------------------------------------------------------%Piecewise cubic hermite interpolation ...
View Full Document

This note was uploaded on 09/27/2011 for the course EGM 3344 taught by Professor Raphaelhaftka during the Spring '09 term at University of Florida.

Page1 / 2

splin - function splin(x,y) %Problem 16.1 %interior knots,...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online