MATLAB 三次样条插值源码

MATLAB 源码:

function yy = Interpolation_Spline0(x, y, xx)
%{
函数功能:三次样条插值法;
输入:
  x:已知点横坐标;
  y:已知点纵坐标;
  xx:插值点;
输出:
  yy:插值点的函数值;
示例:
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) < x(ii + 1)
            j = ii;
            break;
        elseif xx(i) == x(n)
            j = n - 1;
        end
    end
    yy(i) = a(j) + b(j)*(xx(i) - x(j)) + c(j)*(xx(i) - x(j))^2 + d(j)*(xx(i) - x(j))^3;
end

 

转自:

https://blog.csdn.net/u012366767/article/details/81535995

表示感谢!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值