function [a_inter] = cubic_spline(xi,a,inter)
if length(xi) ~= length(a)
error('vectors xi and a must be of same length');
end
% Plotting points we want to interpolate between:
grid on;
hold on;
title('Cubic Spline Interpolation');
plot(xi,a,'or');
n = length(xi);
% Vector h with subintervals:
h = zeros(n-1,1);
for j = 1:n-1
h(j) = xi(j+1) - xi(j);
end
% Coefficient matrix A:
A = zeros(n);
% Natural Spline boundary conditions:
A(1,1)= 1;
A(n,n) = 1;
for i = 2:n-1
A(i,i-1) = h(i-1);
A(i,i) = 2*(h(i-1)+h(i));
A(i,i+1) = h(i);
end
% Vector b:
b = zeros(n,1);
[it wasn't clear to me if this was supposed to be commented out or not..]:
for i = 2:n-1
b(i) = (3/h(i))(a(i+1)-a(i)) - (3/h(i-1))(a(i)-a(i-1));
end
% Coefficient vector cj:
cj = A\b;
% Coefficient vector bj:
bj = zeros(n-1,1);
for i = 1:n-1
bj(i) = (1/h(i))(a(i+1)-a(i)) - (1/3h(i))(2cj(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) = a(i);
end
% Plotting results:
resolution = 100;
for i = 1:n-1
f = @(x) a(i) + bj(i).(x-xi(i)) + cj(i).(x-xi(i)).2 + dj(i).*(x-xi(i)).3;
xf = linspace(xi(i),xi(i+1),resolution);
plot(xf,f(xf),'b');
end
% Given a value on the x-axis, inter, that we wish to know the y-value of,
% we must first find in which interval inter is. We will use bisection
% search to accomplish this. Interval must be ascending or descending.
jl = 1;
ju = n;
while (ju - jl > 1)
jm = ceil((jl + ju)/2);
if (inter < xi(jm))
ju = jm;
else
jl = jm;
end
end
a_inter = polyval(P(jl,:), inter-xi(jl));
fprintf('\n The interpolated value is: %f \n', a_inter);
plot(inter, a_inter, 'og');
fprintf('The value of bj is %f \n',bj);
fprintf('The value of cj is %f \n',cj);
fprintf('The value of dj is %f \n',dj);
end % [end of function]
Problem 3: Write a MATLAB function called cubic_spline to compute the natural cubic spline for a...
matlab matlab For this problem you will test a few interpolation approaches for the application of generating interpolated data. We'll do this by interpolating data that is sampled from a known mathematical function. The same function can then be used to compute true values at the interpolated points to use in error Consider the following mathematical function (Runge's function): 1+25r2 Write a function mfile that uses this formula to generate a set of data use those points along approaches outlined...
Write a complete set of Matlab instructions that implements cubic spline interpolation on the following data (assume clamped-end-condition). Your code should: a. Generate a single plot, over the range 0<= x <= 5, displaying: i. the individual data points ii. the generated spline function b. Predict the interpolated value at x = 0.5, x = 0.8 and x = 3.7 X у 0 1.7 1 0.3 2 1.8 3 0.4 4 2
USE MATLAB 3. Use MATLAB or Scilab to implement cubic spline, and plot the points and the curve for the following data sets. x-[018 12 27) and y=[12345) Input: (copy and paste the MATLAB or Sclla command in the following box) Output: (copy and paste the output in the following box)
Homework Problem Set 16 x-1)3 for 0 sx$1 a cubic spline? 1. Is the function s (x) = {2(-1)3 for 1 < x 2 -5 +8x-6x2 2x3 for1 sx s 2 Is the function s(x)-[ natural? Let x for j 1,2,3,4 by a calculation on paper 3 a cubic spline? Is it [1,0,1,0]. Determine the natural spline s with s(y)-yi 2. 18x2-2x3 for 2
MATLAB Only MATLAB Only MATLAB Only Indicated in the script below, write a function, called arbpoly3, that accepts two inputs: i) a row vector, called c, containing the coefficients of the polynomial, starting with the coefficient for the lowest degree term, which is the constant term. ii) a row vector, called x, which is the set of real numbers where the polynomial is to be evaluated. The output, y, will be a vector containing the values of the polynomial, evaluated...
Design a MATLAB code to construct a natural cubic spline P(x) that passes though the points (1, 1), (4, 1/4), (7, 1/7), (10, 1/10), (13, 1/13) and (16, 1/16). (a) Use the constructed cubic spline to interpolate x = 3; (b) Find the error e_1 = |1/3 - P(3)|;
3D Cubic B-Spline Interpolation and Error Estimation Theme: Implement and study 3D Cubic Spline Interpolation Input: a set of points in 3D space (data(x,y,z)) Output: Spline, Spline data points and error, 3D graph that shows the original points and the Spline The major steps involved in the B-Spline Fitting and Error Estimation are as mentioned below: 1- Sample the data points from input data. 2- Interpolate to find the position of control points. 3- Use the B-Spline basis function to...
In Matlab, Construct the natural cubic spline (see the LiveScript) to interpolate the Runge function (see Homework 6) using 10, 15, 20, and 25 equispaced nodes.
QUESTION 4 (a) A natural cubic spline that fits the data given by f(3.0) = -5.6790, F (3.1) = -3.6674, f(3.2) = -2.2178 is to be constructed. Write down explicitly the system of equations that need to be used to construct the required natural cubic spline. (b) Consider the nonlinear system x2 + y = 9, 22 + y2 = 25, x, y > 0. Perform one iteration of Newton's method to approximate the solution, starting with (2, 4) as...
QUESTION 4 (10) (a) A natural cubic spline that fits the data given by f(3.0) = -5.6790, (3.1) = -3.6674, f(3.2) = -2.2178 is to be constructed. Write down explicitly the system of equations that need to be used to construct the required natural cubic spline.