Example #1
0
 public Regresion_Polinomial(bool resultado_correcto, string mensaje, double coeficiente, Resultado_TP2 resultado_sistema_de_ecuaciones)
 {
     Resultado_Correcto = resultado_correcto;
     Mensaje            = mensaje;
     Coeficiente        = coeficiente;
     Resultado_Sistema_de_Ecuaciones = resultado_sistema_de_ecuaciones;
 }
Example #2
0
        public void MostrarResultadosMinimosCuadrados(Resultado_TP2 rdos)
        {
            string[] v = new string[6] {
                "a0 = ", "a1 = ", "a2 = ", "a3 = ", "a4 = ", "a5 = "
            };
            lbl_textoMC.Visible = true;
            lbl_textoMC.Text    = rdos.Mensaje;
            lbl_textoMC.Font    = new Font(lbl_textoMC.Font.Name, 10);
            panel3.Controls.Add(lbl_textoMC);
            int pointX = 25;
            int pointY = 55;

            if (rdos.Ok != false)
            {
                for (int i = 0; i < rdos.Resoluciones.Length; i++)
                {
                    Label lbl = new Label();
                    lbl.Name      = "lbl_Resultado_" + i;
                    lbl.AutoSize  = false;
                    lbl.Size      = new System.Drawing.Size(85, 17);
                    lbl.Font      = new Font(lbl.Font.Name, 8);
                    lbl.Location  = new Point(pointX, pointY);
                    lbl.Text      = v[i] + Math.Round(rdos.Resoluciones[i], 5);
                    lbl.ForeColor = Color.Red;
                    panel3.Controls.Add(lbl);
                    panel3.Show();
                    pointX += 100;
                }
                lbl_coeficiente.Text    = "Coeficiente de correlación = " + rdos.Coeficiente;
                lbl_coeficiente.Visible = true;
            }
        }
Example #3
0
        public void MostrarEnPantalla(Resultado_TP2 result)
        {
            string[] s = new string[5] {
                "x1 = ", "x2 = ", "x3 = ", "x4 = ", "x5 = "
            };
            lbl_texto.Visible = false;
            if (result.Mensaje != "")
            {
                lbl_texto.Text = result.Mensaje;
            }
            else
            {
                lbl_texto.Text = "El método encontró las soluciones";
            }

            lbl_texto.Font = new Font(lbl_texto.Font.Name, 10);
            panel2.Controls.Add(lbl_texto);
            int pointX = 25;
            int pointY = 225;

            for (int i = 0; i < result.Resoluciones.Length; i++)
            {
                Label lbl = new Label
                {
                    Name     = "lbl_Resultado_" + i,
                    AutoSize = false,
                    Size     = new Size(120, 17)
                };
                lbl.Font      = new Font(lbl.Font.Name, 10);
                lbl.Location  = new Point(pointX, pointY);
                lbl.Text      = s[i] + Math.Round(result.Resoluciones[i], 6);
                lbl.ForeColor = Color.DarkGreen;
                panel2.Controls.Add(lbl);
                panel2.Show();
                pointX += 120;
            }
            if (result.Iteraciones != 0)
            {
                Label lbl = new Label
                {
                    Name     = "lbl_Iteraciones",
                    AutoSize = false,
                    Size     = new Size(200, 17)
                };
                lbl.Font      = new Font(lbl.Font.Name, 10);
                lbl.Location  = new Point(25, 250);
                lbl.Text      = "Cantidad de Iteraciones: " + result.Iteraciones;
                lbl.ForeColor = Color.Green;
                panel2.Controls.Add(lbl);
                panel2.Show();
            }
        }
Example #4
0
        public static Resultado_TP2 RegresionPorMC(double[] vector_x, double[] vector_y, int n, int Grado, int max_grado)
        {
            //Obtengo el sistema de ecuaciones
            double[,] auxiliar = RegresionPolinomial(vector_x, vector_y, n, Grado);
            Resultado_TP2 res = Practico2.GaussJordan(auxiliar, Grado);

            if (res.Ok != false)
            {
                Resultado_TP2 resu = new Resultado_TP2();
                resu.Ok = true;
                //Calcular sr y st para poder calcular el coeficiente de correlación
                double sum_y = 0, sr = 0, st = 0, sr_temp;
                for (int i = 0; i < n + 1; i++)
                {
                    sum_y += vector_y[i];
                }
                double media_y = sum_y / n;
                if (Grado == 2)
                {
                    double sum_xx = 0;
                    double sum_x  = 0;
                    double sum_xy = 0;
                    for (int i = 0; i < n + 1; i++)
                    {
                        sum_xx += Math.Pow(vector_x[i], 2);
                        sum_x  += vector_x[i];
                        sum_xy += vector_x[i] * vector_y[i];
                    }
                    double media_x = sum_x / n;
                    double a1      = (n * sum_xy - sum_x * sum_y) / (n * sum_xx - Math.Pow(sum_x, 2));
                    resu.Resoluciones[0] = a1;

                    double a0 = media_y - (a1 * media_x);
                    resu.Resoluciones[1] = a0;
                    sr = 0;
                    st = 0;
                    for (int i = 0; i < n; i++)
                    {
                        // Se calcula el coeficiente de la recta de mejor ajuste
                        sr += Math.Pow(((a1 * vector_x[i]) + a0 - vector_y[i]), 2);
                        // Se calcula rl coeficiente de la recta promedio
                        st += Math.Pow((media_y - vector_y[i]), 2);
                    }
                    resu.Resoluciones[0] = a0;
                    resu.Resoluciones[1] = a1;
                }
                else
                {
                    for (int i = 0; i < n; i++)
                    {
                        sr_temp = vector_y[i] - res.Resoluciones[0];

                        for (int j = max_grado; j > 0; j--)
                        {
                            sr_temp -= Grado - 1 >= j?Math.Pow(vector_x[i], j) * res.Resoluciones[j] : 0;
                        }
                        sr += Math.Pow(sr_temp, 2);
                        st += Math.Pow(vector_y[i] - (sum_y / n), 2);
                    }
                }

                //Evaluar coeficiente de correlación
                if (Grado == 2)
                {
                    resu.Coeficiente = Math.Sqrt(((st - sr) / st)) * 100;
                    return(resu);
                }
                else
                {
                    res.Coeficiente = Math.Sqrt(((st - sr) / st)) * 100;
                    return(res);
                }
            }
            else
            {
                res.Coeficiente = -1;
            }
            return(res);
        }
