三次样条MATLAB实现前两种
clc,clear
x=input(‘请按照格式[x1,x2,x3…]格式输入y=f(x)函数已知点的横坐标xi=’); %三次样条差值函数
y=input(‘请按照格式[y1,y2,y3…]格式输入y=f(x)函数已知点对应的纵坐标yi=’);
n=size(x,2);
for k=2:n %计算h(i)
h(k-1)=x(k)-x(k-1);
end
for k=1:(n-2) %计算μ和λ
mu(k)=h(k)/(h(k)+h(k+1));
lambda(k)=1-mu(k);
end
A=zeros(n,n);
B=zeros(n,1);
for k=2:(n-1)
A(k,k)=2;
A(k,k+1)=lambda(k-1);
A(k,k-1)=mu(k-1);
end
fprintf(‘边界条件类型选择:\n1.已知f(a)和f(b)的二阶导数\n2.已知f(a)和f(b)的一阶导数\n’);
in=input(‘请输入对应序号:’);
if in1
M(1)=input(‘请输入f(a)的二阶导数值:’);
M(n)=input(‘请输入f(b)的二阶导数值:’);
A(1,1)=2;A(1,2)=0;A(end,end)=2;A(end,end-1)=0;
for k=2:1:(n-1)
B(k,1)=6*(((y(k+1)-y(k))/(x(k+1)-x(k))-(y(k)-y(k-1))/(x(k)-x(k-1)))/(x(k+1)-x(k-1)));
end
B(1,1)=M(1);
B(n,1)=M(N);
A
B
inv(A)*B
end
if in2
y0=input(‘请输入f(a)的一阶导数值:’);
yn=input(‘请输入f(b)的一阶导数值:’);
A(1,1)=2;A(1,2)=1;A(end,end)=2;A(end,end-1)=1;
for k=2:1:(n-1)
B(k,1)=6*(((y(k+1)-y(k))/(x(k+1)-x(k))-(y(k)-y(k-1))/(x(k)-x(k-1)))/(x(k+1)-x(k-1)));
end
B(1,1)=6/h(2)((y(2)-y(1))/(x(2)-x(1))-y0)
B(n,1)=6/h(n-1)(yn-(y(n)-y(n-1))/(x(n)-x(n-1)))
end
A
B
inv(A)*B
三次样条
最新推荐文章于 2024-08-06 09:47:44 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)