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

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

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;

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;
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern