Skip to main content
Engineering LibreTexts

25.4: Example

  • Page ID
    54409
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    This section provides an example program that plots a simple function.

    \[ y = \sin(x) \cdot \left( \frac{1 - \cos(x)}{3.0} \right) \nonumber \]

    The program, data file, and final output are presented for reference.

    Plot Program

    The following is an example program that generates points, opens a plot file, write the header, data points, and footer.

    program plotExample
    
    implicit none
    real, dimension(200) :: x, y
    integer :: i, opnstat
    
    ! Generate x and y points
        do i = 1, 200
            x(i) = i * 0.05
            y(i) = sin(x(i)) * (1-cos(x(i)/3.0))
        end do
    
    ! Output data to a file
        open (12, file="data.plt", status="replace",        &
                    action="write", position="rewind",      &
                    iostat=opnstat)
        if (opnstat > 0) stop "error opening plot file."
    
    ! Write header
        write (12, '(a)') "# Example Plot File"
        write (12, '(a)') "set title ""Example Plot Function"" "
        write (12,'(a,a)') "plot ""-"" notitle with dots ",
                            "linewidth 2 linecolor 2"
    
    ! Write points to file.
        do i=1,100
            write (12,*) x(i), y(i)
        end do
    
    ! Write footer and close file
        write (12, '(a)') "end"
        write (12, '(a)') "pause -1"
        
        close(12)
    
    end program plotExample
    

    The data file name can be changed or read from the user.

    Plot File

    The output file from this program appears as follows:

    # Example Plot File
    set title "Example Plot Function"
    plot "-" notitle with dots linewidth 2 linecolor 2
      5.0000001E-02  6.9413913E-06
      0.1000000      5.5457884E-05
      0.1500000      1.8675877E-04
      0.2000000      4.4132397E-04
      0.2500000      8.5854460E-04
      
        [many points not displayed due to space considerations]
      
      4.900000       -1.043852
      4.950000       -1.048801
      5.000000       -1.050716
    end
    pause -1
    

    The output file can be edited with a standard text editor. This will allow checking the data file for possible errors if it does not display correctly.

    Plot Output

    The plot output is as follows:

    A gnuplot graph.

    On Windows machines, the plot can be printed by right clicking on the blue bar (on top). This will display a menu, with “Print” as one of the options.


    This page titled 25.4: Example is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?