Tuesday, September 7, 2010

Plotting a basic 2D graph using C + gnuplot (Tutorial)

In this tutorial we will learn how to draw a graph using ANSI C language with the help of gnuplot utility.

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;
}


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.

Friday, August 27, 2010

Facts of Lagrange Interpolation

"Lagrangian interpolation is praised for analytic utility and beauty but deplored for numerical practice." This heading, from the extended table of contents of one of the most enjoyable textbooks of numerical analysis [1],expresses a widespread view. 

[...] Given (x0, f0), (x1, f1), . . ., (xn, fn) with arbitrary spaced xj, Lagrange had the idea of multiplying each fj by a polynomial that is 1 at xj and 0 at the other n nodes and then taking the sum of these n + 1 polynomials. Clearly, this gives the unique interpolation polynomial of degree n or less. [...]
                  ( Erwin Kreyszig, Advanced Engineering Mathematics )

Figure : Lagrange Interpolation of function 1/(1+x*x)


[...] Lagrange and other interpolation at equally spaced points, as in the example above, yield a polynomial oscillating above and below the true function. This behaviour tends to grow with the number of points, leading to a divergence known as Runge's phenomenon; the problem may be eliminated by choosing interpolation points at Chebyshev nodes. [...]
                  ( Wikipedia )

Although their are superior interpolation methods than lagrange interpolation method. But it is quite easy to understand and it is superior than Taylor Series Approximation of a function. It can be seen easily by following diagram.
Here I choose f(x) = e^x in the interval [0, 2]

[1]  F. S. Acton, Numerical Methods That [Usually] Work, AMS, Providence, RI, 1990.

Tuesday, March 30, 2010

twitter account

just removed 2 redundant accounts from twitter.com and now i have only 1 twitter acct and its http://twitter.com/rrc_iisc