Example #5
0
        private void btnCalcular_Click(object sender, EventArgs e)
        {
            for (int i = panel3.Controls.Count - 1; i >= 0; i--)
            {
                if ((panel3.Controls[i] is Label label) && (label.Name != "lbl_textoMC"))
                {
                    panel3.Controls.RemoveAt(i);
                    label.Dispose();
                }
            }

            lbl_coeficiente.Visible = false;
            panel3.Update();
            panel3.Refresh();

            double[] vectorX = new double[15];
            double[] vectorY = new double[15];

            //Vector de valores x
            int contador = -1;

            foreach (DataGridViewRow row in dgvXeY.Rows)
            {
                contador += 1;
                string codigo = Convert.ToString(row.Cells["X"].Value);
                if (codigo != "")
                {
                    vectorX[contador] = double.Parse(codigo);
                }
            }
            int grad = contador - 1;

            //Vector de valores y
            contador = -1;
            foreach (DataGridViewRow row in dgvXeY.Rows)
            {
                contador += 1;
                string codigo = Convert.ToString(row.Cells["Y"].Value);
                if (codigo != "")
                {
                    vectorY[contador] = double.Parse(codigo);
                }
            }

            grad = int.Parse(txt_Grado.Text);
            Resultado_TP2 res = new Resultado_TP2(true, "Ajuste no aceptable para polinomios de grado mayor a " + (max_grado), 0, 50);

            if (grad == 0)
            {
                res         = Logica.Unidad_3.Unidad3.RegresionPorMC(vectorX, vectorY, contador, grad + 1, max_grado);
                res.Mensaje = "Los valores son:";
            }
            else
            {
                while (grad < max_grado + 1 & (res.Coeficiente < double.Parse(txt_TP3_Tolerancia.Text)) & res.Ok == true)
                {
                    res = Logica.Unidad_3.Unidad3.RegresionPorMC(vectorX, vectorY, contador, grad + 1, max_grado);  //(vectorX, vectorY, contador, grad + 1, max_grado);
                    if (res.Coeficiente >= double.Parse(txt_TP3_Tolerancia.Text))
                    {
                        res.Mensaje = "Los valores son:";
                    }
                    else
                    {
                        grad += 1;
                    }
                }
                if (grad == max_grado + 1)
                {
                    res.Ok = false;
                }
            }

            MostrarResultadosMinimosCuadrados(res);
        }
Example #6
0
        private void btn_Resolver_Click_1(object sender, EventArgs e)
        {
            int  dim          = int.Parse(textBox1.Text);
            bool camposvacios = false;

            for (int i = 1; i <= dim + 1; i++)
            {
                for (int j = 1; j <= dim; j++)
                {
                    string    nom = "txt" + j + i;
                    Control[] m   = panel2.Controls.Find(nom, true);
                    if (m[0].Text.Trim() == string.Empty)
                    {
                        MessageBox.Show("Hay campos vacíos!");
                        camposvacios = true;
                    }
                }
            }
            if (!camposvacios)
            {
                string[] aux;
                double[,] matriz = new double[dim, dim + 1];
                for (int i = 0; i < dim; i++)
                {
                    for (int j = 0; j < dim + 1; j++)
                    {
                        string nom = "txt";
                        int    x   = j + 1;
                        int    y   = i + 1;
                        nom = nom + y + x;
                        Control[] m = panel2.Controls.Find(nom, true);
                        if (m[0].Text.Contains("/"))
                        {
                            aux          = m[0].Text.Split(new char[] { '/' }, 2);
                            matriz[i, j] = double.Parse(aux[0]) / double.Parse(aux[1]);
                        }
                        else
                        {
                            matriz[i, j] = double.Parse(m[0].Text);
                        }
                    }
                }
                Resultado_TP2 result = new Resultado_TP2(false, "", 0, 0);
                if (cmb_Metodos.SelectedIndex == 0)
                {
                    result = Practico2.GaussJordan(matriz, dim);
                }
                else
                {
                    double tole = 0.0001; //cambiar la tolerancia acá si se necesita para el eje 5
                    int    ite  = 100;    // 2 para el ejercicio 5
                    result = Practico2.GaussSeidel(matriz, dim, ite, tole);
                }

                if (result.Ok)
                {
                    MostrarEnPantalla(result);
                }
                else
                {
                    MessageBox.Show(result.Mensaje);
                }
            }
        }