Octave Example for Evaluating an Integral


MathLab> octave
Octave, version 1.1.1.
Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton.
This is free software with ABSOLUTELY NO WARRANTY.
For details, type `warranty'.

Integral of exp(-x^2) on Both (0,1) and (0,Infinity) Intervals:

Define the Integrand:

octave:1> # 
octave:1> # Comment:  Define Integrand:
octave:1>  function y = f (x)
>  y=exp(-x*x);
> endfunction

Integral on (0,1) Using QUAD:

octave:2> # 
octave:2> # Integral for x on (0,1):
octave:2> [area,ierror,nfneval]=quad("f",0,1)

area = 0.74682

ierror = 0

nfneval = 21

octave:3> # Comment:  Here "area" is the answer, "ierror" is the error code,
octave:3> # and "nfneval" is the number of function evaluations.

Integral on (0,Inf):

octave:3> # Comment: Integral for x on (0,Inf):
octave:3> [area,ierror,nfneval]=quad("f",0,Inf)

area = 0.88623

ierror = 0

nfneval = 135

Recovering more Digits Using PRINTF:

octave:4> # 
octave:4> # Comment: Refined Answer Recovering "area" to 10 Digits using
"prinf":
octave:4> printf("Refined answer to 10 digits = %12.10f\n",area);

Refined answer to 10 digits = 0.8862269255

octave:5> # Comment: Here the format "%w.pf" means "p" "f"loating digits in 
a field of 
octave:5> # width "w" using the C language "printf" function borrowed by 
Octave.

Independent Error Check:

octave:5> # 
octave:5> # Comment: Check Integral on (0,Inf) Against Exact Answer:
octave:5> Error=(area-sqrt(pi)/2)*2/sqrt(pi) 

Error = 0

octave:6> # Comment: Error to 40 digits:
octave:6> printf("Refined Error to 40 digits = %42.40f\n",Error);
Refined Error to 40 digits = 0.0000000000000000000000000000000000000000

Quit:

octave:22> quit

MathLab>



Web Source: http://www.math.uic.edu/~hanson/OctaveIntegralEG.html

Email Comments or Questions to hanson@math.uic.edu