public void TestSimpsonRule() { SimpsonRule algorithm = new SimpsonRule(); Assert.That( algorithm.IntegrateThreePoint(TargetFunctionA, StartA, StopA), NumericIs.AlmostEqualTo(TargetAreaA, 1.7e-1), "Direct (2 Partitions)"); Assert.That( algorithm.IntegrateComposite(TargetFunctionA, StartA, StopA, 2), NumericIs.AlmostEqualTo(TargetAreaA, 1.7e-1), "Composite 2 Partitions"); Assert.That( algorithm.IntegrateComposite(TargetFunctionA, StartA, StopA, 6), NumericIs.AlmostEqualTo(TargetAreaA, 1.2e-1), "Composite 6 Partitions"); Assert.That( algorithm.IntegrateComposite(TargetFunctionA, StartA, StopA, 10), NumericIs.AlmostEqualTo(TargetAreaA, 8e-3), "Composite 10 Partitions"); Assert.That( algorithm.IntegrateComposite(TargetFunctionA, StartA, StopA, 50), NumericIs.AlmostEqualTo(TargetAreaA, 8e-6), "Composite 50 Partitions"); Assert.That( algorithm.IntegrateComposite(TargetFunctionA, StartA, StopA, 1000), NumericIs.AlmostEqualTo(TargetAreaA, 5e-11), "Composite 1000 Partitions"); }
//Calcula um valor especifico de distribuição normal multiplicando a constante pela integral da série private double CalculateNormalDistribuition(double z) { var firstPart = (1 / Math.Sqrt(2 * Math.PI)); var integrate = SimpsonRule.IntegrateComposite(x => CalcSomatorio(x, 27), 0, z, 10000); var result = firstPart * integrate; return(result); }
public void SimpsonRuleSupportsCompositeIntegration(int partitions, double maxRelativeError) { Assert.AreEqual( TargetAreaA, SimpsonRule.IntegrateComposite(TargetFunctionA, StartA, StopA, partitions), maxRelativeError * TargetAreaA, "Composite {0} Partitions", partitions); }
public void SimpsonRuleSupportsCompositeIntegration([Values(2, 6, 10, 50, 1000)] int partitions, [Values(1.7e-1, 1.2e-1, 8e-3, 8e-6, 5e-11)] double maxRelativeError) { Assert.AreEqual( TargetAreaA, SimpsonRule.IntegrateComposite(TargetFunctionA, StartA, StopA, partitions), maxRelativeError * TargetAreaA, "Composite {0} Partitions", partitions); }
public static double simpsonRuleEs(Expr expr, string varName, double a, double b, int n) { var f = expr.Compile(varName); double srValue = SimpsonRule.IntegrateComposite(f, a, b, n); double trueValue = Integrate.OnClosedInterval(f, a, b); return(abs(trueValue - srValue)); }
public double getObjectiveValue(double Qflow) { double f = 0.0; double c1 = 0, c0 = 0, c2 = 0, c5 = 0, c6 = 0, c3 = 0; int c4 = 0; double Qadd = 0; //oklart vad dena tillför Convert.ToDouble(kf); double X = (Qflow + Qadd) / kf; //flöde per körfält switch (vdValue) { case 3: c1 = 100; c0 = 0.1; break; case 10: c1 = 13; c0 = 10; break; case 11: c1 = 35; c0 = 2; break; case 12: c1 = 35; c0 = 4; break; case 13: c1 = 20; c0 = 6; break; case 14: c1 = 22.22; c0 = 6; break; } if (c1 == 0) { // fungerar double intervalBegin = 0; double intervalEnd = X; double partitionNumbers = 10.0; double deltaX = (intervalEnd - intervalBegin) / partitionNumbers; double SimpsonsIntegration = (deltaX / 3) * (getTime(intervalBegin, vdValue) + 4 * getTime(intervalBegin + deltaX * 1, vdValue) + 2 * getTime(intervalBegin + deltaX * 2, vdValue) + 4 * getTime(intervalBegin + deltaX * 3, vdValue) + 2 * getTime(intervalBegin + deltaX * 4, vdValue) + 2 * getTime(intervalBegin + deltaX * 5, vdValue) + 4 * getTime(intervalBegin + deltaX * 6, vdValue) + 2 * getTime(intervalBegin + deltaX * 7, vdValue) + 4 * getTime(intervalBegin + deltaX * 8, vdValue) + 2 * getTime(intervalBegin + deltaX * 9, vdValue) + getTime(intervalEnd, vdValue)); f = SimpsonsIntegration * kf; } else { f = SimpsonRule.IntegrateComposite(x => ((60 * length) / c1 + c0), 0, Qflow, 10); } return(f * (100.0 / 60.0)); }
/// <summary> /// Simpsonses the rule integral. /// </summary> /// <returns>The rule integral.</returns> /// <param name="values">Values.</param> public static List <decimal> SimpsonsRuleIntegral(List <decimal> values) { var integralVals = new List <decimal>(); if (values.Any()) { currentVals = values.Select(x => (double)x).ToList(); for (int i = 1; i < currentVals.Count(); i++) { var simpsonIntegral = SimpsonRule.IntegrateComposite(GetValue, 0, i, (i * 2)); integralVals.Add((decimal)simpsonIntegral); } } return(integralVals); }
private void btn_Integrate_Click(object sender, EventArgs e) { double a = Convert.ToDouble(txt_A.Text), b = Convert.ToDouble(txt_B.Text); string Function = txt_Func.Text; SymbolicExpression x = Variable("x"); Func <double, double> f = Parse(Function).Compile(nameof(x)); double IntegrationResult = SimpsonRule.IntegrateComposite(f, a, b, 4); txt_Result.Text = IntegrationResult.ToString(); // Draw area under function graph var model = new PlotModel { Title = "Area of f(x)" }; var series = new AreaSeries { Title = "∫ f(x)dx" }; for (double x_val = a; x_val <= b; x_val += 0.01d) { series.Points.Add(new DataPoint(x_val, f(x_val))); series.Points2.Add(new DataPoint(x_val, 0)); } series.Color2 = OxyColors.Transparent; model.Series.Add(series); FunctionSeries func = new FunctionSeries(f, a - (1.0d / 4.0d) * Math.Abs(a), b + (1.0d / 4.0d) * b, 0.01d, "f(x)"); model.Series.Add(func); plot1.Model = model; }
/// <summary> /// Funzione che calcola l'integrale tra a e b della funzione di densità di probabilità. /// </summary> /// <param name="a">L'estremo sinistro dell'intervallo. Può anche essere -∞.</param> /// <param name="b">L'estremo destro dell'intervallo. Può anche essere +∞.</param> /// <param name="sigma">Lo scarto quadratico medio. Se non passato, di default è 1.</param> /// <param name="mu">La media. Se non passata, di default è 0.</param> /// <returns>Il valore dell'area</returns> public static double CalcolaIntegrale(double a, double b, double sigma = 1, double mu = 0) { //check condizioni di operabilità if (a > b || a.Equals(double.PositiveInfinity) || b.Equals(double.NegativeInfinity) || sigma < 0 || mu < 0) { return(double.NaN); } // - e + inf if (a == double.NegativeInfinity && b == double.PositiveInfinity) { return(1); } // -inf e valore if (a == double.NegativeInfinity) { if (b >= mu) { return(0.5 + SimpsonRule.IntegrateComposite(funzioneDensita(mu, sigma), mu, b, 100000)); } else { return(0.5 - SimpsonRule.IntegrateComposite(funzioneDensita(mu, sigma), mu, -b, 100000)); } } // valore e +inf else if (b == double.PositiveInfinity) { if (a >= mu) { return(0.5 - SimpsonRule.IntegrateComposite(funzioneDensita(mu, sigma), mu, a, 100000)); } else { return(0.5 + SimpsonRule.IntegrateComposite(funzioneDensita(mu, sigma), mu, -a, 100000)); } } return(SimpsonRule.IntegrateComposite(funzioneDensita(mu, sigma), a, b, 100000)); }
//************************************************************************** // Function: WaterUsage // // Description: Calculate the water usage of a specific pivot. Based on // the Pacific University 2021 mathematics capstone 'RainCat' // // Parameters: fieldTable - FieldTable object to calculate water usage of // // Returns: Total water that the pivot uses. //************************************************************************** public static double WaterUsage(ref FieldTable fieldTable) { double constA = SoilModel.InfiltrationConstants[fieldTable.SoilType].Item1; double constB = SoilModel.InfiltrationConstants[fieldTable.SoilType].Item2; double totalWater = 0; double t_a = 0; double da_dt_max = 0; double da_dt = 0; double di_dt = 0; double intersect1 = 0; double intersect2 = 0; bool bInt1Found = false; bool bInt2Found = false; double circumference = 0; double totalSlice = 0; double integration = 0; // For each sprinkler in the pivot for (int i = CenterPivotModel.Spacing; i < fieldTable.PivotLength; i += CenterPivotModel.Spacing) { // Get sprinkler specific data t_a = (CenterPivotModel.RevolutionTime) * (CenterPivotModel.WettedDiameter / (2 * Math.PI * i)); da_dt_max = (4 / Math.PI) * (CenterPivotModel.WettedSoilDepth / t_a); // Find first intersection by incrementing through small time increments starting at zero for (double t = .001; t < t_a && bInt1Found == false; t += .001) { da_dt = (da_dt_max) * Math.Sqrt(1 - (Math.Pow((t - (t_a / 2)), 2) / Math.Pow(t_a / 2, 2))); di_dt = 600 * constA * constB * Math.Pow(t, constB - 1); if (di_dt - da_dt < 1 && di_dt - da_dt > -1) { intersect1 = t; bInt1Found = true; } } // If first intersection is found then we need to search for the second if (true == bInt1Found) { // Find second intersection by incrementing through small time increments starting at the end of the interval for (double t = t_a; t > intersect1 + .005 && bInt2Found == false; t -= .001) { da_dt = (da_dt_max) * Math.Sqrt(1 - (Math.Pow((t - (t_a / 2)), 2) / Math.Pow(t_a / 2, 2))); di_dt = 600 * constA * constB * Math.Pow(t, constB - 1); if (di_dt - da_dt < 1 && di_dt - da_dt > -1) { intersect2 = t; bInt2Found = true; } } } // If both intersections are found then we have enough information to get the water usage stats if (true == bInt1Found && true == bInt2Found) { // Get distance traveled then integrate to get amount of water used from t_0 to t_1 circumference = 2 * Math.PI * i; totalSlice = (circumference / CenterPivotModel.RevolutionTime) * (intersect2 - intersect1); integration = SimpsonRule.IntegrateComposite(x => (da_dt_max) * Math.Sqrt(1 - (Math.Pow((x - (t_a / 2)), 2) / Math.Pow(t_a / 2, 2))), 0, intersect1, 4); } // If first intersection was not found, OR if second intersection was not found then assume pivot will run entire time else if (false == bInt1Found || false == bInt2Found) { circumference = 2 * Math.PI * i; totalSlice = (circumference / CenterPivotModel.RevolutionTime) * (t_a - 0); integration = SimpsonRule.IntegrateComposite(x => (da_dt_max) * Math.Sqrt(1 - (Math.Pow((x - (t_a / 2)), 2) / Math.Pow(t_a / 2, 2))), 0, t_a, 4); } totalWater += integration * (circumference / totalSlice); // Reset Values intersect1 = 0; intersect2 = 0; bInt1Found = false; bInt2Found = false; circumference = 0; totalSlice = 0; } return(totalWater); }
public static double simpsonRule(Expr expr, string varName, double a, double b, int n) { var f = expr.Compile(varName); return(SimpsonRule.IntegrateComposite(f, a, b, n)); }
static void Main(string[] args) { /// Git test project Console.WriteLine("ASP joined the project"); /// Simpsons Rule //--------------------------------------------- Console.WriteLine("Simpson integration"); Console.WriteLine(""); // Composite approximation with 4 partitions double composite = SimpsonRule.IntegrateComposite(x => x * x, 0.0, 10.0, 4); // Approximate value using IntegrateComposite with 4 partitions is: 333.33333333333337 Console.WriteLine("Approximate value using IntegrateComposite with 4 partitions is: " + composite); // Three point approximation double threePoint = SimpsonRule.IntegrateThreePoint(x => x * x, 0.0, 10.0); // Approximate value using IntegrateThreePoint is: 333.333333333333 Console.WriteLine("Approximate value using IntegrateThreePoint is: " + threePoint); /// Gauss-Legendre integration //--------------------------------------------- Console.WriteLine(""); Console.WriteLine("Gauss-Legendre integration"); Console.WriteLine(""); // Create a 5-point Gauss-Legendre rule over the integration interval [0, 10] GaussLegendreRule rule = new GaussLegendreRule(0.0, 10.0, 5); double sum = 0; // Will hold the approximate value of the integral for (int i = 0; i < rule.Order; i++) // rule.Order = 5 { // Access the ith abscissa and weight sum += rule.GetWeight(i) * rule.GetAbscissa(i) * rule.GetAbscissa(i); } // Approximate value is: 333.333333333333 Console.WriteLine("Approximate value is: " + sum); // The order of the rule is: 5 Console.WriteLine("The order of the rule is: " + rule.Order); // 1D integration using a 5-point Gauss-Legendre rule over the integration interval [0, 10] double integrate1D = GaussLegendreRule.Integrate(x => x * x * x, 0.0, 10.0, 5); // Approximate value of the 1D integral is: 333.333333333333 Console.WriteLine("Approximate value of the 1D integral is: " + integrate1D); // 2D integration using a 5-point Gauss-Legendre rule over the integration interval [0, 10] X [1, 2] double integrate2D = GaussLegendreRule.Integrate((x, y) => (x * x) * (y * y), 0.0, 10.0, 1.0, 2.0, 5); // Approximate value of the 2D integral is: 777.777777777778 Console.WriteLine("Approximate value of the 2D integral is: " + integrate2D); Console.ReadLine(); }