コード例 #1
0
        public Polinomio retornarPolinomio(List <List <float> > fs, List <float> xs)
        {
            String    polinomioSinResolver = "P(x)=";
            Polinomio polInterpolante      = new Polinomio();

            for (int j = 0; j < fs.Count; j++)
            {
                if (fs[j].Count != 0)//sin este if rompe al tener solo un punto
                {
                    Polinomio termino = new Polinomio();
                    termino.AgregarCoeficiente(1);

                    polinomioSinResolver += fs[j].First().ToString();
                    for (int i = 0; i < j; i++)
                    {
                        Polinomio aux = new Polinomio();
                        aux.AgregarCoeficiente(-xs[i]);
                        aux.AgregarCoeficiente(1);
                        polinomioSinResolver += "(x-" + xs[i] + ")";
                        termino = termino.Multiplicar(aux);
                    }
                    if (j != fs.Count - 1)
                    {
                        polinomioSinResolver += "+";
                    }
                    termino         = termino.MultiplicarEscalar(fs[j].First());
                    polInterpolante = polInterpolante.Sumar(termino);
                    pasos.Add(polinomioSinResolver);
                }
            }
            return(polInterpolante);
        }
コード例 #2
0
ファイル: Metodo.cs プロジェクト: EzeTardieu/MatSup
        public Polinomio aplicar(Dictionary <float, float> tablaValores)
        {
            reset();
            int grado = tablaValores.Count() - 1;

            xs = tablaValores.Keys.ToList();
            ys = tablaValores.Values.ToList();
            ls = new List <Polinomio>();

            int n = tablaValores.Count();

            int a = 0;

            pasos.Add("Calcular los Li(Xi)");
            foreach (var raiz in xs)
            {
                var xsNew = xs.Where(x => x != raiz).ToList();

                var pol = new Polinomio();
                pol.coeficientes.Add(1);
                foreach (var raizNew in xsNew)
                {
                    List <float> coefAux = new List <float>();
                    coefAux.Add(-raizNew);
                    coefAux.Add(1);
                    pol = pol.Multiplicar(new Polinomio(coefAux));
                }
                float denominador = 1;
                foreach (var raizNew in xsNew)
                {
                    denominador *= (xs.First(x => x == raiz) - raizNew);
                }
                pol = pol.DividirEscalar(denominador);
                a++;
                ls.Add(pol);
                pasos.Add("L" + a + "(" + raiz + ") = " + pol.Formatear());
            }
            int y = 0;

            Polinomio polInterpolante = new Polinomio();

            pasos.Add("Calcular el polinomio interpolante");

            foreach (var imagen in ys)
            {
                Polinomio polAux = ls[y].MultiplicarEscalar(imagen);
                polInterpolante = polInterpolante.Sumar(polAux);
                y++;
            }

            for (int b = 0; b < polInterpolante.coeficientes.Count; b++)
            {
                //polInterpolante.coeficientes[b] = (float) Math.Truncate(polInterpolante.coeficientes[b] * 10000) / 10000;
                polInterpolante.coeficientes[b] = (float)Math.Round(polInterpolante.coeficientes[b], 4);
            }

            pasos.Add("P(X)=" + polInterpolante.Formatear());
            return(polInterpolante);
        }