# spline - function spline(x,y) %Problem 16.1 %interior...

This preview shows pages 1–3. Sign up to view the full content.

function spline(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;

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

View Full Document
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')
This is the end of the preview. Sign up to access the rest of the 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 / 3

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

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

View Full Document
Ask a homework question - tutors are online