Question: 1 . ( a ) Write a MATLAB function ( without loops ) to perform cubic spline interpolation using the not a knot end conditions.

1.(a) Write a MATLAB function (without loops) to perform cubic spline interpolation using the not a knot end conditions. The first line of your code should be
function s = myspline(y,r)
where y is the column vector of data values, and the positive integer r is the interpolation factor, which means that the spline will be evaluated at r1 uniformly spaced points between each pair of adjacent data points. The spline values will appear in the column vector s. Incorporate the trid function from the last homework to solve the tridiagonal matrix equation that arises in this problem. (b) Let y = randn(1000000,1), r =3, x =(1:3:2999998), xx =(1:2999998). Check your myspline function by computing norm(s-sm)/norm(sm), where
sm = spline(x,y,xx)
and spline is MATLABs built-in spline interpolation function. (c) Using the same values as in (b), how many times faster is myspline compared to spline?
The function code used in last class is:
function x = trid(a, b, g)
n = length(g);
L = zeros(n-1,1);
D = zeros(n,1);
x = zeros(n,1);
D(1)= a;
for i =2:n
L(i-1)= b / D(i-1);
D(i)= a - D(i-1)*(L(i-1)^2);
end
x(1)= g(1)/a;
for i =2:n
x(i)=(g(i)-b*x(i-1))/D(i);
end
for i = n-1:-1:1
x(i)= x(i)- L(i)*x(i+1);
end
end
The test code:
clear all;
format long;
n = randi([10,50],1,1);
a = randi([11,20],1,1);
b = randi([-5,5],1,1);
c =[a b zeros(1, n-2)];
r =[a b zeros(1, n-2)];
A = toeplitz(c, r);
g = randi([1,10], n,1);
x = trid(a, b, g);
x_hat_1= A\g; % backslash
opts.POSDEF = true;
opts.SYM = true;
x_hat_2= linsolve(A, g, opts); % linsolve
error1= sum(abs(x - x_hat_1))/n;
error2= sum(abs(x - x_hat_2))/n;
fprintf('The average difference between x and x_hat_1 is:');
disp(error1);
fprintf('The average difference between x and x_hat_2 is:');
disp(error2); Please don't use AI . I will give upvote if it is giving output and without using CHAT GPT

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!