how to do trapz on a data file

View: New views
5 Messages — Rating Filter:   Alert me  

how to do trapz on a data file

by warpino :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi everyone,

I've just installed octave 3.2.3 and I'm a complete newbie. What I need most is a quick command to get the area under a curve. The curve is made of columns 1 and 3 of a file like this which I use to plot with gnuplot:

1.000000000000e+02 0.000000000000e+00 6.917126169316e-04
9.798418000000e+01 0.000000000000e+00 6.923512999807e-04
9.596836000000e+01 0.000000000000e+00 6.947493914720e-04
9.400841000000e+01 0.000000000000e+00 7.045027864544e-04

and so forth...

I use to import the file in a spreadsheet and then do a trapezoidal integration "by hand"

sum((y[i-1]+y[i])*(x[i]-x[i-1])/2) for i=2:n

being i the row and n the number of rows.

But this is very time wasting when doing loads of parametric analyses. How can I get this quickly with octave?
There would be any difference if the file had a header?

Thanks for your help,

N.


_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: how to do trapz on a data file

by Jaroslav Hajek-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, Oct 17, 2009 at 6:19 PM, Nunzio Losacco
<nunzio.losacco@...> wrote:

> Hi everyone,
>
> I've just installed octave 3.2.3 and I'm a complete newbie. What I need most
> is a quick command to get the area under a curve. The curve is made of
> columns 1 and 3 of a file like this which I use to plot with gnuplot:
>
> 1.000000000000e+02 0.000000000000e+00 6.917126169316e-04
> 9.798418000000e+01 0.000000000000e+00 6.923512999807e-04
> 9.596836000000e+01 0.000000000000e+00 6.947493914720e-04
> 9.400841000000e+01 0.000000000000e+00 7.045027864544e-04
>
> and so forth...
>
> I use to import the file in a spreadsheet and then do a trapezoidal
> integration "by hand"
>
> sum((y[i-1]+y[i])*(x[i]-x[i-1])/2) for i=2:n
>
> being i the row and n the number of rows.
>
> But this is very time wasting when doing loads of parametric analyses. How
> can I get this quickly with octave?
> There would be any difference if the file had a header?
>
> Thanks for your help,
>
> N.
>

data = load ("datafile");
trapz (data (:,1), data (:,3))


--
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz
_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: how to do trapz on a data file

by Jordi GutiƩrrez Hermoso :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2009/10/17 Nunzio Losacco <nunzio.losacco@...>:
> There would be any difference if the file had a header?

Unless this has changed differently, Octave does two passes on a file
in order to determine the size and well-formness of the data if there
is no header. So it can be slightly faster to load data from a file if
you put a header in it. For an example of what should go in the
header, use the save command to write any matrix to file.
_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: how to do trapz on a data file

by warpino :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thank you all for your help,


now I have to find a way to make a simple command to do it all passing filename.txt as argument.
Even better if I can program it to scan some folders and do it on filename.txt on those folders.
Is it possible to make a command which can be run from the shell or even better from gnuplot shell? (sorry if that's a bit OT)

NL

_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave

Re: how to do trapz on a data file

by Carlo de Falco-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On 18 Oct 2009, at 13:32, Nunzio Losacco wrote:

> Thank you all for your help,
>
>
> now I have to find a way to make a simple command to do it all  
> passing filename.txt as argument.
> Even better if I can program it to scan some folders and do it on  
> filename.txt on those folders.
> Is it possible to make a command which can be run from the shell or  
> even better from gnuplot shell? (sorry if that's a bit OT)
>
> NL

To make an octave script executable as a shell command just add the  
shebang line:

#!/path/to/octave/binary

on the first line.
I think you can run any shell command from the gnuplot prompt by  
escaping the command name with a "!" sign.
c.

_______________________________________________
Help-octave mailing list
Help-octave@...
https://www-old.cae.wisc.edu/mailman/listinfo/help-octave