[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: integration using Quad or Simpsons 1/8th or Trapezoid of a data fil
From: |
Thomas Shores |
Subject: |
Re: integration using Quad or Simpsons 1/8th or Trapezoid of a data file!! |
Date: |
Wed, 31 Oct 2001 14:52:01 -0600 |
Viswanath P wrote:
> Hi,
>
> I have a data file with discrete points in it.
> I wanted to use the integration of the data sets from
> the range which I will be defining. The usage of
> quad function declares a mathematical function and
> the integration is carried out for the function.
>
> Is it possible to use this for datas also.
>
> thanx,
>
> Viswap.
>
Sure. It depends on the nature and quality of your data. The best way
to use quad would be to create your own function that incorporates
special features of the data. For example, if it is highly accurate,
you might want to define a cubic spline that interpolates the data, then
define a function that evaluates the spline and pass it to quad. On the
other hand, if it isn't highly accurate, or you simply want a quick and
not necessarily highly accurate answer, using quad might be like using a
hammer to swat a fly, so try this: put your data in a 2xm matrix d,
where each row of d is an ordered pair (x,y) of your data in
abscissa/ordinate format. To get an integral using the trapezoidal
method, issue the following commands in Octave:
r = rows(d);
int_dta = sum((d(2:r,1) - d(1:r-1,1)) .* (d(2:r,2) + d(1:r,2)))/2
This will do it.
Example: the integral from 0 to 1 of x^2, which is 1/3:
octave:22> x = 0:0.01:1;
octave:23> y = x .* x;
octave:24> d = [x', y'];
octave:25> r = rows(d);
int_dta = sum((d(2:r,1) - d(1:r-1,1)) .* (d(2:r,2) + d(1:r-1,2)))/2
int_dta = 0.33335
Hope this helps,
Tom Shores
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------