Esempio n. 1
0
        private void Button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "" && textBox2.Text != "")
            {
                double  x1      = Convert.ToDouble(textBox1.Text);
                double  x2      = Convert.ToDouble(textBox2.Text);
                Secante secante = new Secante();
                Salida  salida  = new Salida();
                salida = secante.MetodoSecante(x1, x2);


                if (salida.Raiz.ToString() == "NaN" || salida.ErrorRelativo.ToString() == "NaN")
                {
                    salida.ErrorMsje = "Mal elegidos los extremos";
                }
                else
                {
                    textBox8.Text = salida.Raiz.ToString();
                    textBox7.Text = salida.NroIteraciones.ToString();
                    textBox6.Text = Convert.ToDecimal(salida.ErrorRelativo).ToString();
                }
                textBox5.Visible = true;
                textBox5.Text    = salida.ErrorMsje;
                if (salida.ErrorMsje != null)
                {
                    textBox8.Text = 0.ToString();
                    textBox7.Text = 0.ToString();
                    textBox6.Text = 0.ToString();
                }
            }
        }
Esempio n. 2
0
        public static List <Secante> secante(string expresion, double x0, double x1, double valorVerdadero, double errorTolerancia, bool hayValorVverdadero)
        {
            int    iteracion   = 0;
            double fx0         = 0;
            double fx1         = 0;
            double aux         = 0;
            double errorAprox  = 0;
            double errorVerd   = 0;
            bool   ea_menor_es = false;

            Secante        secante      = new Secante();
            List <Secante> listaSecante = new List <Secante>();

            do
            {
                iteracion++;

                fx0 = evaluarFuncion(expresion, x0);
                fx1 = evaluarFuncion(expresion, x1);

                aux = x1;
                x1  = x1 - (fx1 * (x0 - x1)) / (fx0 - fx1);
                x0  = aux;

                if (hayValorVverdadero)
                {
                    errorVerd = calcularErrorVerdadero(valorVerdadero, x1);
                }

                errorAprox = calcularErrorAproximacion(x1, x0);
                if (iteracion > 1)
                {
                    if (errorAprox < errorTolerancia)
                    {
                        ea_menor_es = true;
                    }
                }

                if (hayValorVverdadero)
                {
                    secante = new Secante(iteracion, x1, x0, fx1, fx0, errorVerd, errorAprox);
                }
                else
                {
                    secante = new Secante(iteracion, x1, x0, fx1, fx0, 0, errorAprox);
                }
                listaSecante.Add(secante);
            } while (!ea_menor_es);

            return(listaSecante);
        }
Esempio n. 3
0
        private void btnAplicarMetodo_Click(object sender, EventArgs e)
        {
            listaSecante.Clear();
            errorProvider.Clear();
            Secante secante      = new Secante();
            Calculo funcion      = new Calculo();
            int     cifrasSignif = 8;
            bool    hayErrores   = false;

            double x0 = 0;
            double x1 = 0;
            double errorTolerancia = 0.0;
            double valorVerd       = 0;
            string expresion       = null;

            bool hayValorVerdadero = true;

            if (string.IsNullOrEmpty(txtVv.Text))
            {
                hayValorVerdadero = false;
            }
            else
            {
                valorVerd = Convert.ToDouble(txtVv.Text);
                if (valorVerd == 0)
                {
                    hayValorVerdadero = false;
                }
            }

            if (datosLlenos())
            {
                expresion         = txtExpresion.Text;
                expresion         = expresion.Replace(',', '.');
                txtExpresion.Text = expresion;

                if (!funcion.Sintaxis(expresion, 'x'))
                {
                    errorProvider.SetError(txtExpresion, "Hay un error en la expresión f(x)");
                    hayErrores = true;
                }

                cifrasSignif    = Convert.ToInt32(cmbCifras.SelectedItem);
                x0              = Convert.ToDouble(txtX0.Text);
                x1              = Convert.ToDouble(txtX1.Text);
                errorTolerancia = Convert.ToDouble(txtEs.Text);

                if (!hayErrores)
                {
                    //Llama al método de la secante
                    listaSecante = Algoritmos.Algoritmos.secante(expresion, x0, x1, valorVerd, errorTolerancia, hayValorVerdadero);

                    mostrarDatosEnTabla(cifrasSignif, hayValorVerdadero);
                    lblRaiz.Text = Algoritmos.Algoritmos.toCifraSignif(listaSecante[listaSecante.Count - 1].X1, cifrasSignif);
                    lblEa.Text   = Algoritmos.Algoritmos.toCifraSignif(listaSecante[listaSecante.Count - 1].Ea, cifrasSignif) + "%";
                }
            }
            else
            {
                MessageBox.Show("Faltan datos por ingresar", "¡ERROR!");
            }
        }