Examples - pi.science.math.PINumericalIntegrationTrapezoidal
1. How to compute numerical integration for functions with using Trapezoidal rule ?
PIDebug.TitleBig( "Numeric integration - Trapezoidal method" ); PIDebug.TitleBig( "Sin(x)^2 from 0..PI" ); PINumericalIntegrationTrapezoidal integration = new PINumericalIntegrationTrapezoidal( 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 - TRAPEZOIDAL METHOD ---------------------------------------- SIN(X)^2 FROM 0..PI ------------------- 1.57078082875621 SQRT(X^2 + 1 ) FROM 0..1 ------------------------ 1.14871446639272 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.144;0.152;0.161;0.171 Y values cumulative: 0.125;0.253;0.384;0.521;0.664;0.816;0.977;1.149