www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/02/11/19:19:53

Date: Mon, 12 Feb 1996 08:02:44 -0800 (GMT)
From: Orlando Andico <oandico AT eee DOT upd DOT edu DOT ph>
To: DJGPP Mailing List <djgpp AT delorie DOT com>
Subject: Re: Integrals/Derivatives in C
Message-ID: <Pine.SOL.3.91.960212075659.518B-100000@gollum>
MIME-Version: 1.0

> ohd AT msn DOT com (David Oh) writes:
> 
> >	I was wondering if anyone out there can help me out with a simple C 
> >code fragment to find the (numeric & symbolic) derivative or integral 
> >of a function.
> 

Just my 0.02 worth... MathCAD (even the old 1988 version) was able to do 
symbolic integration with something called MAPLE? I'm not sure if the 
source for this thing is available... but you could try looking.

If you wanna do a numerical integration, the *easiest* and *dumbest* way 
by far is to use a loop, like so:

/* your function to integrate */
double your_function (double x)
{
    /* whatever to do... */
}

double integrate (double xlo, xhi)
{
    /* xlo and xhi are the limits of integration */

    double integral = 0;
    double TOL = 1e-10;
    double X;

    for (X = xlo; X < xhi; X += TOL)
        integral += (your_function (X) * TOL); 

    return (integral);
}


You can make TOL as small as you wish. Essentially, this is just 
performing the integration as it's defined, and as Archimedes did it. You 
can get more accuracy by making the integral "slices" trapezoids, instead 
of rectangles (this latter is called Simpson's rule).

Derivatives can be found iteratively (forgot the technique).

/----------------------------------------------------------------------------\
|  Orlando A. Andico                 "I have no concept of time, other than  |
|  oandico AT eee DOT upd DOT edu DOT ph               it is flying." -- Alanis Morissette  |
\----------------------------------------------------------------------------/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019