示例#1
0
        internal override List <PointD>[] Solve(double x0, double y0, double max, int num, int max_er)
        {
            double h            = (max - x0) / num; //step size
            PointD point        = new PointD();
            Exact  exacEquation = new Exact();

            List <PointD>[] result = new List <PointD> [3]; //creating a list
            result[0] = new List <PointD>();                // List for points of Euler's method grapgh
            result[1] = new List <PointD>();                // List for local errors of the Euler's method
            result[2] = new List <PointD>();                // List for global error plotting

            double x = x0, y = y0;

            while (x <= max) //Euler's method
            {
                point.X = x;
                point.Y = y;
                result[0].Add(point);
                double localError = Math.Abs(y - exacEquation.getValue(x, x0, y0));
                point.X = x;
                point.Y = localError;
                result[1].Add(point);
                double k1 = orig(x, y);
                double k2 = orig(x + h / 2, y + h * k1 / 2);
                double k3 = orig(x + h / 2, y + h * k2 / 2);
                double k4 = orig(x + h, y + h * k3);
                y = y + h * (k1 + 2 * k2 + 2 * k3 + k4) / 6;
                x = x + h;
            }

            //global error
            for (int i = 0; i < max_er; i++)
            {
                x = x0;
                y = y0;
                h = (max - x0) / (i + 1);
                for (int j = 0; j < i + 1; j++)
                {
                    double k1 = orig(x, y);
                    double k2 = orig(x + h / 2, y + h * k1 / 2);
                    double k3 = orig(x + h / 2, y + h * k2 / 2);
                    double k4 = orig(x + h, y + h * k3);
                    y = y + h * (k1 + 2 * k2 + 2 * k3 + k4) / 6;
                    x = x + h;
                }
                if (i > 1)
                {
                    double globalError = Math.Abs(y - exacEquation.getValue(x, x0, y0));
                    point.X = i;
                    point.Y = globalError;
                    result[2].Add(point);
                }
            }

            return(result);
        }
示例#2
0
        private void buttonCalculate_Click(object sender, EventArgs e)
        {
            Chart1.Series.Clear();
            Chart2.Series.Clear();
            Chart3.Series.Clear();

            double x0      = Double.Parse(textBoxX0.Text);
            double y0      = Double.Parse(textBoxY0.Text);
            double max     = Double.Parse(textBoxX.Text);
            int    num     = int.Parse(textBoxSteps.Text);
            int    max_err = int.Parse(textBoxIntervalFrom.Text);

            SeriesCollection[] series = new SeriesCollection[3];
            for (int i = 0; i < 3; i++)
            {
                series[i] = new SeriesCollection();
            }
            ChartValues <ObservablePoint>[] values = new ChartValues <ObservablePoint> [10];
            for (int i = 0; i < 10; i++)
            {
                values[i] = new ChartValues <ObservablePoint>();
            }

            List <PointD> exact = Exact.Solve(x0, y0, max, num);

            foreach (var value in exact)
            {
                values[0].Add(new ObservablePoint(value.X, value.Y));
            }
            series[0].Add(new LiveCharts.Wpf.LineSeries()
            {
                Title = "Exact", Values = values[0], PointGeometrySize = 0
            });


            List <PointD>[] result_euler = (new Euler()).Solve(x0, y0, max, num, max_err);
            foreach (var value in result_euler[0])
            {
                values[1].Add(new ObservablePoint(value.X, value.Y));
            }
            series[0].Add(new LiveCharts.Wpf.LineSeries()
            {
                Title = "Euler", Values = values[1], PointGeometrySize = 0
            });

            List <PointD>[] result_ext = (new Euler_ex()).Solve(x0, y0, max, num, max_err);
            foreach (var value in result_ext[0])
            {
                values[2].Add(new ObservablePoint(value.X, value.Y));
            }
            series[0].Add(new LiveCharts.Wpf.LineSeries()
            {
                Title = "Euler_ex", Values = values[2], PointGeometrySize = 0
            });

            List <PointD>[] result_ru = (new Runge_Kutta()).Solve(x0, y0, max, num, max_err);
            foreach (var value in result_ru[0])
            {
                values[3].Add(new ObservablePoint(value.X, value.Y));
            }
            series[0].Add(new LiveCharts.Wpf.LineSeries()
            {
                Title = "Runge_Kutta", Values = values[3], PointGeometrySize = 0
            });

            Chart1.Series = series[0];

            foreach (var value in result_euler[1])
            {
                values[4].Add(new ObservablePoint(value.X, value.Y));
            }
            series[1].Add(new LiveCharts.Wpf.LineSeries()
            {
                Title = "Euler", Values = values[4], PointGeometrySize = 0
            });

            foreach (var value in result_ext[1])
            {
                values[5].Add(new ObservablePoint(value.X, value.Y));
            }
            series[1].Add(new LiveCharts.Wpf.LineSeries()
            {
                Title = "Euler_ex", Values = values[5], PointGeometrySize = 0
            });

            foreach (var value in result_ru[1])
            {
                values[6].Add(new ObservablePoint(value.X, value.Y));
            }
            series[1].Add(new LiveCharts.Wpf.LineSeries()
            {
                Title = "Runge_Kutta", Values = values[6], PointGeometrySize = 0
            });

            Chart2.Series = series[1];

            foreach (var value in result_euler[2])
            {
                values[7].Add(new ObservablePoint(value.X, value.Y));
            }
            series[2].Add(new LiveCharts.Wpf.LineSeries()
            {
                Title = "Euler", Values = values[7], PointGeometrySize = 0
            });

            foreach (var value in result_ext[2])
            {
                values[8].Add(new ObservablePoint(value.X, value.Y));
            }
            series[2].Add(new LiveCharts.Wpf.LineSeries()
            {
                Title = "Euler_ex", Values = values[8], PointGeometrySize = 0
            });

            foreach (var value in result_ru[2])
            {
                values[9].Add(new ObservablePoint(value.X, value.Y));
            }
            series[2].Add(new LiveCharts.Wpf.LineSeries()
            {
                Title = "Runge_Kutta", Values = values[9], PointGeometrySize = 0
            });

            Chart3.Series = series[2];
        }