matlab - 使用周期样条绘制参数样条曲线
问题描述
因此,我创建了以下 matlab 代码来绘制此函数:
%initializing values
t = [1,2,3,4,5,6,7,8,9,10,11,12,13];
tplot = [1:0.1:13];
x = [2.5,1.3,-0.25,0.0,0.25,-1.3,-2.5,-1.3,0.25,0.0,-0.25,1.3,2.5];
y = [0.0,-0.25,1.3,2.5,1.3,-0.25,0.0,0.25,-1.3,-2.5,-1.3,0.25,0.0];
[a1, b1, c1, d1] = perspline2(t,x);
[a2, b2, c2, d2] = perspline2(t,y);
for i = 1:12
xx = linspace(x(i), x(i+1), 100);
xxx = a1(i) + b1(i)*(xx-x(i)) + c1(i)*(xx-x(i)).^2 ...
+ d1(i)*(xx-x(i)).^3;
yyy = a2(i) + b2(i)*(xx-x(i)) + c2(i)*(xx-x(i)).^2 ...
+ d2(i)*(xx-x(i)).^3;
h3=plot(xxx, yyy, 'r-');
hold on
end
plot(x,y,'k.', 'MarkerSize', 30)
hold off
perspline2() 看起来像这样:
function [a1,b1,c1,d1] = perspline2(xnot,ynot)
x = xnot';
y = ynot';
n = length(x) - 1;
h = diff(x);
diag0 = [1; 2*(h(1:end-1)+h(2:end)); 2*h(end)];
A = spdiags([[h;0], diag0, [0;h]], [-1, 0, 1], n+1, n+1);
% Do a little surgery on the matrix:
A(1,2) = 0;
A(1,end) = -1;
A(end,1) = 2*h(1);
A(end,2) = h(1);
dy = diff(y);
rhs = 6*[0; diff(dy./h); dy(1)/h(1)-dy(end)/h(end)];
m = A \ rhs; % Solve for the slopes, S''(x_i)
% Compute the coefficients of the cubics.
a1 = y;
b1 = dy./h - h.*m(1:end-1)/2 - h.*diff(m)/6;
c1 = m/2;
d1 = diff(m)./h/6;
所以基本上,我知道我必须使用参数样条来找到要绘制的正确点。我t = [1,2,3,4,5,6,7,8,9,10,11,12,13];
用作我的索引。因此,我找到 t 与 x 的三次样条多项式的系数,然后找到 t 与 y 的系数,然后我尝试使用 t 中的值将它们相互绘制以绘制参数曲线。但是,我不断得到这条曲线:
我真的不确定为什么会发生这种情况。
PS 我知道我可以使用 matlab 样条函数,但是当我这样做时,它会导致正确的尖点比其他尖点大一点。我希望所有尖点的大小都相等,并且作业说我们必须使用三次样条。
任何帮助是极大的赞赏。
解决方案
推荐阅读
- django-forms - 如何使 django 表单更加对齐
- java - 了解java内存管理(Spring boot框架)
- python-3.x - MongoEngine 什么都不返回
- java - STAX 使用条件修改嵌套 XML 并为巨大的 XML 写回大约 6 GB
- javascript - 在打字稿中遍历数组
- kubernetes - K0s 是否支持 Helm v.3?
- typescript - 为什么按下按钮时(点击)不触发?
- c# - ASP.NET MVC LINQ:在行动中加入另一个模型
- python-3.x - MacOS:为任何数据库运行 aiosqlite 时出现问题
- python - 登录系统flask TypeError:元组索引必须是整数或切片,而不是str