Gnuplot-how to find the area under a curve / integrate?

In summary, using Gnuplot.py to integrate a curve is possible, but it's more complicated than just running Gnuplot.
  • #1
Dunhausen
30
0
Gnuplot--how to find the area under a curve / integrate?

I would assume this is a quite popular undertaking, so how is it normally done?

I just want to know the approximate area under the curve if all the points were joined by straight lines, no need (or desire) for curve fitting.

If I have to use another graphing program to do this then gnuplot becomes pretty pointless. :p
 
Physics news on Phys.org
  • #2


Hi, As far as i know integration is not possible in gnuplot (i agree gnuplot is an excellent simple plotting and fitting package, especially for fitting it is wonderful with high precession).
But in xmgrace you can integrate a full curve..
The best advice i can give is to write a simple program for integration..so that you can even set the ranges of X-axis values for integration.
good luck
 
  • #3


I wound up using Gnuplot.py, which let's you run Gnuplot using python. In hindsight this is easier, anyway.

There are several functions in the scipy package which can perform this type of numeric integration. (I used "trapz")

Then Gnuplot can be called like this:
(posted because so far Gnuplot.py is not very well documented)

Code:
    import Gnuplot
    gp2 = Gnuplot.Gnuplot(persist=1)  #persist=1 keeps the window open after the program runs
    gp2('set data style lines')
    gp2('set termoption enhanced')  #so that the fancy symbols below work in the GUI display
    gp2('set title "Graph of {/Symbol Y}_{II}"')
    gp2('set xlabel "x (meters)"')
    gp2('set ylabel "{/Symbol Y}"')


    gp3 = Gnuplot.Gnuplot(persist=1)  #create a different object for a different plot
    gp3('set data style lines')
    gp3('set termoption enhanced')
    gp3('set title "Graph of {/Symbol Y}_{III}"')
    gp3('set xlabel "x (meters)"')
    gp3('set ylabel "{/Symbol Y}"')    
    
    #Generate wave function data for the specified energy levels
    
    num_divisions=1000
    spacing=L/num_divisions
    x2=arange(0,L, spacing)
    x3=arange(L,2*L, spacing)

    Psi2_0=A[0]*Psi2(x2,L_2[0])
    Psi2_1=A[1]*Psi2(x2,L_2[1])
    Psi2_2=A[2]*Psi2(x2,L_2[2])

    Psi3_0=Psi3(x3,L_3[0])
    Psi3_1=Psi3(x3,L_3[1])
    Psi3_2=Psi3(x3,L_3[2])


    plot2_0=Gnuplot.PlotItems.Data(x2,Psi2_0,title='{/Symbol Y}_{II_0}')
    plot2_1=Gnuplot.PlotItems.Data(x2,Psi2_1,title='{/Symbol Y}_{II_1}')
    plot2_2=Gnuplot.PlotItems.Data(x2,Psi2_2,title='{/Symbol Y}_{II_2}')

    plot3_0=Gnuplot.PlotItems.Data(x3,Psi3_0,title='{/Symbol Y}_{III_0}')
    plot3_1=Gnuplot.PlotItems.Data(x3,Psi3_1,title='{/Symbol Y}_{III_1}')
    plot3_2=Gnuplot.PlotItems.Data(x3,Psi3_2,title='{/Symbol Y}_{III_2}')
    
    gp2.plot(plot2_0,plot2_1,plot2_2)
    gp3.plot(plot3_0,plot3_1,plot3_2)
    
#note that terminal="postscript enhanced" returns an error, you have set "enhanced" as so
    gp2.hardcopy(filename="psi2.ps",terminal="postscript",enhanced=1) 
    gp3.hardcopy(filename="psi3.ps",terminal="postscript",enhanced=1)
 
  • #4


Hi that is wonderful,
but i don't understand..
Normally in linux i type gnuplot in command..then i work with gnuplot ..[i use only for fitting]..
I curious to know about integration in gnuplot..If you have some time please help..For e.g., I have a lorentzian and want to find the area..
my function:
[tex]f(x)=\frac{h\Gamma^2}{\Gamma^2+4(x-x_0)^2}[/tex],
where [tex]h[/tex] is height and [tex]\Gamma[/tex] is full width at half maximum and [tex]x_0[/tex] is peak position. It would be nice if you provide the code with 5 input parameters (peak position, full width, height, left and right limit of peak (left=right), number of data points). It is often time consuming for me to write C codes and finding bugs..
thank you very much
 
  • #5


Although gnuplot is not really very well suited for operations like integration etc., a workaround is possible - within gnuplot itself! Check out the example file bivariat.dem that comes with the gnuplot documentation - it provides a way to use Simpson's rule for numerical integration that you can easily adapt for your use.
 

1. How do I plot a graph in Gnuplot?

To plot a graph in Gnuplot, you need to first create a data file with the x and y values of the points you want to plot. Then, open Gnuplot and use the "plot" command to plot the data. You can also customize the appearance of the graph using various options.

2. How do I find the area under a curve in Gnuplot?

To find the area under a curve in Gnuplot, you can use the "integrate" command. This command takes the function or data points as input and calculates the area under the curve using numerical integration techniques. You can also specify the limits of integration and the number of intervals for a more accurate result.

3. Can I integrate a non-continuous function in Gnuplot?

Yes, Gnuplot has the ability to integrate non-continuous functions using the "integrate" command. However, the result may not be accurate if the function has discontinuities or sharp changes in the curve.

4. How do I plot the integrated curve in Gnuplot?

After using the "integrate" command to find the area under the curve, you can plot the integrated curve using the "plot" command. Simply input the integrated function as the argument for the "plot" command and specify the limits of integration. This will plot the integrated curve on the same graph as the original function.

5. Can I save the integrated curve as a data file in Gnuplot?

Yes, you can save the integrated curve as a data file in Gnuplot by using the "set table" command. This command will create a new data file and save the integrated curve as x and y values in columns. You can then use this data file for further analysis or plotting.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
1K
Replies
10
Views
822
Replies
2
Views
262
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
Replies
11
Views
980
Replies
2
Views
2K
  • Introductory Physics Homework Help
Replies
8
Views
1K
  • Calculus
Replies
5
Views
2K
Replies
3
Views
325
Replies
4
Views
1K
Back
Top