static void Main(string[] args) { var lb = 0.01f; var ub = 0.0001f; Console.WriteLine("Bisection methods:"); Console.WriteLine("#5"); Console.WriteLine(BisectionMethod.Evaluate(Function_5, 2, 3, lb)); Console.WriteLine(BisectionMethod.Evaluate(Function_5, 2, 3, ub)); Console.WriteLine("#9"); Console.WriteLine(BisectionMethod.Evaluate(Function_9, 0, (float)Math.PI * (3f / 2f), lb)); Console.WriteLine(BisectionMethod.Evaluate(Function_9, 0, (float)Math.PI * (3f / 2f), ub)); Console.WriteLine("#11"); Console.WriteLine(BisectionMethod.Evaluate(Function_11, 0.000000000001f, 2, lb)); Console.WriteLine(BisectionMethod.Evaluate(Function_11, 0.000000000001f, 2, ub)); Console.WriteLine("#15"); Console.WriteLine(BisectionMethod.Evaluate(Function_15, 0.2f, 2f, lb)); Console.WriteLine(BisectionMethod.Evaluate(Function_15, 0.2f, 2f, ub)); Console.WriteLine("Newton method"); Console.WriteLine("#5"); Console.WriteLine(NewtonMethod.Evaluate(Function_5, 2, 3, lb)); Console.WriteLine(NewtonMethod.Evaluate(Function_5, 2, 3, ub)); Console.WriteLine("#9"); Console.WriteLine(NewtonMethod.Evaluate(Function_9, 0, (float)Math.PI * (3f / 2f), lb)); Console.WriteLine(NewtonMethod.Evaluate(Function_9, 0, (float)Math.PI * (3f / 2f), ub)); Console.WriteLine("#11"); Console.WriteLine(NewtonMethod.Evaluate(Function_11, 0.000000000001f, 2, lb)); Console.WriteLine(NewtonMethod.Evaluate(Function_11, 0.000000000001f, 2, ub)); Console.WriteLine("#15"); Console.WriteLine(NewtonMethod.Evaluate(Function_15, 0.2f, 2f, lb)); Console.WriteLine(NewtonMethod.Evaluate(Function_15, 0.2f, 2f, ub)); }
static void Main(string[] args) { int caseSwitch = 0; Console.WriteLine("Please select a number:"); Console.WriteLine("1.Implement bisection algorithm"); Console.WriteLine("2.Guess my number, human"); Console.WriteLine("3.Guess my number, computer"); caseSwitch = Convert.ToInt32(Console.ReadLine()); switch (caseSwitch) { case 1: Console.Clear(); BisectionMethod bisectionMethod = new BisectionMethod(); bisectionMethod.UserInput(); break; case 2: Console.Clear(); HumanPlays humanPlays = new HumanPlays(); humanPlays.UserPlays(); break; case 3: Console.Clear(); ComputerPlays computerPlays = new ComputerPlays(); computerPlays.computerPlays(); break; } }
public static void Main() { BisectionMethod bm = new BisectionMethod(); // 解を計算(初期値, 収束条件) double s = bm.calc(0.0, 4.0, 0.0001); // 結果表示 System.Console.WriteLine(s); // 解:1.16595458984375 }
public void Test_Function() { var a = 1.6f; var b = 6f; var result1 = BisectionMethod.Evaluate(TestFunction, a, b, accuracyLowerBound); var result2 = BisectionMethod.Evaluate(TestFunction, a, b, accuracyUpperBound); Assert.LessOrEqual(result2.Value, result1.Value); Assert.GreaterOrEqual(result2.Iterations, result1.Iterations); Console.WriteLine(result1); Console.WriteLine(result2); }
static void Main(string[] args) { double f(double x) => 3 * Math.Pow(x, 3) - 2 * Math.Pow(x, 2) + 5 * x - 2 * Math.Exp(x) + 1; // We hard code the derivative of the function for now double df(double x) => 9 * Math.Pow(x, 2) - 4 * x + 5 - 2 * Math.Exp(x); IterationCounter = 0; var bisectionPoint = BisectionMethod(f, (-1, 1), tolerance: 0.0001); Console.WriteLine(bisectionPoint); var newtonsPoint = NewtonsMethod(f, df, 1, epsilon: 0.0001, maxIterations: 50); Console.WriteLine(newtonsPoint); var secantPoint = SecantMethod(f, 1, 0.99, epsilon: 0.0001, maxIterations: 50); Console.WriteLine(secantPoint); double g(double x) => Math.Pow(x, 3) - 17 * Math.Pow(x, 2) + 6 * x + 5.5; bisectionPoint = BisectionMethod(g, (0, 16), tolerance: Math.Pow(10, -25)); Console.WriteLine(bisectionPoint); }
public void Function_15_Test() { var a = 0.2f; var b = 2f; var expectedResult = 2.0f; var result1 = BisectionMethod.Evaluate(Function_15, a, b, accuracyLowerBound); var result2 = BisectionMethod.Evaluate(Function_15, a, b, accuracyUpperBound); Assert.LessOrEqual(result1.Value, expectedResult); Assert.LessOrEqual(result2.Value, expectedResult); Console.WriteLine(result1); Console.WriteLine(result2); }
public void Function_9_Test() { var a = 0f; float b = (float)Math.PI * (3f / 2f); var result1 = BisectionMethod.Evaluate(Function_9, a, b, accuracyLowerBound); var result2 = BisectionMethod.Evaluate(Function_9, a, b, accuracyUpperBound); Assert.LessOrEqual(result2.Value, result1.Value); Assert.GreaterOrEqual(result2.Iterations, result1.Iterations); Console.WriteLine(result1); Console.WriteLine(result2); }
/// <summary> /// Executes the canal simulation /// </summary> /// <returns></returns> public CanalSimulationResult ExecuteCanalSimulation() { var result = new CanalSimulationResult(); RungeKutta solver = new RungeKutta(); double flow = CanalStretches.Where(cs => cs.Flow > 0).Select(cs => cs.Flow).FirstOrDefault(); if (!(flow > 0)) { flow = CanelEdges.OfType <SluiceCanalEdge>().Select(sce => sce.GetFreeFlow).FirstOrDefault(); } CanalStretches.ForEach(cs => cs.Flow = flow); foreach (ICanalStretchModel canalStretch in CanalStretches) { CanalStretchResult canalStretchResult = result.GetCanalStretchResult(canalStretch.Id); canalStretchResult.CriticalSlope = canalStretch.CanalSection.GetCriticalSlope(canalStretch.Flow); canalStretchResult.CriticalWaterLevel = canalStretch.CanalSection.GetCriticalWaterLevel(canalStretch.Flow); canalStretchResult.NormalWaterLevel = canalStretch.CanalSection.GetNormalWaterLevel(canalStretch.Flow); ICanalStretchModel preCanalStretch = CanalStretches.FirstOrDefault(cs => cs.ToNode.Id == canalStretch.FromNode.Id); ICanalStretchModel postCanalStretch = CanalStretches.FirstOrDefault(cs => cs.FromNode.Id == canalStretch.ToNode.Id); bool postCriticalSection = false; if (preCanalStretch != null) { } if (postCanalStretch != null) { double normalWaterLevel = postCanalStretch.CanalSection.GetNormalWaterLevel(postCanalStretch.Flow); postCriticalSection = normalWaterLevel <= canalStretchResult.CriticalWaterLevel; } AnalysisOptions options = new AnalysisOptions(); options.AnalysisSteps = (int)(canalStretch.Length > 10000 ? 10000 : canalStretch.Length); // M flow if (canalStretch.CanalSection.Slope > 0 && canalStretch.CanalSection.Slope < canalStretchResult.CriticalSlope) { // M1 // Regimen lento se impone aguas abajo if (canalStretch.ToNode.WaterLevel.HasValue && canalStretch.ToNode.WaterLevel.Value > canalStretchResult.CriticalWaterLevel) { options.InitialX = GetAbsoluteInitialLength(CanalStretches, canalStretch) + canalStretch.Length; options.FinalWaterLevel = canalStretch.ToNode.WaterLevel.Value; options.BackwardsAnalysis = true; options.ExecuteAnalysis = true; } // M2 Flow // Regimen lento se impone aguas abajo else if (postCriticalSection) { canalStretch.ToNode.WaterLevel = canalStretchResult.CriticalWaterLevel; options.InitialX = GetAbsoluteInitialLength(CanalStretches, canalStretch) + canalStretch.Length; options.FinalWaterLevel = canalStretchResult.CriticalWaterLevel + Sensibility /* Salvando numéricamente por la izquierda el problema */; options.BackwardsAnalysis = true; options.ExecuteAnalysis = true; } // M3 Flow // Regimen rapido se impone aguas arriba if (canalStretch.FromNode.WaterLevel.HasValue && canalStretch.FromNode.WaterLevel.Value < canalStretchResult.CriticalWaterLevel) { // Hydraulic jump will occur as we have in the same stretch both conditions for both flows options.HydraulicJumpOccurs = options.ExecuteAnalysis; options.InitialX = GetAbsoluteInitialLength(CanalStretches, canalStretch) + 0.0; options.InitialWaterLevel = canalStretch.FromNode.WaterLevel.Value; options.BackwardsAnalysis = false; options.ExecuteAnalysis = true; } } // S flow else if (canalStretch.CanalSection.Slope > canalStretchResult.CriticalSlope) { // S1 Flow // Regimen lento se impone aguas abajo if (canalStretch.ToNode.WaterLevel.HasValue && canalStretch.ToNode.WaterLevel.Value > canalStretchResult.CriticalWaterLevel) { throw new NotImplementedException(); } // S2 flow // Regimen rapido se impone aguas arriba // TODO el simbolo <= debe ser mejorado else if (canalStretch.FromNode.WaterLevel.HasValue && canalStretch.FromNode.WaterLevel.Value <= canalStretchResult.CriticalWaterLevel && canalStretch.FromNode.WaterLevel.Value > canalStretchResult.NormalWaterLevel) { options.InitialX = GetAbsoluteInitialLength(CanalStretches, canalStretch) + 0.0; options.InitialWaterLevel = canalStretch.FromNode.WaterLevel.Value - Sensibility /* Salvando numéricamente por la derecha el problema */; options.BackwardsAnalysis = false; options.ExecuteAnalysis = true; } } // H flow else { if (canalStretch.FromNode.WaterLevel.HasValue) { options.InitialX = GetAbsoluteInitialLength(CanalStretches, canalStretch) + 0.0; options.InitialWaterLevel = canalStretch.FromNode.WaterLevel.Value; options.BackwardsAnalysis = false; options.ExecuteAnalysis = true; } } if (options.ExecuteAnalysis) { double x = options.InitialX; double waterLevel = options.BackwardsAnalysis ? options.FinalWaterLevel : options.InitialWaterLevel; int steps = options.AnalysisSteps; solver.Interval = canalStretch.Length / steps; solver.Equation = canalStretch.FlowEquation(); if (options.HydraulicJumpOccurs && canalStretch.FromNode.WaterLevel.HasValue && canalStretch.ToNode.WaterLevel.HasValue) { List <CanalPointResult> backwardsAnalysisResult = new List <CanalPointResult>(); List <CanalPointResult> conjugateWaterLevelResult = new List <CanalPointResult>(); List <CanalPointResult> frontAnalysisResult = new List <CanalPointResult>(); double x2 = options.InitialX + canalStretch.Length; waterLevel = options.FinalWaterLevel; for (int i = 1; i <= steps; i++) { waterLevel = solver.SolveBackwards(x2, waterLevel); x2 = x2 - solver.Interval; backwardsAnalysisResult.Add(new CanalPointResult(x2, waterLevel)); } waterLevel = options.InitialWaterLevel; while (waterLevel < canalStretchResult.CriticalWaterLevel - Sensibility) { waterLevel = solver.Solve(x, waterLevel); x = x + solver.Interval; conjugateWaterLevelResult.Add(new CanalPointResult(x, canalStretch.GetHydraulicJumpDownstreamDepth(waterLevel))); frontAnalysisResult.Add(new CanalPointResult(x, waterLevel)); } Func <double, double> conjugatedEquation = GetHydraulicJumpEquation(backwardsAnalysisResult, conjugateWaterLevelResult); BisectionMethod findHydraulicJump = new BisectionMethod(conjugatedEquation, options.InitialX + Sensibility, conjugateWaterLevelResult.OrderByDescending(wl => wl.X).First().X); double hydraulicJumpX = findHydraulicJump.Solve(0.01); // Found result, otherwise it could be "desagüe anegado" if (hydraulicJumpX < double.MaxValue) { result.AddCanalPointResult(canalStretch.Id, options.InitialX, options.InitialWaterLevel); result.AddRangeCanalPointResult(canalStretch.Id, frontAnalysisResult.Where(ar => ar.X < hydraulicJumpX).ToList()); result.AddRangeCanalPointResult(canalStretch.Id, backwardsAnalysisResult.Where(ar => ar.X >= hydraulicJumpX).ToList()); } } else if (options.BackwardsAnalysis) { result.AddCanalPointResult(canalStretch.Id, x, waterLevel); for (int i = 1; i <= steps; i++) { waterLevel = solver.SolveBackwards(x, waterLevel); x = x - solver.Interval; result.AddCanalPointResult(canalStretch.Id, x, waterLevel); } canalStretch.FromNode.WaterLevel = waterLevel; } else { result.AddCanalPointResult(canalStretch.Id, x, waterLevel); // Regimen lento se impone aguas abajo // Regimen rapido se impone aguas arriba for (int i = 1; i <= steps; i++) { waterLevel = solver.Solve(x, waterLevel); x = x + solver.Interval; result.AddCanalPointResult(canalStretch.Id, x, waterLevel); } canalStretch.ToNode.WaterLevel = waterLevel; } } } return(result); }