Question
Note: the cubic spline clamped and natural are given matlab scripts. below is the similar solution for the same question but different x and y
Note: the cubic spline clamped and natural are given matlab scripts. below is the similar solution for the same question but different x and y values:
x = 0:0.5*pi:pi*2.5;
y = sin(x);
xx = linspace(0,pi*2.5) ;
m = length(xx) ;
yy = zeros(1,m);
yy_ac = sin(xx);
for i = 1:m
yy(i) = cubic_spline_natural(x,y,0,0,xx(i));
end plot (x,y,'o',xx,yy,'--', xx, yy_ac, 'b-');
Please fix above code to suit new question and variables
function yint = cubic_spline_clamped(x,y,y_des_1,y_des_n,xx) % Cubic spline interpolatingwith clamped end condition
% Cubic spline interpolating based on n data points (i = 1,2 ..., n) % to determine a value of the dependent variable (yint) at % a given value of the independent variable, xx. % input: % x = independent variable % y = dependent variable % y_des_1 = derivative at the first point % y_des_n = derivative at the last point % xx = value of independent variable at which the % interpolation is calculated
% output: % yint = interpolated value of dependent variable
% Author: Yucang Wang, Central Queensland University, 2016
% Number of data points n = length(x); if length(y)~=n, error('x and y must be same length'); end
%Vector h with subintervals h = zeros(n-1,1); for j = 1:n-1 h(j) = x(j+1)-x(j) ; end
%Vector d, initialized to zero vector d = zeros(n,1);
for i = 2:n-1 d(i) = (y(i+1)-y(i))/h(i)- (y(i)-y(i-1))/h(i-1) ; end
d(1) = (y(2)-y(1))/h(1)-y_des_1 ; d(n) = y_des_n - (y(n)-y(n-1))/h(n-1) ;
%Coefficient matrix A, initialized to nXn zeros A = zeros(n) ;
A(1,1) = h(1)/3 ; A(1,2) = h(1)/6 ; A(n,n) = h(n-1)/3 ; A(n,n-1) = h(n-1)/6 ;
for i = 2:n-1 A(i,i-1) = h(i-1)/6 ; A(i,i) = ( h(i-1)+h(i) )/3 ; A(i,i+1) = h(i)/6 ;
end
% disp(A) ;
% Calculate Vector M M = A\d ;
%disp('M = ') ; disp(M);
yint = 0 ;
for i = 1:n-1 if (xx>= x(i) & xx
function yint = cubic_spline_natural(x,y,y_desdes_1,y_desdes_n,xx) % Lagrange: Lagrange interpolating polynomial % yint = Lagrange(x,y,xx): Uses an (n - 1)-order % Lagrange interpolating polynomial based on n data points % to determine a value of the dependent variable (yint) at % a given value of the independent variable, xx. % input: % x = independent variable % y = dependent variable % xx = value of independent variable at which the % interpolation is calculated % output: % yint = interpolated value of dependent variable n = length(x); if length(y)~=n, error('x and y must be same length'); end
h = zeros(n-1,1); for j = 1:n-1 h(j) = x(j+1)-x(j) ; end
d = zeros(n,1);
for i = 2:n-1 d(i) = (y(i+1)-y(i))/h(i)- (y(i)-y(i-1))/h(i-1) ; end
d(1) = h(1)*y_desdes_1/3.0 ; d(n) = h(n-1)*y_desdes_n/3.0 ;
A = zeros(n) ;
A(1,1) = h(1)/3 ; A(1,2) = 0 ; A(n,n) = h(n-1)/3 ; A(n,n-1) = 0 ;
for i = 2:n-1 A(i,i-1) = h(i-1)/6 ; A(i,i) = ( h(i-1)+h(i) )/3 ; A(i,i+1) = h(i)/6 ;
end
%disp(A) ;
disp (d) ; M = A\d ;
%disp('M = ') ; disp(M);
yint = 0 ;
for i = 1:n-1 if (xx>= x(i) & xx
Given the following data x 2 2.5 3 4 5 6.5 8 y 3-1 3 -403 2 (a) Construct and plot a free cubic spline with zero second derivatives at two ends using user- defined function cubie-spline-naturali (b) Construct and plot a clamped cubic spline with zero first derivatives at two ends user- defined function cubic spline clamped
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started