示例#1
0
        public ExponentialViewModel(double[] xdata, double[] ydata, Tuple <double, double> xlimit, Tuple <double, double> ylimit)
        {
            displayed = true;
            DirectRegressionMethod method = DirectRegressionMethod.QR;
            Tuple <double, double> p      = Fit.Exponential(xdata, ydata, method);
            double a = p.Item1;
            double r = p.Item2;

            if (!Double.IsNaN(a) && !Double.IsNaN(r))
            {
                this.FunctionModel = new PlotModel {
                    Title = "Exponential regresion [ f(x) = " + a + " * exp( " + r + " * x ) ]"
                };
                Func <double, double> exponentialFunction = (x) => a *Math.Exp(r *x);

                FunctionModel.Series.Add(new FunctionSeries(exponentialFunction, xlimit.Item1, xlimit.Item2, 0.0001));
                // view limits
                FunctionModel.Axes.Add(new LinearAxis {
                    Position = AxisPosition.Bottom, Minimum = xlimit.Item1, Maximum = xlimit.Item2
                });
                FunctionModel.Axes.Add(new LinearAxis {
                    Position = AxisPosition.Left, Minimum = ylimit.Item1, Maximum = ylimit.Item2
                });
            }
            else
            {
                displayed = false;
            }
        }
        /// <summary>
        /// Least-Squares fitting the points (x, y) to an exponential
        /// y : x -> a * exp(r * x).
        /// </summary>
        public static IModelledFunction ExponentialFunc(double[] xArray, double[] yArray)
        {
            Tuple <double, double> parameters = Fit.Exponential(xArray, yArray);
            double a = parameters.Item1;
            double r = parameters.Item2;

            Func <double, double> function = t => a *System.Math.Exp(r *t);

            return(new ExponentialFunction(function, parameters));
        }
示例#3
0
        public List <DataPoint> CalculatePoints(List <DataPoint> points)
        {
            var pointsLine           = new List <DataPoint>();
            Tuple <double, double> p = Fit.Exponential(points.Select(x => x.X).ToArray(), points.Select(x => x.Y).ToArray());
            double a = p.Item1;
            double b = p.Item2;

            foreach (var item in points)
            {
                var y = a * Math.Exp(item.X * b);
                pointsLine.Add(new DataPoint(item.X, y));
            }

            return(pointsLine);
        }
示例#4
0
        public FitData CalcFit(List <DataPoint> points)
        {
            FitData result           = new FitData("Exponential");
            var     xdata            = points.Select(x => x.X).ToArray();
            var     ydata            = points.Select(x => x.Y).ToArray();
            Tuple <double, double> p = Fit.Exponential(xdata, ydata);

            result.A = p.Item1;
            result.R = p.Item2;

            result.Points = DataPoints(points).ToList();

            return(result);

            //local function for return list of regresion Points[need to C# 8]
            IEnumerable <DataPoint> DataPoints(List <DataPoint> points)
            {
                foreach (var point in points)
                {
                    yield return(new DataPoint(point.X, result.A * Math.Exp(result.R * point.X)));
                }
            }
        }
示例#5
0
        public void FitsToExponentialSameAsExcelTrendLine()
        {
            // X	Y
            // 1   0.2
            // 2   0.3
            // 4   1.3
            // 6   4.2
            // -> y = 0.0981*exp(0.6284*x)

            var x = new[] { 1.0, 2.0, 4.0, 6.0 };
            var y = new[] { 0.2, 0.3, 1.3, 4.2 };

            var resp = Fit.Exponential(x, y);

            Assert.AreEqual(0.0981, resp.Item1, 1e-3);
            Assert.AreEqual(0.6284, resp.Item2, 1e-3);

            var resf = Fit.ExponentialFunc(x, y);

            foreach (var z in Enumerable.Range(-3, 10))
            {
                Assert.AreEqual(0.0981 * Math.Exp(0.6284 * z), resf(z), 1e-2);
            }
        }
示例#6
0
 public virtual Tuple <double, double> ExponentialFitting(double[] x, double[] y)
 {
     return(Fit.Exponential(x, y));
 }