How do I find the equation of points on a spline curve?

33 次查看(过去 30 天)
I have a cam contour that I generated using Solidworks that I exported as a series of points and fit a spline to using the spline and ppval commands. I need to analyse this cam mathematically, and to do so I need to find the gradient at many points on it, hence needing the equation. I have seen comments about getting the equation from the coefficients and breaks of the spline output, but I don't understand how it applies as when I have manually looked at the polynomial resulting from that and compared it to the figure it doesn't appear to line up. Can anyone help or explain?

回答(2 个)

David Goodmanson
David Goodmanson 2024-12-17,23:51
编辑:David Goodmanson 2024-12-18,9:14
Hi Aiden,
you have to remember that for each section of the spline, the polynomial is local to that section, i.e. the argument of the polynomial is 0 at the lower boundary of each section So if a section runs from, say,
xs = pp.breaks(15); to xf = pp.breaks(16);
with c = pp.coefs(15,:)
and, say x1 = linspace(xs,xf,20);
then in that section
y = polyval(c,x1-xs)
not
y = polyval(c,x1)

John D'Errico
John D'Errico 2024-12-18,13:08
编辑:John D'Errico 2024-12-18,13:16
This is a question that gets aslked many many times about splines. And, well, you can't easily get a simple equation, as there is no one equation. Depending on how many breaks there are in the spline, you may have dozens, or thousands of different "equations", or more. A spline is composed of pieces of polynomial segments, one between each pair of breaks. nd even then it gets a little tricky. So I'm sorry to say it is rarely of any value to try to write down "the" equation.
Instead, you can use tools like fnder to differentiate the spline for you, creating a new spline that can then be evaluated to produce the derivative at any point.
For example...
x = linspace(0,2*pi,8);
y = cos(x);
spl = spline(x,y)
spl = struct with fields:
form: 'pp' breaks: [0 0.8976 1.7952 2.6928 3.5904 4.4880 5.3856 6.2832] coefs: [7x4 double] pieces: 7 order: 4 dim: 1
fplot(@(X) fnval(spl,X),[0,2*pi])
hold on
plot(x,y,'ro')
hold off
grid on
title 'Data, with an interpolating spine through it'
Now differentate the spline, creating a new spline curve that is the derivative of spl. It will be a lower order curve.
splder = fnder(spl)
splder = struct with fields:
form: 'pp' breaks: [0 0.8976 1.7952 2.6928 3.5904 4.4880 5.3856 6.2832] coefs: [7x3 double] pieces: 7 order: 3 dim: 1
As you can see, MATLAB knows the derivative of a spline that was originally cubic (degree == 4) is now made from quadratic segments.
We can now evaluate this derivative at any point.
fnval(splder,4)
ans = 0.7601
fplot(@(X) fnval(splder,X),[0,2*pi])
grid on
title 'Derivative of the spline'
And you should see this will look a lot like a sine wave. Actually, -sin(x), to be pedantic. Not perfect, since we had only 8 data points.
Finally, yes, you can extract those segments. My SLM toolboix, on the file exchange, as a function that will provide to you those segments as polynomials, if you desperately wanted, The function is called SLMPAR.
C = slmpar(spl,'symabs');
C{2,1}
ans =
0.14827585699644890704362865108124*x^3 - 0.69064468900003539442167266315664*x^2 + 0.080993821270724186689449197729118*x + 1.0
And so we see the cubic polynomial produced in the fiirst segment of the spline. You will need to download my SLM toolbox of course to use it. Easier just to use fnder.

类别

Help CenterFile Exchange 中查找有关 Spline Postprocessing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by