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

조회 수: 9 (최근 30일)
Shady
Shady 2011년 12월 22일
답변: Christopher Crawford 2023년 1월 4일
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
Shady
Shady 2011년 12월 22일
Thank you again :)

댓글을 달려면 로그인하십시오.

추가 답변 (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
Christopher Crawford 2023년 1월 4일
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