In: Advanced Math
The question is to use Matlab to find the clamped cubic spline v(x) that interpolates a function f(x) that satisfies: f(0)=0, f(1)=0.5, f(2)=2, f(3)=1.5, f'(0)=0.2, f'(3)=-1 and then plot v(x).
This is my code so far:
x = [0 1 2 3];
y = [0 0.5 2 1.5];
cs = spline(x,[0 y 0]);
xx = linspace(0,3,101);
figure()
plot(x,y,'o',xx,ppval(cs,xx),'-');
IS THIS RIGHT? HOW CAN I GET MATLAB TO GIVE ME THE EQUATION OF v(x)?
MATLAB Code:
close all
clear
clc
x = [0 1 2 3];
y = [0 0.5 2 1.5];
cs = spline(x, [0.2 y -1]); % Slopes at end-points: f'(0) = 0.2 and
f'(3) = -1
xx = linspace(0, 3, 101);
figure, plot(x, y, 'o', xx, ppval(cs, xx), '-')
xlabel('x'), ylabel('v(x)')
title('Clamped Spline Interpolation')
% Here cs.coefs is matrix of coefficients of dimensions
MxN
% i-th row of cs.coefs contains coefficients of a polynomial of
order N for the i-th interval
fprintf('Polynomial [x = 0 to 1]:\n\t')
fprintf('p1(x) = (%.4f)*x^3 + (%.4f)*x^2 + (%.4f)*x + (%.4f)\n',
cs.coefs(1,:))
fprintf('Polynomial [x = 1 to 2]:\n\t')
fprintf('p2(x) = (%.4f)*(x - 1)^3 + (%.4f)*(x - 1)^2 + (%.4f)*(x -
1) + (%.4f)\n', cs.coefs(2,:))
fprintf('Polynomial [x = 2 to 3]:\n\t')
fprintf('p3(x) = (%.4f)*(x - 2)^3 + (%.4f)*(x - 2)^2 + (%.4f)*(x -
2) + (%.4f)\n', cs.coefs(3,:))
% Note that for polynomials p2 and p3, the terms (x - 1) and (x
- 2) are
% used. It's basically (x - xi), where xi is the starting value of
x in the
% corresponding interval.
Output:
Plot: