キュービックスプライン補間ソースコード



Cubic Spline Interpolation Source Code



3次スプライン補間関数、自然境界条件、強力なプログラム移植性を実現します。

  • ソースコード
function yy = Interpolation_Spline0(x, y, xx) %{ Function function: cubic spline interpolation method Enter: x: the abscissa of the known point y: the ordinate of the known point xx: interpolation point Output: yy: the function value of the interpolation point Examples: clear clc x = 0 : 0.2 : 2 y = sin(x) xx = 0 : 0.05 : 2 yy= Interpolation_Spline0(x, y, xx) yyy = spline(x, y, xx) plot(x, y, '-r', xx, yy, 'ob', xx, yyy, '*k') %} % = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = n = length(x) a = y(1 : end - 1) b = zeros(n - 1, 1) d = zeros(n - 1, 1) dx = diff(x) dy = diff(y) A = zeros(n) B = zeros(n, 1) A(1, 1) = 1 A(n, n) = 1 for i = 2 : n - 1 A(i, i - 1) = dx(i - 1) A(i, i) = 2*(dx(i - 1) + dx(i)) A(i, i + 1) = dx(i) B(i) = 3*(dy(i) / dx(i) - dy(i - 1) / dx(i - 1)) end c = A B for i = 1 : n - 1 d(i) = (c(i + 1) - c(i)) / (3 * dx(i)) b(i) = dy(i) / dx(i) - dx(i)*(2*c(i) + c(i + 1)) / 3 end [mm, nn] = size(xx) yy = zeros(mm, nn) for i = 1 : mm*nn for ii = 1 : n - 1 if xx(i) >= x(ii) && xx(i)