Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Below is what I have come up with: x=[2 2.5 3 4 5 6.5 8]; y=[3 -1 3 -4 0 3 2]; x

ID: 2247294 • Letter: B

Question

Below is what I have come up with:

x=[2 2.5 3 4 5 6.5 8];
y=[3 -1 3 -4 0 3 2];
xx=linspace(2,8);
m=length(xx);
yy=zeros(1,m);

yy_ac=y(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-');       

                                                                                                                                                                                                           My problem is highlighted in bold above, what do I put there to fix make this code work? below is the cubic pline natural code source, save this off as a .m file to use. Please help!

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(1);
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) = 1(j+1)-1(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 = Ad ;

%disp('M = ') ; disp(M);


yint = 0 ;

for i = 1:n-1
   
    if (xx>= x(i) & xx<= x(i+1) )
       
        %disp ('xx = ' ); disp (xx) ;
        %disp ('i= '); disp (i) ;
        %disp ('xi= ') ; disp (x(i)) ;
        %disp ('xi+1= ') ; disp (x(i+1)) ;
       
        %disp('Mi= '); disp(M(i)) ;
        %disp('Mi+1= '); disp(M(i+1)) ;
        %disp ('yi= ') ; disp (y(i)) ;
        %disp ('yi+1= ') ; disp (y(i+1)) ;
        %disp ('hi= ') ; disp (h(i)) ;
       
        yint1 = M(i)*(x(i+1)-xx)^3/(6*h(i)) ;
        yint2 = M(i+1)*(xx-x(i))^3/(6*h(i)) ;   
        yint3 = ( y(i)-M(i)*h(i)^2/6.0 )*( x(i+1)-xx )/h(i) ;    
        yint4 = ( y(i+1)-M(i+1)*h(i)^2/6.0 )*( xx-x(i) )/h(i) ;
        yint = yint1+yint2+yint3+yint4 ;     
       
       
        %disp('yint = ') ; disp(yint);
       
    end
   
    coef_x11 = ( M(i+1)*x(i)^2- M(i)*x(i+1)^2 )/(2*h(i)) ;
    coef_x12 = ( y(i+1)- y(i) )/h(i) ;
    coef_x13 = ( M(i)- M(i+1) )*h(i)/6 ;
   
    coef_x01 = ( M(i)*x(i+1)^3 - M(i+1)*x(i)^3 )/(6*h(i)) ;
    coef_x02 = ( x(i+1)*y(i) - x(i)*y(i+1) )/h(i) ;
    coef_x03 = ( M(i+1)*x(i)-M(i)*x(i+1) )*h(i)/6 ;
   
    coef_x3 = ( M(i+1)- M(i) )/(6*h(i)) ;
    coef_x2 = ( x(i+1)*M(i)- x(i)*M(i+1) )/(2*h(i)) ;
    coef_x1 = coef_x11+coef_x12+coef_x13 ;
    coef_x0 = coef_x01+coef_x02+coef_x03 ;
   
   
    fprintf('i = ') ; fprintf('%3d',i); fprintf('   coef of s_');
    fprintf('%1d',i); fprintf(' are: ') ;
   
    fprintf('coef_x^3 = ') ;
    fprintf('%12.8f ',coef_x3);
   
    fprintf('coef_x^2 = ') ;
    fprintf('%12.8f ',coef_x2);
   
    fprintf('coef_x^1 = ') ;
    fprintf('%12.8f ',coef_x1);
   
    fprintf('coef_x^0 = ') ;
    fprintf('%12.8f ',coef_x0);
   
end

Given the following data 2 2.5 3 4 56.5 8 3-13-403 (a) Construct and plot a free cubic spline with zero second derivatives at two ends using user defined function cubic spline natural:

Explanation / Answer

x=[2 2.5 3 4 5 6.5 8];
y=[3 -1 3 -4 0 3 2];
xx=linspace(2,8);
m=length(xx);
yy=zeros(1,m);
yy_ac=y(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-');

In the code above yy_ac=y(xx); means that y(index xx)

like yy_ac=y(1)=3

but the values in xx does not match with the index number of y that is why it is giving the error

and the length of y does not match with the length of xx which is 100 and you need to put the array in yy_ac such that its length becomes 100 because to plot it on graph the lenght of both axis must be same

x=[2 2.5 3 4 5 6.5 8];
y=[3 -1 3 -4 0 3 2];
length(x)
length(y)

xx=linspace(2,8);
m=length(xx);
yy=zeros(1,m);
%yy_ac=xx;
for i=1:m
yy(i)=cubic_spline_natural(x,y,0,0,xx(i));
end
plot (x,y,'o',xx,yy,'--')

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(1);Error

n = length(x);
if length(y)~=n, error('x and y must be same length'); end
h = zeros(n-1,x);
for j = 1:n-1

%h(j) = 1(j+1)-1(j);Error
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 = Ad ;
%disp('M = ') ; disp(M);

yint = 0 ;
for i = 1:n-1

if (xx>= x(i) & xx<= x(i+1) )

%disp ('xx = ' ); disp (xx) ;
%disp ('i= '); disp (i) ;
%disp ('xi= ') ; disp (x(i)) ;
%disp ('xi+1= ') ; disp (x(i+1)) ;

%disp('Mi= '); disp(M(i)) ;
%disp('Mi+1= '); disp(M(i+1)) ;
%disp ('yi= ') ; disp (y(i)) ;
%disp ('yi+1= ') ; disp (y(i+1)) ;
%disp ('hi= ') ; disp (h(i)) ;

yint1 = M(i)*(x(i+1)-xx)^3/(6*h(i)) ;
yint2 = M(i+1)*(xx-x(i))^3/(6*h(i)) ;   
yint3 = ( y(i)-M(i)*h(i)^2/6.0 )*( x(i+1)-xx )/h(i) ;
yint4 = ( y(i+1)-M(i+1)*h(i)^2/6.0 )*( xx-x(i) )/h(i) ;
yint = yint1+yint2+yint3+yint4 ;   


%disp('yint = ') ; disp(yint);

end

coef_x11 = ( M(i+1)*x(i)^2- M(i)*x(i+1)^2 )/(2*h(i)) ;
coef_x12 = ( y(i+1)- y(i) )/h(i) ;
coef_x13 = ( M(i)- M(i+1) )*h(i)/6 ;

coef_x01 = ( M(i)*x(i+1)^3 - M(i+1)*x(i)^3 )/(6*h(i)) ;
coef_x02 = ( x(i+1)*y(i) - x(i)*y(i+1) )/h(i) ;
coef_x03 = ( M(i+1)*x(i)-M(i)*x(i+1) )*h(i)/6 ;

coef_x3 = ( M(i+1)- M(i) )/(6*h(i)) ;
coef_x2 = ( x(i+1)*M(i)- x(i)*M(i+1) )/(2*h(i)) ;
coef_x1 = coef_x11+coef_x12+coef_x13 ;
coef_x0 = coef_x01+coef_x02+coef_x03 ;


fprintf('i = ') ; fprintf('%3d',i); fprintf(' coef of s_');
fprintf('%1d',i); fprintf(' are: ') ;

fprintf('coef_x^3 = ') ;
fprintf('%12.8f ',coef_x3);

fprintf('coef_x^2 = ') ;
fprintf('%12.8f ',coef_x2);

fprintf('coef_x^1 = ') ;
fprintf('%12.8f ',coef_x1);

fprintf('coef_x^0 = ') ;
fprintf('%12.8f ',coef_x0);

end

Here i highlighted some errors which i resolved Please check and comment if this helped you or not??

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote