How do I find the equation of points on a spline curve?
42 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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?
0 Commenti
Risposte (2)
David Goodmanson
il 17 Dic 2024 alle 23:51
Modificato: David Goodmanson
il 18 Dic 2024 alle 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)
0 Commenti
John D'Errico
il 18 Dic 2024 alle 13:08
Modificato: John D'Errico
il 18 Dic 2024 alle 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)
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)
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)
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 Commenti
Torsten
circa 6 ore fa
Modificato: Torsten
circa 6 ore fa
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
circa 6 ore fa
Modificato: John D'Errico
circa 6 ore fa
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.
Vedere anche
Categorie
Scopri di più su Spline Postprocessing in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!