Step 1 write a c program using any editor. (here file name is plot.c)
/*
* file: plot.c
*
* Copyright 2010 Rooparam Choudhary <rooparam@rishi.serc.iisc.ernet.in>
*
* Date : 07.09.2010
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
#include <stdio.h>
double func ( double x ) {
if ( x < 0 )
return 0 ;
if ( x < 2 )
return x * x ;
return 16.0 / ( x * x ) ;
}
/*
* output will be in 2 coloumns
* 1 column contains x-axis values
* 2 column contains y-axis {or f(x) } values corresponding to x-value
*/
int main ( ) {
double x = 0.0; // initial value
double x_max = 5.0;
double step = 0.01;
printf ( "# x \t f(x) \n" );
while ( x <= x_max ) {
printf ( "%.3f \t %.3f \n", x, func(x) );
x += step;
}
return 0;
}
* file: plot.c
*
* Copyright 2010 Rooparam Choudhary <rooparam@rishi.serc.iisc.ernet.in>
*
* Date : 07.09.2010
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
#include <stdio.h>
double func ( double x ) {
if ( x < 0 )
return 0 ;
if ( x < 2 )
return x * x ;
return 16.0 / ( x * x ) ;
}
/*
* output will be in 2 coloumns
* 1 column contains x-axis values
* 2 column contains y-axis {or f(x) } values corresponding to x-value
*/
int main ( ) {
double x = 0.0; // initial value
double x_max = 5.0;
double step = 0.01;
printf ( "# x \t f(x) \n" );
while ( x <= x_max ) {
printf ( "%.3f \t %.3f \n", x, func(x) );
x += step;
}
return 0;
}
Step 2 compile it and run it and grab the output in plot.txt file
$ cc plot.c
$ ./a.out >plot.txt
Step 3 Draw using gnuplot
$ gnuplot
gnuplot> plot "./plot.txt" with lines
OK folks.
Tutorial is over.
This was just an introduction for plotting basic graphs. For more plotting tutorials and advanced plotting, just visit gnuplot .
See ya later.