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

REQUIRES MATLAB Cubic SPLINE problem. I am having trouble creating two matlib fi

ID: 3108979 • Letter: R

Question

REQUIRES MATLAB Cubic SPLINE problem. I am having trouble creating two matlib files spline_parm.m ands pline_eval.m they could contain code that implement the function. Please provide both code and explain how to implement them using the current code ive provided. Thanks

PLEASE PROVIDE THE TWO SPLINE CODE SNIPS SPLINE_PARM AND SPLINE_EVAL FOR GOOD FEEDBACK AND FULL POINTS WITH COMMENTS THANKS

You will write part of a complete cubic spline program in MATLAB. You will be interpolating the function f(a) r E [0,1n 2] with the interpolation points ark (k/20) (ln 2), k 0, 1, 2, 20 with the endpoint conditions f'(0) 1, and f'(1) 2 You will compute the compute cubic spline that interpolates this data (zk,yk), k 0, 1.2 20. Use the notes and section 11.3 of your text as a guide. I suggest that you write two functions. The first will have the calling sequence function Cb,c,d] spline-parm (x,y).

Explanation / Answer

function [B,C,D]=spline_parm(x,y)
% Check that data are acceptable and, if not, try to adjust them appropriately
[x,y,sizey,endslopes] = chckxy(x,y);
n = length(x); yd = prod(sizey);

% Generate the cubic spline interpolant in ppform

dd = ones(yd,1); B = diff(x); divdif = diff(y,[],2)./B(dd,:);
if n==2
if isempty(endslopes) % the interpolant is a straight line
pp=mkpp(x,[divdif y(:,1)],sizey);
else % the interpolant is the cubic Hermite polynomial
pp = pwch(x,y,endslopes,B,divdif); pp.dim = sizey;
end
elseif n==3&&isempty(endslopes) % the interpolant is a parabola
y(:,2:3)=divdif;
y(:,3)=diff(divdif')'/(x(3)-x(1));
y(:,2)=y(:,2)-y(:,3)*B(1);
pp = mkpp(x([1,3]),y(:,[3 2 1]),sizey);
else % set up the sparse, tridiagonal, linear system b = ?*c for the slopes
b=zeros(yd,n);
b(:,2:n-1)=3*(B(dd,2:n-1).*divdif(:,1:n-2)+B(dd,1:n-2).*divdif(:,2:n-1));
if isempty(endslopes)
x31=x(3)-x(1);xn=x(n)-x(n-2);
b(:,1)=((B(1)+2*x31)*B(2)*divdif(:,1)+B(1)^2*divdif(:,2))/x31;
b(:,n)=...
(B(n-1)^2*divdif(:,n-2)+(2*xn+B(n-1))*B(n-2)*divdif(:,n-1))/xn;
else
x31 = 0; xn = 0; b(:,[1 n]) = B(dd,[2 n-2]).*endslopes;
end
Bt = B(:);
c = spdiags([ [x31;Bt(1:n-2);0] ...
[Bt(2);2*(Bt(2:n-1)+Bt(1:n-2));Bt(n-2)] ...
[0;Bt(2:n-1);xn] ],[-1 0 1],n,n);
  
D=b/c;

function yy= spline_eval(x,y,b,c,d)
%
% yy= spline_eval(x,y,b,c,d) returns the piecewise cubic Hermite
% interpolant f to the given data values Y and the given slopes d
% at the given data sites x. x must be a vector, and d must be of the same
% size as y.

if nargin<4, b = diff(x(:).'); end
n = size(y,1); dxd = repmat(b,n,1);
if nargin<5, c = diff(y,1,2)./dxd; end

n = numel(x);
dzzdx = (c-d(:,1:n-1))./dxd; dzdxdx = (d(:,2:n)-c)./dxd;
dnm1 = n*(n-1);

coefs=[reshape((dzdxdx-dzzdx)./dxd,dnm1,1) ...
reshape(2*dzzdx-dzdxdx,dnm1,1) ...
reshape(d(:,1:n-1),dnm1,1) ...
reshape(y(:,1:n-1),dnm1,1)];

if nargin==2, n = 1; else n = n(:).'; end
dlk=numel(coefs); l=length(x)-1; dl=prod(d)*l; k=fix(dlk/dl+100*eps);
if (k<=0)||(dl*k~=dlk)
error(message('NumberMismatchCoeffs',int2str(l),int2str(d),int2str(dlk)))
end

yy.form = 'yy';
yy.breaks = reshape(x,1,l+1);
yy.coefs = reshape(coefs,dl,k);
yy.pieces = l;
yy.order = k;
yy.dim = n;