Examples - pi.science.math.PINumericalIntegrationSimpson
1. How to compute numerical integration for functions with using Simpson`s rule ?
PIDebug.TitleBig( "Numeric integration - Simpson`s method" );
PIDebug.TitleBig( "Sin(x)^2 from 0..PI" );
PINumericalIntegrationSimpson integration = new PINumericalIntegrationSimpson( 0, Math.PI );
integration.Expression = (x) => { return Math.Pow( Math.Sin( x ), 2 ); };
integration.SetStepsCount( 100 );
double value = integration.Calc();
Console.WriteLine( value );
/* ----------------------- */
PIDebug.TitleBig( "Sqrt(x^2 + 1 ) from 0..1", true );
integration.Expression = (x) => { return Math.Sqrt( x*x + 1 ); };
integration.A = 0;
integration.B = 1;
integration.Step = 0.125;
value = integration.Calc();
Console.WriteLine( value );
PIDebug.Blank();
Console.WriteLine( "X starting interval positions: " + integration.VarX.AsString( 3 ) );
Console.WriteLine( "Y values: " + integration.VarY.AsString( 3 ) );
Console.WriteLine( "Y values cumulative: " + integration.VarYCumulative.AsString( 3 ) );
Output:
NUMERIC INTEGRATION - SIMPSON`S METHOD
--------------------------------------
SIN(X)^2 FROM 0..PI
-------------------
1.57078599349424
SQRT(X^2 + 1 ) FROM 0..1
------------------------
1.14779352976621
X starting interval positions: 0.000;0.125;0.250;0.375;0.500;0.625;0.750;0.875
Y values: 0.125;0.127;0.131;0.137;0.143;0.152;0.161;0.171
Y values cumulative: 0.125;0.253;0.384;0.520;0.664;0.815;0.976;1.148