Problem with cubic spline
Show older comments
I got this message:
Unable to perform assignment because the left and right sides have a different number of elements.
Error in NCS_Construct (line 16)
h(1:5) = x(2:n)-x(1:n-1) % lengths of intervals [x_i, x_{i+1}]
Code
x = [0, 1, 2, 3]; % x coordinates of the data points
n = length(x);
f = inline('-exp(-4*x)+exp(-.5*x)');
%f = inline('(x-3).^2'); % the actual function that the data points come from.
y = f(x)
% Use the colon command (:) to construct vectors h and r
% that contain h_i and r_i as components
h(1:5) = x(2:n)-x(1:n-1) % lengths of intervals [x_i, x_{i+1}]
r(1:5) = y(2:n)-y(1:n-1) % differences y_{i+1}-y_i
% Define A to be the zero matrix of size nxn
A = zeros(n);
% Construct A given in (1b) for the natural cubic spline.
% You will have to change this for the clamped cubic spline
A(1,1) = 1; %CHANGE THIS FOR THE CLAMPED CUBIC SPLINE
A(2:n-1, 1:n-2) = diag(h(1:3));
A(2:n-1, 2:n-1) = A(2:n-1, 2:n-1)+diag(2.0*(h(1:n-2)+h(2:n-1)));
A(2:n-1, 3:n) = A(2:n-1, 3:n)+diag(h(2:n-1));
Answers (1)
John D'Errico
on 29 May 2019
THINK! What is n?
x = [0, 1, 2, 3]; % x coordinates of the data points
n = length(x);
n is 4.
Then what do you do?
h(1:5) = x(2:n)-x(1:n-1) % lengths of intervals [x_i, x_{i+1}]
How long is the vector 1:5? What was the error message? How many elements are on the right hand side of the equality? (Hint: 4)
Now read the error message again.
Categories
Find more on Spline Postprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!