Ejemplo n.º 1
0
        /// <summary>
        /// Plot solution
        /// </summary>
        /// <param name="sender">sender object</param>
        /// <param name="e">Event arguments</param>
        private void PlotButton_Click(object sender, EventArgs e)
        {
            pane.CurveList.Clear();

            // Prey population equation
            Func <double, double, double> preyPop = (x, y) =>
                                                    x * ((double)PreyBirthRate.Value - (double)PreyDeathRate.Value * y);

            // Predator population equation
            Func <double, double, double> predatorPop = (x, y) =>
                                                        y * ((double)PredatorBirthRate.Value * x - (double)PredatorDeathRate.Value);

            // Solution for predator and prey
            double[,] preyAndPredatorCurves;

            try
            {
                // checking what algorith to use
                if (RKF45.Checked)
                {
                    preyAndPredatorCurves = RungeKutta.Rkf45(preyPop, predatorPop, (double)PreyPopulation.Value,
                                                             (double)PredatorPopulation.Value, (int)TimeSpan.Value, 0.01);
                }
                else
                {
                    preyAndPredatorCurves = RungeKutta.Rk4(preyPop, predatorPop, (double)PreyPopulation.Value,
                                                           (double)PredatorPopulation.Value, (int)TimeSpan.Value, (double)StepSize.Value);
                }

                PointPairList preySolution = new PointPairList();

                for (int i = 0; i <= preyAndPredatorCurves.GetUpperBound(1); i++)
                {
                    preySolution.Add(preyAndPredatorCurves[2, i], preyAndPredatorCurves[0, i]);
                }

                PointPairList predatorSolution = new PointPairList();

                for (int i = 0; i <= preyAndPredatorCurves.GetUpperBound(1); i++)
                {
                    predatorSolution.Add(preyAndPredatorCurves[2, i], preyAndPredatorCurves[1, i]);
                }

                // Adding prey population curve
                LineItem preyCurve = pane.AddCurve("Prey population",
                                                   preySolution, Color.Green, SymbolType.None);
                preyCurve.Line.Width    = 2;
                preyCurve.Line.IsSmooth = true;

                // Adding predator population curve
                LineItem predatorCurve = pane.AddCurve("Predator population",
                                                       predatorSolution, Color.Red, SymbolType.None);
                predatorCurve.Line.Width    = 2;
                predatorCurve.Line.IsSmooth = true;
            }
            catch (ArgumentException ex)
            {
                MessageBox.Show(ex.Message + "\nPlease enter smaller parameters", "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            // Changing axis to fit the curves
            GraphControl.AxisChange();
            GraphControl.Invalidate();
        }