This example uses the trapezoidal rule to approximate an integral.
The input file (test5.xml)
<?xml version="1.0"?> <program> <comment> <center> <h1>Simple Integration Example</h1> </center> </comment> <math> <comment> <p>Lower limit</p> </comment> <declare type="fn" constant="true"> <ci>A</ci> <lambda> <cn>0</cn> </lambda> </declare> <comment> <p>Upper limit</p> </comment> <declare type="fn" constant="true"> <ci>B</ci> <lambda> <cn>1</cn> </lambda> </declare> <comment> <p>Number of subintervals</p> </comment> <declare type="fn" constant="true" output="int"> <ci>N</ci> <lambda> <cn>100</cn> </lambda> </declare> <comment> <p>Function to integrate</p> </comment> <declare type="fn"> <ci>f</ci> <lambda> <bvar><ci>x</ci></bvar> <apply><power/> <ci>x</ci> <cn>3</cn> </apply> </lambda> </declare> <comment> <p>Size of each subinterval</p> </comment> <declare type="fn" constant="true"> <ci>h</ci> <lambda> <bvar><ci>A</ci></bvar> <bvar><ci>B</ci></bvar> <bvar><ci>N</ci></bvar> <apply><divide/> <apply><minus/> <ci>B</ci> <ci>A</ci> </apply> <ci>N</ci> </apply> </lambda> </declare> <comment> <p>Starting location of each subinterval</p> </comment> <declare type="fn" input="int" output="double" index="true"> <ci>a</ci> <lambda> <bvar><ci>i</ci></bvar> <apply><plus/> <ci>A</ci> <apply><times/> <ci>i</ci> <ci>h</ci> </apply> </apply> </lambda> </declare> <comment> <p>Approximation to integral</p> </comment> <declare type="fn" input="int"> <ci>Integral</ci> <lambda> <apply><plus/> <apply><times/> <apply><divide/> <ci>h</ci> <cn>2</cn> </apply> <apply><plus/> <apply> <ci>f</ci> <apply> <ci>a</ci> <cn>0</cn> </apply> </apply> <apply> <ci>f</ci> <apply> <ci>a</ci> <ci>N</ci> </apply> </apply> </apply> </apply> <apply><times/> <ci>h</ci> <apply><sum/> <bvar><ci>i</ci></bvar> <lowlimit><cn>1</cn></lowlimit> <uplimit> <apply><minus/> <ci>N</ci> <cn>1</cn> </apply> </uplimit> <apply> <ci>f</ci> <apply> <ci>a</ci> <ci>i</ci> </apply> </apply> </apply> </apply> </apply> </lambda> </declare> </math> </program>
A few attributes have been added to the "fn" tag. The "input" and "output" attributes give the C types of the input and output of the function (eventually, the input type attribute should be attached to the arguments, not the function).
The "index" attribute indicates arguments should be rendered with a subscript rather than parenthesis. (Once again, this attribute should be attached to the argument instead)
The "constant" attribute renders the function without any arguments. The translation to C++ is as an assignment declaration, rather than a function.
The text output is
A = 0 B = 1 N = 100 f(x) = x^3 h(A,B,N) = (B-A)/N a(i) = A+i*h Integral = (h/2)*(f(a(0))+f(a(N)))+h*sum_{i=1}^{N-1} (f(a(i)))
The output in MathML presentation form (test5.xhtml)
Lower limit
Upper limit
Number of subintervals
Function to integrate
Size of each subinterval
Starting location of each subinterval
Approximation to integral
The C++ output (test5.cpp)
double A = 0; double B = 1; int N = 100; double f(double x){ return pow(x,3); } double h = (B - A) / N; double a(int i){ return A + i * h; } double Integral(){ double sum = 0.0; int i; for (i = 1;i <= N - 1;i++) { sum += f(a(i)); } return (h / 2) * (f(a(0)) + f(a(N))) + h * sum; }
To make use of the C++ output, use the following code (main.cpp)
#include <stdio.h> #include <math.h> #include "test5.cpp" int main() { double in = Integral(); printf("integral = %g\n",in); return 0; }
Previous page: Simple function definition example.
Next page: Integration with random sampling
Written by Mark Dewing on July 20, 2005. Last updated on September 21, 2005.