private PVTEntry GetEntryAtPressureAndProperty( double pressure, double targetPropVal, Func <PVTEntry, double> propGetter) { PVTEntry liqEntry = GetEntryAtSatPressure(pressure, SaturationRegion.Liquid); PVTEntry vapEntry = GetEntryAtSatPressure(pressure, SaturationRegion.Vapor); if (vapEntry != null && liqEntry != null && propGetter(vapEntry) >= targetPropVal && propGetter(liqEntry) <= targetPropVal) { double liqFac = (propGetter(vapEntry) - targetPropVal) / (propGetter(vapEntry) - propGetter(liqEntry)); LiquidVaporEntryFactory fac = new LiquidVaporEntryFactory(vapEntry, liqEntry, liqFac); return(fac.BuildThermoEntry()); } double fx(double x) { PVTEntry entry = GetEntryAtTemperatureAndPressure(x, pressure); return(entry != null?propGetter(entry) - targetPropVal : double.NaN); } Range tempRange = GetTemperatureRange(pressure); SolverResult temperature = NewtonsMethod.Solve(fx, tempRange); return(temperature.ReachedMaxGuesses ? null : GetEntryAtTemperatureAndPressure(temperature.Value, pressure)); }
public void CountRootTests(double number, int degree, double precision) { double expect = Math.Pow(number, (double)1 / degree); double result = NewtonsMethod.CountRoot(number, degree, precision); Assert.Less(Math.Abs(expect - result), precision); }
public void Mewtoon_Maximum_In_0(double startPoint, double eps) { const double expectedValue = 0; var newtonsMethod = new NewtonsMethod(MaximizationFunctionDerivative1, MaximizationFunctionDerivative2); var root = newtonsMethod.Newton(startPoint, eps); Assert.True(Math.Abs(expectedValue - root) < eps); }
/// <summary> /// /// </summary> /// <param name="temperature">in K</param> /// <param name="pressure">in Pa</param> /// <param name="maxTemperature">in K</param> public Region3Factory(double temperature, double pressure, Range temperatureRange) { Props = new Region3Properties() { Pressure = pressure, Temperature = temperature }; SolverResult result = NewtonsMethod.Solve((x) => DensitySolver(Props.Temperature, x), temperatureRange); if (result.ReachedMaxGuesses) { throw new ArgumentOutOfRangeException("Could find a point in region3 at the given temperature and pressure"); } Props.Density = result.Value; }
static void Main(string[] args) { const double eps = 0.001; Console.WriteLine("Установление первоначальных границ"); var boundFoundary = new UnconditionalOptimization(MaximizationFunction); boundFoundary.OnIteration += BoundFoundary_OnIteration; var(startLeftBound, startRightBound) = boundFoundary.GetBoundForMaximization(5, .5); DisplayResult((startLeftBound, startRightBound), boundFoundary.Iterations, eps); Console.WriteLine("Уточнение границ методом Золотого сечения"); var goldenSection = new GoldenSection(MaximizationFunction); goldenSection.OnIteration += BoundFoundary_OnIteration; var goldenSectionBounds = goldenSection.FindMax(startLeftBound, startRightBound, eps); DisplayResult(goldenSectionBounds, goldenSection.Iterations, eps); Console.WriteLine("Квадратичная апроксимация"); var quadraticApproximation = new QuadraticApproximation(MaximizationFunction); quadraticApproximation.OnIteration += BoundFoundary_OnIteration; var quadApproxBounds = quadraticApproximation.Calculate(startLeftBound, startRightBound, eps); DisplayResult(quadApproxBounds, quadraticApproximation.Iterations, eps); Console.WriteLine("Метод Ньютона"); var newtonsMethod = new NewtonsMethod(MaximizationFunctionDerivative1, MaximizationFunctionDerivative2); var rootNewtoon = newtonsMethod.Newton(-9, eps); Console.WriteLine($"Корень {rootNewtoon}"); Console.WriteLine(); Console.ReadLine(); }
/// <summary> /// Finds the NTH root. /// </summary> /// <param name="number">The number.</param> /// <param name="rootDegree">The root degree.</param> /// <param name="accuracy">The accurancy.</param> /// <returns>Root Nth degrees of the number.</returns> public static double FindNthRoot(double number, int rootDegree, double accuracy) { return(NewtonsMethod.NthRoot(number, rootDegree, accuracy)); }
private async void button1_Click(object sender, EventArgs e) { try { labelerr.Text = ""; _fx = comboBoxf.Text; if (v() != 0) { var model = new NewtonsModel { Func = _fx, Tol = double.Parse(tolBox.Text), IterationMax = int.Parse(k_maxBox.Text), PointX = decimal.Parse(aBox.Text), Delta = (decimal)double.Parse(DeltaBox.Text), Epsilon = (decimal)double.Parse(tolBox.Text), R = decimal.Parse(RBox.Text) }; progressBar1.Visible = true; progressBar1.Maximum = model.IterationMax + 1; progressBar1.Value = 0; var stopWatch = new Stopwatch(); stopWatch.Start(); var newtonsMethod = new NewtonsMethod(); newtonsMethod.ProgressBarIncrement += ProgressBarIncrement; var result = await Task.Run(() => newtonsMethod.Calculate(model)); stopWatch.Stop(); var ts = stopWatch.Elapsed; if (!string.IsNullOrWhiteSpace(result.Error)) { MessageBox.Show(result.Error); } else { Sec.Text = ts.TotalSeconds.ToString("0.0"); fx1outBox.Text = result.Fx.ToString(" 0e0"); x1uotFBox.Text = result.X.ToString(CultureInfo.InvariantCulture); outTolBox.Text = result.RelError.ToString("0e0"); countinerBox.Text = result.Iteration.ToString(); // fx1outBox.Text = result.Fx.ToString(CultureInfo.InvariantCulture); outdfx1.Text = result.Dfx1.ToString(CultureInfo.InvariantCulture); if (result.Iteration == model.IterationMax && result.RelError > (decimal)model.Tol) { labelerr.Text = @"Решение с заданной точностью \n за K_Max(" + model.IterationMax + @")итераций не удалось найти."; } } newtonsMethod.ProgressBarIncrement -= ProgressBarIncrement; } } catch (Exception ex) { MessageBox.Show(@"Не удалось распознать F. " + ex.Message); } }