Matlab integration of numerical data

I have numerical data for function f, calculated for a vector x (not a defined function of x). Can I create a functional form out of this so that I can use something like f(y) in further calculations and integrals ?
x = 0:1:10; F is calculated for eac x. Example : Want to integrate f(x+c) between some limits, (not 0,10) and for any constant c.

답변 (2개)

Star Strider
Star Strider 2021년 4월 8일

0 개 추천

The cumtrapz funciton will likely do what you want, although I am not certain what result you want.
John D'Errico
John D'Errico 2021년 4월 8일
편집: John D'Errico 2021년 4월 8일

0 개 추천

In order for it to be truly general, you will need to interpolate the "function". For example, we might do this:
x = 0:10;
y = sin(x); % some function
Now, we wish to integrate y, but only based on the values we have generated. Remember that integrating beyond the linits of our data will be bad idea, because then we are forced to extrapolate the function. And THAT is a bad idea, unless our extrapolant is one chosen carefully. A spline will be a terribly poor tool to extrapolate.
f = spline(x,y);
fint = fnint(f); % this lives in the curve fitting toolbox.
Now, assume we wish the integral of y, between x and x + c. In this example, I'll use x=0.1223, and c=4.75.
fintxc = @(x,c) fnval(fint,x+c) - fnval(fint,x);
fintxc(0.1223,4.75)
ans = 0.8435
How well did we do? Remember, this can be no more than an approximation. We can use integral on the original function to do the work, although I could be more intelligent, since I know the integral of the sine function. I'll take the lazy way here.
integral(@sin,0.1223,0.1223 + 4.75)
ans = 0.8333
Which given the coarseness of the original sample, is not at all bad.

댓글 수: 3

Sib RV
Sib RV 2021년 4월 8일
Ok, thanks. Some interpolation need to be done, which is inaccurate and not a good idea.
Why I did not try the obvious integration: F is not some well defined function of x. It is the result of some calculations done in several steps.
John D'Errico
John D'Errico 2021년 4월 8일
It depends on how nasty is the function, and how densely sampled. Interpolation can be very well behaved. No, you probably don't want to do linear interpolation, but you could, and it would provide an integratino estimate that is essentially equivalent to a trapezoidal rule.
That is why a spline is a good choice, since it is effectively a higher orer interpolant. Or you might want to use a shape preserving interpolant, like pchip. Finally, you could even use a tool like my SLM toolbox, which can produce spline interpolants that will be well-behaved.
Sib RV
Sib RV 2021년 4월 8일
Thank you.

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

카테고리

도움말 센터File Exchange에서 Interpolation에 대해 자세히 알아보기

질문:

2021년 4월 8일

댓글:

2021년 4월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by