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

42 ビュー (過去 30 日間)
Aiden Morcombe
Aiden Morcombe 2024 年 12 月 17 日 23:18
編集済み: John D'Errico 約6時間 前
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.
  3 件のコメント
Torsten
Torsten 約2時間 前
編集済み: Torsten 約2時間 前
Where do you use "radial splines" in the code you posted ?
We must see the code (as plain ascii text, not as a graphics) with which you generated the splines. Then maybe we can tell how to get the gradient.
It seems that "cscvn" would be the appropriate MATLAB function to construct the spline curve through your points in 2d.
John D'Errico
John D'Errico 約1時間 前
編集済み: John D'Errico 約1時間 前
I cannot guess what you mean by radial splines. Maybe this, but I doubt it. Online searches so often yield random crap. ;-)
I truly doubt that is what you intended. But perhaps you are talking about radial basis functions, as are often used for interpolation in multiple dimensions.
Anyway, your question explicitly refers to the tools spline and ppval, which have nothing to do with radial splines in any form.
Regardless, if you have something that is incompatible with the tools in MATLAB, then you will need to spend the time to understand what they are, and to then write some code of your own. At the very least, you need to explain more about what you have than a random piece of jargon, as jargon often means different things to different people.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSpline Postprocessing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by