Examples - pi.science.math.PINumericalIntegrationRectangle
1. How to compute numerical integration for functions with using Rectangle rule ?
PIDebug.TitleBig( "Numeric integration - Rectangle method" ); PIDebug.TitleBig( "Sin(x)^2 from 0..PI" ); PINumericalIntegrationRectangle integration = new PINumericalIntegrationRectangle( 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 - RECTANGLE METHOD -------------------------------------- SIN(X)^2 FROM 0..PI ------------------- 1.57078857586325 SQRT(X^2 + 1 ) FROM 0..1 ------------------------ 1.14733306145295 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.136;0.143;0.152;0.161;0.171 Y values cumulative: 0.125;0.252;0.383;0.520;0.663;0.815;0.976;1.147