How to find b spline values?

8 views (last 30 days)
azarang asadi
azarang asadi on 29 Oct 2020
Answered: Bruno Luong on 29 Oct 2020
Hello,
So I have some position data, I fitted a Bspline on the data and differentiate twice to get the accelerations. But now that I want to find the values of accelerations, I'm confused how to get that so I can divide them some number for normalization. So are the y values of a curve fitted by a bspline same as coefficients? I attached the spline, I need to find the y values of 101 points of this figure.
  5 Comments
azarang asadi
azarang asadi on 29 Oct 2020
post it on the answer section so I can accept your answer:)
azarang asadi
azarang asadi on 29 Oct 2020
noooo,,, I'm so sorry, I just unaccepted the answer ! I was confused I though he's the one with the answer, please post it againg and I'll accept yours, the fnval helped me. It was yours? please send it again, see I removed the accepted answer

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 29 Oct 2020
After computing the derivative
spline_position = spaps(time ,x,0,3); % x is a 101 element vector representing postion, I'm fitting a 5th order bspline
acceleration = fnder(spline_position,2); % this gives me a struct, I need to have a 101 acceleration vector corresponsing to each x
run this to evaluate the acceleration
a = fnval(acceleration, time)

More Answers (2)

Mathieu NOE
Mathieu NOE on 29 Oct 2020
hello
my suggestion below
% test data
x = cumsum(rand(1,10));
y = randn(size(x));
pp = spline(x,y);
% fine grid points
xi = linspace(min(x),max(x),300);
yi = ppval(pp,xi); % spline function values at xi
% first and second derivatives (finite difference scheme)
[dy, ddy] = firstsecondderivatives(xi,yi);
% graphics
figure(1);
plot(x,y,'or',xi,yi,'-b')
yyaxis right
plot(xi,ddy,'-')
ylabel('second derivative')
%%%%%%%%%%%%%%%%%%%%%%
function [dy, ddy] = firstsecondderivatives(x,y)
% The function calculates the first & second derivative of a function that is given by a set
% of points. The first derivatives at the first and last points are calculated by
% the 3 point forward and 3 point backward finite difference scheme respectively.
% The first derivatives at all the other points are calculated by the 2 point
% central approach.
% The second derivatives at the first and last points are calculated by
% the 4 point forward and 4 point backward finite difference scheme respectively.
% The second derivatives at all the other points are calculated by the 3 point
% central approach.
n = length (x);
dy = zeros;
ddy = zeros;
% Input variables:
% x: vector with the x the data points.
% y: vector with the f(x) data points.
% Output variable:
% dy: Vector with first derivative at each point.
% ddy: Vector with second derivative at each point.
dy(1) = (-3*y(1) + 4*y(2) - y(3)) / 2*(x(2) - x(1)); % First derivative
ddy(1) = (2*y(1) - 5*y(2) + 4*y(3) - y(4)) / (x(2) - x(1))^2; % Second derivative
for i = 2:n-1
dy(i) = (y(i+1) - y(i-1)) / 2*(x(i+1) - x(i-1));
ddy(i) = (y(i-1) - 2*y(i) + y(i+1)) / (x(i-1) - x(i))^2;
end
dy(n) = (y(n-2) - 4*y(n-1) + 3*y(n)) / 2*(x(n) - x(n-1));
ddy(n) = (-y(n-3) + 4*y(n-2) - 5*y(n-1) + 2*y(n)) / (x(n) - x(n-1))^2;
end
%%%%%%%%%%%%%%%%%%%%%%%
  2 Comments
azarang asadi
azarang asadi on 29 Oct 2020
I need a 6th order spline so the polynomials are of degree 5 and the second derivative of degree 3.
Mathieu NOE
Mathieu NOE on 29 Oct 2020
hi
sure - the spline order is 3 here so evident that the second derivative is a first order function
that does not change the logic of my code. you can use higher order polynomials

Sign in to comment.


Mathieu NOE
Mathieu NOE on 29 Oct 2020
hi
basically same code with different input data and 7th order polynomial interpolation
so the second derivative plot looks nicer
% test data
% x = cumsum(rand(1,10));
% y = randn(size(x));
x = linspace(0,4*pi,10);
y = sin(x);
% pp = spline(x,y);
pp = polyfit(x,y,7);
% fine grid points
xi = linspace(min(x),max(x),300);
% yi = ppval(pp,xi); % spline function values at xi
yi = polyval(pp,xi);
% first and second derivatives (finite difference scheme)
[dy, ddy] = firstsecondderivatives(xi,yi);
% graphics
figure(1);
plot(x,y,'or',xi,yi,'-b')
yyaxis right
plot(xi,ddy,'-')
ylabel('second derivative')
%%%%%%%%%%%%%%%%%%%%%%
function [dy, ddy] = firstsecondderivatives(x,y)
% The function calculates the first & second derivative of a function that is given by a set
% of points. The first derivatives at the first and last points are calculated by
% the 3 point forward and 3 point backward finite difference scheme respectively.
% The first derivatives at all the other points are calculated by the 2 point
% central approach.
% The second derivatives at the first and last points are calculated by
% the 4 point forward and 4 point backward finite difference scheme respectively.
% The second derivatives at all the other points are calculated by the 3 point
% central approach.
n = length (x);
dy = zeros;
ddy = zeros;
% Input variables:
% x: vector with the x the data points.
% y: vector with the f(x) data points.
% Output variable:
% dy: Vector with first derivative at each point.
% ddy: Vector with second derivative at each point.
dy(1) = (-3*y(1) + 4*y(2) - y(3)) / 2*(x(2) - x(1)); % First derivative
ddy(1) = (2*y(1) - 5*y(2) + 4*y(3) - y(4)) / (x(2) - x(1))^2; % Second derivative
for i = 2:n-1
dy(i) = (y(i+1) - y(i-1)) / 2*(x(i+1) - x(i-1));
ddy(i) = (y(i-1) - 2*y(i) + y(i+1)) / (x(i-1) - x(i))^2;
end
dy(n) = (y(n-2) - 4*y(n-1) + 3*y(n)) / 2*(x(n) - x(n-1));
ddy(n) = (-y(n-3) + 4*y(n-2) - 5*y(n-1) + 2*y(n)) / (x(n) - x(n-1))^2;
end

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!