Exemplo n.º 1
0
        public static List <double> CalcLSM(Gf gf, List <Point> points)
        {
            List <List <double> > a = new List <List <double> >();
            List <double>         b = new List <double>();
            int n = points.Count;

            for (int i = 0; i < gf.getK(); i++)
            {
                a.Add(new List <double>());
                for (int j = 0; j < gf.getK(); j++)
                {
                    double av = 0;
                    for (int l = 0; l < n; l++)
                    {
                        av += gf.calc(j, points[l].X) * gf.calc(i, points[l].X);
                    }
                    a[i].Add(av);
                }
                double bv = 0;
                for (int l = 0; l < n; l++)
                {
                    bv += points[l].Y * gf.calc(i, points[l].X);
                }
                b.Add(bv);
            }
            return(Gauss.Solve(a, b));
        }
Exemplo n.º 2
0
        public void DrawFunction(Graphics g, Gf f, List <double> c, Point s, Point e, Matrix transform)
        {
            if (s.X > e.X)
            {
                Point b = s;
                s = e;
                e = b;
            }
            Point  previous = new Point();
            Point  current  = new Point();
            Point  pt       = null;
            Point  ct       = null;
            double step     = (e.X - s.X) / 10;

            for (double x = s.X; x < e.X; x += step)
            {
                current.X = x;
                current.Y = f.CalcAll(c, x);
                ct        = transform.MulP(current);
                if (x > s.X)
                {
                    g.DrawLine(mLinePen, (float)pt.X, (float)pt.Y, (float)ct.X, (float)ct.Y);
                }
                previous.X = current.X;
                previous.Y = current.Y;
                pt         = ct;
            }
            current.X = e.X;
            current.Y = f.CalcAll(c, e.X);
            ct        = transform.MulP(current);
            g.DrawLine(mLinePen, (float)pt.X, (float)pt.Y, (float)ct.X, (float)ct.Y);
        }
Exemplo n.º 3
0
        protected override void OnMouseMove(MouseEventArgs e)
        {
            if ((e.Button & MouseButtons.Left) == 0)
            {
                return;
            }
            Point p = new Point();

            p.X = e.X;
            p.Y = e.Y;
            list_V.Add(p);


            Gf            bestF = funcs[0];
            List <double> bestC = null;

            foreach (Gf func in funcs)
            {
                List <double> c = LSM.CalcLSM(func, list_V);
                bestC = c;
            }

            if (bestC == null)
            {
                return;
            }



            float r = 5;

            ImageGraphics.FillRectangle(mBackgroundBrush, 0, 0, 800, 800);
            foreach (Point point in list_V)
            {
                ImageGraphics.FillEllipse(mDotBrush, (float)point.X - r, (float)point.Y - r, 2 * r, 2 * r);
            }

            var n = p[0].;
Exemplo n.º 4
0
        public Function InterpolatePoints()
        {
            if (mPointList.Count < 5)
            {
                return(null);
            }
            List <Point> tp = new List <Point>();
            //get vect
            Vector directionVector = new Vector();

            directionVector.X = mPointList[mPointList.Count - 1].X - mPointList[0].X;
            directionVector.Y = mPointList[mPointList.Count - 1].Y - mPointList[0].Y;
            //normalize
            directionVector = directionVector.Normalize();
            //cslcu matrix
            Matrix m  = Matrix.mat(directionVector);
            Matrix mi = Matrix.Invert(m);

            //transform
            int n = mPointList.Count;

            for (int i = 0; i < n; i++)
            {
                tp.Add(mi.MulP(mPointList[i]));
            }

            List <double> bestC   = null;
            double        bestSqr = double.MaxValue;
            Gf            bestF   = funcs[0];

            foreach (Gf func in funcs)
            {
                List <double> c = LSM.CalcLSM(func, tp);
                if (c == null)
                {
                    continue;
                }
                double sqrs = 0;
                for (int j = 0; j < tp.Count; j++)
                {
                    sqrs += Math.Pow(tp[j].Y - func.CalcAll(c, tp[j].X), 2);
                }
                if (sqrs < bestSqr)
                {
                    bestSqr = sqrs;
                    bestF   = func;
                    bestC   = c;
                }
            }
            if (bestC == null)
            {
                return(null);
            }

            Function mFunction = new Function();

            mFunction.BestFunction             = bestF;
            mFunction.BestCoefficient          = bestC;
            mFunction.bestSqr                  = bestSqr;
            mFunction.Transform                = mi;
            mFunction.TransformInverse         = m;
            mFunction.TransformStartingPoint   = tp[0];
            mFunction.TransformStartingPoint.Y = mFunction.BestFunction.CalcAll(mFunction.BestCoefficient, mFunction.TransformStartingPoint.X);
            mFunction.TransformEndingPoint     = tp[mPointList.Count - 1];
            mFunction.TransformEndingPoint.Y   = mFunction.BestFunction.CalcAll(mFunction.BestCoefficient, mFunction.TransformEndingPoint.X);
            mFunction.StartingPoint            = mFunction.TransformInverse.MulP(mFunction.TransformStartingPoint);
            mFunction.EndingPoint              = mFunction.TransformInverse.MulP(mFunction.TransformEndingPoint);
            return(mFunction);
        }