Numerical integration using Simpson's Rule on

2019-04-14 12:31发布

I am looking for numerical integration with matlab. I know that there is a trapz function in matlab but the precision is not good enough. By searching it online, I found there is a quad function there it seems only accept symbolic expression as input. My data is all discrete and one-dimensional. Is that any way to use quad on my data? Thanks.

4条回答
萌系小妹纸
2楼-- · 2019-04-14 12:39

Integration of a function of one variable is the computation of the area under the curve of the graph of the function. For this answer I'll leave aside the nasty functions and the corner cases and all the twists and turns that trip up writers of numerical integration routines, most of which are probably not relevant here.

Simpson's rule is an approach to the numerical integration of a function for which you have a code to evaluate the function at points within its domain. That's irrelevant here.

Let's suppose that your data represents a time series of values collected at regular intervals. Then you can plot your data as a histogram with bars of equal width. The integrand you seek is the sum of the areas of the bars in the histogram between the limits you are interested in.

You should be able to apply this approach to data sets where the x-axis (ie the width of the bars in the histogram) does not show time, to the situation where the bars are not of equal width, to the situation where the data crosses the x-axis, and most reasonable data sets, quite easily.

The discretisation of your data establishes a limit to the accuracy of the result you can get. If, for example, your time series is sampled at 1sec intervals you can't integrate over an interval which is not a whole number of seconds by this approach. But then, you don't really have the data on which to compute a figure with any more accuracy by any approach. Sure, you can use Matlab (or anything else) to generate extra digits of precision but they don't carry any meaning.

查看更多
Animai°情兽
3楼-- · 2019-04-14 12:49

An answer to your question would be no. The only way to perform numerical integration for data with no expression in Matlab is by using the trapz function. If it's not accurate enough for you, try writing your own quad function as Li-aung said, it's very simple, this may help.

Another method you may try is to use the powerful Curve Fitting Tool cftool to make a fit then use the integrate function which can operate on cfit objects (it has a weird convention, the upper limit is the first argument!). I don't think you will get much accurate answers than trapz, it depends on the fit.

查看更多
冷血范
4楼-- · 2019-04-14 12:49

Use the spline function in MATLAB to interpolate your data, then integrate this data. This is the standard method for integrating data in discrete form.

查看更多
再贱就再见
5楼-- · 2019-04-14 12:55

You can use quadl() to integrate your data if you first create a function in which you interpolate them.

function f = int_fun(x,xdata,ydata)
f = interp1(xdata,ydata,x);

And then feed it to the quadl() function:

integral = quadl(@int_fun,A,B,[],[],x,y) % syntax to pass extra arguments
                                         % to the function
查看更多
登录 后发表回答