% csdriver.m % This is a driver that specifies: % 1. x = Nodes % 2. y = Function values at those nodes % (Currently f(x) = x + 2/x) % 3. dx0 and dxn = Derivative values at the left endpoint, % f'(x_0), and at the right endpoint, f'(x_n), resp. % for a cubic clamped spline. % % The driver then calls csfit.m to compute the % cubic spline function which is stored in a matrix S. % clear workspace clear % --------------------------- % % specify nodes x = [0.5 1.0 1.5 2.0]; % specify function values y = x+2./x; % specify clamped conditions dx0 = 1 - 2./x(1)^2; dxn = 1 - 2./x(4)^2; % --------------------------- % % % Construct Spline S = csfit(x,y,dx0,dxn) % % --------------------------------------------------- % % Evaluate the spline on a very fine grid of points % on each of the three intervals t1 = [x(1):0.01:x(2)]; y1 = polyval(S(1,:),t1-x(1)); t2 = [x(2):0.01:x(3)]; y2 = polyval(S(2,:),t2-x(2)); t3 = [x(3):0.01:x(4)]; y3 = polyval(S(3,:),t3-x(3)); % --------------------------------------------------- % % % --------------------------- % % Evaluate the true function % on a fine grid. trux = [x(1):0.01:x(4)]; truy = trux +2./trux; % --------------------------- % % % Plots plot(t1,y1,'b--',t2,y2,'g--',t3,y3,'m--',x,y,'k*',trux,truy,'r-'); xlabel('x'); ylabel('S(x) and f(x)'); title('f(x)=x+2/x'); legend('Spline 1','Spline 2','Spline3','Ordinates','f(x)');