How to use the Output function of a Cubic Spline Interpolation?

9 次查看(过去 30 天)
I'm supposed to use cubic spline interpolation to approximate a function such as: exp(-1*(X^2)) then integrate the approximated function using different methods.
What I've been able to do so far is this: x=-10:1:10 y=exp(-1*(x.^2)); plot(x,y)
Then using the Curve fitting toolbox the approximated function shown on the graph is: y=-483e-020*x^3 - 0.00286*x^2+4.54e-018*x+0.18
How can I get the same function as an output in order to use it in the integration? Thanks in Advance!

采纳的回答

Dr. Seis
Dr. Seis 2011-12-22
Example:
x = 0:10;
y = 5 + 3*x.^2;
pp = spline(x,y);
int_y = quad(@(xx)ppval(pp,xx),x(1),x(end));
  3 个评论
Dr. Seis
Dr. Seis 2011-12-22
I found (and modified) the example inside "doc ppval"
The part with the "@(xx)" is where a prototype (I am not sure what the technical term for it would be) of the equation is generated. For example, you could define an equation as:
myfun = @(xx)5+3*xx.^2;
Then:
int_y = quad(myfun,0,10); % which will also yield 1050
The equation prototype "myfun" does not store any data. So to see the result of evaluating "myfun" for "x" would give:
myvals = myfun(x); % which would yield [5 8 17 32 53 80 113 152 197 248 305] for x = 1:10

请先登录,再进行评论。

更多回答(2 个)

Mike Hosea
Mike Hosea 2011-12-28
Just wanted to add that the best way of integrating a piecewise-defined function in MATLAB is
quadgk(@(t)ppval(pp,t),x(1),x(end),'Waypoints',x)
where x is, as in the above examples, the vector of locations where the pieces of the function are joined. Supplying these "waypoints" to QUADGK allows it to integrate efficiently over any discontinuities or limited smoothness where the pieces are joined. If there is, in fact, some of that limited smoothness there, you will typically find that using QUADGK like that is significantly faster and more accurate.

Christopher Crawford
Piecewise polynomials like cubic splines can be integrated analytically. This is implemented by 'ppint' (8ve), or 'fnint' (Curvefitting Toolbox), see https://www.mathworks.com/matlabcentral/answers/394457-piecewise-polynomial-integration-ppint
diff(ppval( ppint(pp), x([1,end]) ))

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by