Question
Please help me with MATLAB code for this interpolation. Apply parametric cubic spline interpolation to the ellipse x 2 +y 2 /2 = 1. Use
Please help me with MATLAB code for this interpolation. Apply parametric cubic spline interpolation to the ellipse x2+y2/2 = 1. Use 8, 12,16, and 20 more or less evenly spaced points.
I used this code but it is not coming out well
t = linspace(0.12,2*pi(),11);
a = 1;
b = sqrt(2);
x = a.*cos(t);
y = b.*sin(t);
% Boundary_Type: 1 - Natural; 2 - Parabolic Boundary_Type = 1;
interpolate_point = 3;
% Plotting points we want to interpolate between:
grid on;
hold on;
title('Cubic Spline Interpolation');
plot(x,y,'or');
n = length(x);
% Vector h with subintervals:
h = zeros(n-1,1);
for j = 1:n-1
h(j) = x(j+1) - x(j);
end
% Coefficient matrix H:
H = zeros(n);
% Boundary conditions for natural spline:
if Boundary_Type == 1 %Natural
H(1,1) = 1;
H(n,n) = 1;
elseif Boundary_Type == 2 %Parabolic
H(1,1:2) = [1,-1];
H(n,n-1:n) = [-1,1];
else
end
for i = 2:n-1
H(i,i-1) = h(i-1);
H(i,i) = 2*(h(i-1)+h(i));
H(i,i+1) = h(i);
end
% Vector b:
b = zeros(n,1);
for i = 2:n-1
% b(i) = (3/h(i))*(y(i+1)-y(i)) - (3/h(i-1))*(y(i)-y(i-1)); b(i) = 6*(((y(i+1)-y(i))/(x(i+1)-x(i)))-((y(i)-y(i-1))/(x(i)-x(i-1))));
end
% Coefficient vector cj:
cj = H\b;
% Coefficient vector bj:
bj = zeros(n-1,1); for i = 1:n-1 bj(i) = (1/h(i))*(y(i+1)-y(i)) - (1/3*h(i))*(2*cj(i)+cj(i+1));
end
% Coefficient vector dj:
dj = zeros(n-1,1); for i = 1:n-1 dj(i) = (1/(3*h(i))) * (cj(i+1)-cj(i));
end
% Making a matrix P with all polynomials
P = zeros(n-1,4); for i = 1:n-1 P(i,1) = dj(i); P(i,2) = cj(i); P(i,3) = bj(i); P(i,4) = y(i);
end
% Plotting results:
L = 50;
for i = 1:n-1
f = @(x) y(i) + bj(i).*(x-x(i)) + cj(i).*(x-x(i)).^2 + dj(i).*(x-x(i)).^3;
xf = linspace(x(i),x(i+1),L);
plot(xf,f(xf),'b');
end
% Given a value on the x-axis, interpolate_point
k = 1;
l = n;
while (l - k > 1)
jm = ceil((k + l)/2);
if (interpolate_point < x(jm))
l = jm;
else k = jm;
end
end
a_inter = polyval(P(k,:),interpolate_point-x(k));
fprintf(' The interpolated value is: %f ', a_inter);
plot(interpolate_point, a_inter, 'og');
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