How can I do a spline interpolation with tangent continuity
7 次查看(过去 30 天)
显示 更早的评论
Hi every one this is my first post on this forum so please don't hate me for formating etc.
I'm trying to derive data from a spline interpolation (twice). The problem I run into here is that there are always kinks in the interpolated data which leads to problems with the derivation.
My question is: Is there a way to do a spline (or other) interpolation of my data where the slope at every interpolated point stays the same (there are no kinks in the interpolated data, tangent continuity at every spline point) ?
This is so i can do a derivation twice to find linear portions of my data.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/592630/image.jpeg)
Thanks for your help in advance.
load('test.mat');
HV4502=table2array(HV450102);
x452=linspace(0,HV4502(end,2),200);
HV4502S=spline(HV4502(1:end,2),HV4502(1:end,1),x452);
plot(x452,HV4502S,'g');
hold on;
plot(HV4502(1:end,2),HV4502(1:end,1),'r--');
hold on;
G4502=gradient(HV4502S)./gradient(x452);
GG452=gradient(G4502)./gradient(x452);
plot(x452,GG452,'b');
回答(1 个)
darova
2021-8-8
What about this? I calculated tangent at each point and added points
dx = 0.1; % adding points distance
x = 0:5;
y = sin(x); % origial data
x1 = 0:.1:5;
y1 = spline(x,y,x1); % refine the data
dy = atan(diff(y)./diff(x)); % find tangent
a = diff(dy)/2 + dy(1:end-1); % find tangent
k = tan(a); % find tangent
x2 = zeros(1,2*numel(x)-2);
x2([1 end]) = x([1 end]); % first and last points
tmp = [x(2:end-1)-dx;x(2:end-1)+dx];% adding points
x2(2:end-1) = tmp(:); % one row
y2 = x2*0;
y2([1 end]) = y([1 end]); % first and last points
for i = 1:length(k)
y2(2*i) = y(i+1) - k(i)*dx; % left point
y2(2*i+1) = y(i+1) + k(i)*dx; % right point
end
x3 = x1;
y3 = spline(x2,y2,x3); % create new spline
plot(x,y,'r')
hold on
plot(x1,y1,'r')
plot(x2,y2,'.b')
plot(x3,y3,'b')
hold off
legend('original data','original spline','added points','spline through added points')
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!