# the inverse of spapi function

조회 수: 11(최근 30일)
Foufa.h 12 Oct 2019
답변: John D'Errico 13 Oct 2019
Hi, I have a data of (knots and coefs of function of order 5) and i want to plot the function .

#### 댓글 수: 5

표시 이전 댓글 수: 2
John D'Errico 12 Oct 2019
Sorry, but how does a function evolve? Does it start out as a constant, then gradiually become linear, then higher order? Do functions have DNA at all?
Seriously, what are you asking? Evolution makes no sense in this context.
If all you want to do is plot the function, then fplot will do nicely.
If you are asking for an explanation of what a spline is, and what the parameters mean (that is, knots/breaks & coefficients) I'd strongly suggest reading the first few chapters of deBoors book, "A Practical Guide to Splines".
Foufa.h 13 Oct 2019
In my case I dont know the function. I only knows the order =5 and the vector of Knots and vector of coefs.
There is a function of matlab called
pp = mkpp(breaks,coefs) which Make piecewise polynomial. Maybe it is the solution of my problem.
Thank you very much.
John D'Errico 13 Oct 2019
Ok, so you built a spline, apparntly using spapi? And now, you want to know something about the spline?
NO, mkpp will do absolutely nothing for you. It does not apply to existing splines.
Do you want to know the actually polynomial pieces in each segment?

### 답변(1개)

John D'Errico 13 Oct 2019
I think this may be what you are asking for.
x = 1:7;
y = rand(1,7);
S = spapi(5,x,y)
S =
struct with fields:
form: 'B-'
knots: [1 1 1 1 1 3.5 4.5 7 7 7 7 7]
coefs: [0.95949 2.2569 -1.8189 2.4593 0.40081 0.66712 0.75774]
number: 7
order: 5
dim: 1
The result is a 5th order B-spline. It is simple enough to convert to a pp form though, which is a bit more readable.
pp = fn2fm(S,'pp')
pp =
struct with fields:
form: 'pp'
breaks: [1 3.5 4.5 7]
coefs: [3×5 double]
pieces: 3
order: 5
dim: 1
Here we see the result is a set of 3 polynomial segments. There are 4 breaks, so between each pair of breaks you have one polynomial segment. Be careful, as those polynomials are intended to be used relative to the break at the lower end of the corresponding interval. Thus, we see the first polynomial segment as:
format long g
pp.coefs(1,:)
ans =
-0.275001995156781 1.93576726441605 -4.04042647382373 2.07590947732814 0.959492426392903
It lives on the interval [1,3.5], thus the first knot interval from this set:
pp.breaks
ans =
1 3.5 4.5 7
Can we use that polynomial to evaluate the spline? Of course. As you can see, these two calls are equivalent.
fnval(S,2.5)
ans =
0.123413993204698
polyval(pp.coefs(1,:),2.5 - pp.breaks(1))
ans =
0.123413993204696
Is this what you are asking for? Of that, I have no clue.