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

4 visualizaciones (últimos 30 días)
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!

Respuesta aceptada

Dr. Seis
Dr. Seis el 22 de Dic. de 2011
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 comentarios
Dr. Seis
Dr. Seis el 22 de Dic. de 2011
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
Shady
Shady el 22 de Dic. de 2011
Thank you again :)

Iniciar sesión para comentar.

Más respuestas (2)

Mike Hosea
Mike Hosea el 28 de Dic. de 2011
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
Christopher Crawford el 4 de En. de 2023
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]) ))

Categorías

Más información sobre Get Started with Curve Fitting Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by