nspline

# nspline - elseif n~=length(y error'Abscissa and ordinate...

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

function output = nspline(x,y,xx) %NSPLINE Natural cubic spline interpolation. % YY = NSPLINE(X,Y,XX) interpolates the data in vector Y at the % abscissas in vector X using a cubic spline with natural % end conditions, and evaluates it at XX. % PP = NSPLINE(X,Y) returns the pp-form of the cubic spline % interpolant for later use with PPVAL or PPDER. % See also SPLINE, PSPLINE, LSPLINE. % Ref: Kincaid & Cheney, Numerical Analysis, Brooks-Cole, 1991, section 6.4 % Author: Robert Piche, Tampere University of Technology, Finland % (with code from MatLab toolbox routine SPLINE) % Last updated: Jan. 31, 1994 (requires MatLab 4) if nargin==0, help nspline, return, end x = x(:); y = y(:); n = length(x); [x,ind] = sort(x); if n<2 error('There should be at least two data points.') elseif all(diff(x))==0 error('The data abscissas should be distinct.')

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: elseif n~=length(y) error('Abscissa and ordinate vectors should be the same length.') else y = y(ind); if (n==2), % the interpolant is a straight line pp=mkpp(x',[diff(y)./diff(x) y(1)]); else % % Set up the linear system T*z=v for curvatures (K&C p.319) % h = x(2:n) - x(1:n-1); u = 2*( h(1:n-2) + h(2:n-1) ); b = 6*( y(2:n) - y(1:n-1) )./h; if n>3 T = spdiags([ h(2:n-1) u h(1:n-2)],-1:1,n-2,n-2); else T = u; end v = b(2:n-1) - b(1:n-2); % % Solve for curvatures at the internal knots % z = T\v; % % Add the curvatures at the ends % z = [0; z; 0]; % % Compute coefficients of pp form (K&C Eqn 11) % A = ( z(2:n) - z(1:n-1) )./(6*h); B = z(1:n-1)/2; C = b/6 - ( z(2:n)/6 + z(1:n-1)/3 ).*h; pp = mkpp(x',[ A B C y(1:n-1) ]); end if nargin==2, output = pp; else output = ppval(pp,xx); end end...
View Full 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