예제 #1
0
        // GET: Main
        public ActionResult Index()
        {
            //REngine.SetEnvironmentVariables();
            REngine.SetEnvironmentVariables("C:/Rtools/R/R342/bin/i386", "C:/Rtools/R/R342");
            // There are several options to initialize the engine, but by default the following suffice:
            REngine engine = REngine.GetInstance();

            //smaple 1 -- use statics function
            //engine.Evaluate("library(stats)");
            //engine.Evaluate("options(digits=2)");
            //var colname = new string[] { "Student","Math","Science","English" };
            //var Student = new string[] { "john dav","angela willia","bullwink moose","david jone","janice mark","chery cush","reuven ytz","greg knox","joel england","mary ray"};
            //var Math = new int[] { 502,600,412,358,495,512,410,625,573,522};
            //var Science = new int[] { 95,99,80,82,75,85,80,95,89,86};
            //var English = new int[] { 25,22,18,15,20,28,15,30,27,18};
            //IEnumerable[] srcdata = new IEnumerable[4];
            //srcdata[0] = Student;
            //srcdata[1] = Math;
            //srcdata[2] = Science;
            //srcdata[3] = English;
            //var roster = engine.CreateDataFrame(srcdata, colname);

            //engine.SetSymbol("roster", roster);
            //engine.Evaluate("z <- scale(roster[,2:4])");
            //engine.Evaluate("score <- apply(z,1,mean)");
            //engine.Evaluate("roster <- cbind(roster,score)");
            //engine.Evaluate("y <- quantile(score, c(.8,.6,.4,.2))");
            //engine.Evaluate("roster$grad[score >= y[1]] <- \"A\"");
            //engine.Evaluate("roster$grad[score < y[1] & score >= y[2]] <- \"B\"");
            //engine.Evaluate("roster$grad[score < y[2] & score >= y[3]] <- \"C\"");
            //engine.Evaluate("roster$grad[score < y[3] & score >= y[4]] <- \"D\"");
            //engine.Evaluate("roster$grad[score < y[4]] <- \"F\"");
            //engine.Evaluate("roster <- roster[order(roster$Student),]");

            //roster = engine.GetSymbol("roster").AsDataFrame();

            //var colnum = roster.ColumnCount;
            //var rows =  roster.GetRows();
            //foreach (DataFrameRow r in rows)
            //{
            //    var vlist = new List<object>();
            //    for (var cl = 0; cl < colnum; cl++)
            //    {
            //        vlist.Add(r[cl]);
            //    }
            //    System.Windows.MessageBox.Show(string.Join(",", vlist));
            //}

            //sample2 -- generate plot
            var plotfolder = this.Server.MapPath("~/userfiles") + "/docs/plot/";

            CreateFolder(plotfolder);
            var fn       = Guid.NewGuid().ToString("N") + ".png";
            var plotname = plotfolder + fn;
            var url      = "/userfiles/docs/plot/" + fn;

            var does   = new double[] { 20, 30, 40, 45, 60 };
            var druga  = new double[] { 16, 20, 27, 40, 60 };
            var cdoes  = engine.CreateNumericVector(does);
            var cdruga = engine.CreateNumericVector(druga);

            engine.Evaluate("library(graphics)");
            engine.SetSymbol("does", cdoes);
            engine.SetSymbol("draga", cdruga);
            engine.Evaluate("png('" + plotname.Replace("\\", "/") + "')");
            engine.Evaluate("plot(does,draga,type='b')");
            engine.Evaluate("dev.off()");
            engine.ClearGlobalEnvironment();
            ViewBag.url = url;

            return(View());
        }
예제 #2
0
파일: Form1.cs 프로젝트: bordenchen/DBSCAN
 private void button1_Click(object sender, EventArgs e)
 {
     en.ClearGlobalEnvironment();
 }
예제 #3
0
        private void button1_Click(object sender, EventArgs e)
        {
            REngine.SetEnvironmentVariables();

            textBox6.Text = "";
            textBox6.Hide();
            REngine engine = REngine.GetInstance();

            engine.Evaluate(".libPaths('C:\\\\Program Files\\\\R\\\\R-3.4.3\\\\library')");
            pictureBox1.Hide();
            string rExp, input, bl = "\r\n", its = "";
            float  a, b, valorReal = 0, Stop, aux;

            if ((comboBox2.SelectedItem.Equals("ER") && float.Parse(textBox4.Text) != 0) || !comboBox2.SelectedItem.Equals("ER"))
            {
                if (comboBox2.SelectedItem.Equals("EA") || comboBox2.SelectedItem.Equals("ER"))
                {
                    try
                    {
                        valorReal = float.Parse(textBox4.Text);
                    }
                    catch (Exception l)
                    {
                        MessageBox.Show("Insira o Valor Real.");
                    }
                }

                #region METODO DA BISSEÇÃO
                if (comboBox1.SelectedItem.Equals("Método da Bisseção"))
                {
                    int   count = 1;
                    float midT = 0, mid = 0;

                    try
                    {
                        input = textBox1.Text;
                        string aT = textBox2.Text;
                        string bT = textBox3.Text;
                        Stop = float.Parse(textBox5.Text);

                        if (comboBox2.SelectedItem.Equals("EA"))
                        {
                            its += input + bl;
                            its += "[a,b] = [" + aT + "," + bT + "]" + bl;

                            do
                            {
                                its += bl + "-> Iteração " + Convert.ToString(count) + bl + bl;
                                count++;

                                rExp = "x=" + aT + ";" + input;
                                a    = float.Parse(rExe(engine, rExp));
                                its += "f(" + aT + ") = " + Convert.ToString(a);
                                if (a < 0)
                                {
                                    its += " < 0" + bl;
                                }
                                else
                                {
                                    its += " > 0" + bl;
                                }

                                rExp = "x=" + bT + ";" + input;
                                b    = float.Parse(rExe(engine, rExp));
                                its += "f(" + bT + ") = " + Convert.ToString(b);
                                if (b < 0)
                                {
                                    its += " < 0" + bl;
                                }
                                else
                                {
                                    its += " > 0" + bl;
                                }

                                midT = (float.Parse(aT) + float.Parse(bT)) / 2;
                                rExp = "x=" + Convert.ToString(midT) + ";" + input;
                                mid  = float.Parse(rExe(engine, rExp));
                                its += "(a+b)/2 = " + Convert.ToString(midT) + bl;
                                its += "f(" + midT + ") = " + Convert.ToString(mid);
                                if (mid < 0)
                                {
                                    its += " < 0" + bl;
                                }
                                else
                                {
                                    its += " > 0" + bl;
                                }
                                its += "Erro absoluto: " + Convert.ToString(Math.Abs(valorReal - midT)) + bl;

                                if ((mid > 0 && b > 0 && a < 0) || (mid < 0 && b < 0 && a > 0))
                                {
                                    bT = Convert.ToString(midT);
                                }
                                else
                                {
                                    aT = Convert.ToString(midT);
                                }
                            }while (Math.Abs(valorReal - midT) > Stop);
                            its += bl + "Resultado: " + midT + bl;
                            its += bl;
                        }
                        else if (comboBox2.SelectedItem.Equals("ER"))
                        {
                            its += input + bl;
                            its += "[a,b] = [" + aT + "," + bT + "]" + bl;

                            do
                            {
                                its += bl + "-> Iteração " + Convert.ToString(count) + bl + bl;
                                count++;

                                rExp = "x=" + aT + ";" + input;
                                a    = float.Parse(rExe(engine, rExp));
                                its += "f(" + aT + ") = " + Convert.ToString(a);
                                if (a < 0)
                                {
                                    its += " < 0" + bl;
                                }
                                else
                                {
                                    its += " > 0" + bl;
                                }

                                rExp = "x=" + bT + ";" + input;
                                b    = float.Parse(rExe(engine, rExp));
                                its += "f(" + bT + ") = " + Convert.ToString(b);
                                if (b < 0)
                                {
                                    its += " < 0" + bl;
                                }
                                else
                                {
                                    its += " > 0" + bl;
                                }

                                midT = (float.Parse(aT) + float.Parse(bT)) / 2;
                                rExp = "x=" + Convert.ToString(midT) + ";" + input;
                                mid  = float.Parse(rExe(engine, rExp));
                                its += "(a+b)/2 = " + Convert.ToString(midT) + bl;
                                its += "f(" + midT + ") = " + Convert.ToString(mid);
                                if (mid < 0)
                                {
                                    its += " < 0" + bl;
                                }
                                else
                                {
                                    its += " > 0" + bl;
                                }
                                its += "Erro relativo: " + Convert.ToString((Math.Abs(valorReal - midT)) / valorReal) + bl;

                                if ((mid > 0 && b > 0 && a < 0) || (mid < 0 && b < 0 && a > 0))
                                {
                                    bT = Convert.ToString(midT);
                                }
                                else
                                {
                                    aT = Convert.ToString(midT);
                                }
                            }while ((Math.Abs(valorReal - midT) / valorReal) > Stop);
                            its += bl + "Resultado: " + midT + bl;
                            its += bl;
                        }
                        else if (comboBox2.SelectedItem.Equals("Nº de iterações"))
                        {
                            its += input + bl;
                            its += "[a,b] = [" + aT + "," + bT + "]" + bl;

                            do
                            {
                                its += bl + "-> Iteração " + Convert.ToString(count) + bl + bl;
                                count++;

                                rExp = "x=" + aT + ";" + input;
                                a    = float.Parse(rExe(engine, rExp));
                                its += "f(" + aT + ") = " + Convert.ToString(a);
                                if (a < 0)
                                {
                                    its += " < 0" + bl;
                                }
                                else
                                {
                                    its += " > 0" + bl;
                                }

                                rExp = "x=" + bT + ";" + input;
                                b    = float.Parse(rExe(engine, rExp));
                                its += "f(" + bT + ") = " + Convert.ToString(b);
                                if (b < 0)
                                {
                                    its += " < 0" + bl;
                                }
                                else
                                {
                                    its += " > 0" + bl;
                                }

                                midT = (float.Parse(aT) + float.Parse(bT)) / 2;
                                rExp = "x=" + Convert.ToString(midT) + ";" + input;
                                mid  = float.Parse(rExe(engine, rExp));
                                its += "(a+b)/2 = " + Convert.ToString(midT) + bl;
                                its += "f(" + midT + ") = " + Convert.ToString(mid);
                                if (mid < 0)
                                {
                                    its += " < 0" + bl;
                                }
                                else
                                {
                                    its += " > 0" + bl;
                                }

                                its += "Erro absoluto: " + Convert.ToString(Math.Abs(valorReal - midT)) + bl;
                                its += "Erro relativo: " + Convert.ToString((Math.Abs(valorReal - midT)) / valorReal) + bl;

                                if ((mid > 0 && b > 0 && a < 0) || (mid < 0 && b < 0 && a > 0))
                                {
                                    bT = Convert.ToString(midT);
                                }
                                else
                                {
                                    aT = Convert.ToString(midT);
                                }
                            }while (count <= Stop);
                            its += bl + "Resultado: " + midT + bl;
                            its += bl;
                        }
                        else if (comboBox2.SelectedItem.Equals("Valor no ponto"))
                        {
                            its += input + bl;
                            its += "[a,b] = [" + aT + "," + bT + "]" + bl;

                            do
                            {
                                its += bl + "-> Iteração " + Convert.ToString(count) + bl + bl;
                                count++;

                                rExp = "x=" + aT + ";" + input;
                                a    = float.Parse(rExe(engine, rExp));
                                its += "f(" + aT + ") = " + Convert.ToString(a);
                                if (a < 0)
                                {
                                    its += " < 0" + bl;
                                }
                                else
                                {
                                    its += " > 0" + bl;
                                }

                                rExp = "x=" + bT + ";" + input;
                                b    = float.Parse(rExe(engine, rExp));
                                its += "f(" + bT + ") = " + Convert.ToString(b);
                                if (b < 0)
                                {
                                    its += " < 0" + bl;
                                }
                                else
                                {
                                    its += " > 0" + bl;
                                }

                                midT = (float.Parse(aT) + float.Parse(bT)) / 2;
                                rExp = "x=" + Convert.ToString(midT) + ";" + input;
                                mid  = float.Parse(rExe(engine, rExp));
                                its += "(a+b)/2 = " + Convert.ToString(midT) + bl;
                                its += "f(" + Convert.ToString(midT) + ") = " + Convert.ToString(mid);
                                if (mid < 0)
                                {
                                    its += " < 0" + bl;
                                }
                                else
                                {
                                    its += " > 0" + bl;
                                }

                                its += "Valor no ponto: " + Convert.ToString(mid);

                                if ((mid > 0 && b > 0 && a < 0) || (mid < 0 && b < 0 && a > 0))
                                {
                                    bT = Convert.ToString(midT);
                                }
                                else
                                {
                                    aT = Convert.ToString(midT);
                                }
                            }while (Math.Abs(mid) > Stop);
                            its += bl + "Resultado: " + midT + bl;
                            its += bl;
                        }

                        textBox6.Show();
                        textBox6.Text = its;
                        #region PLOT
                        pictureBox1.Show();
                        rPlot(engine, "curve(" + textBox1.Text + "," + Convert.ToString(midT - (0.5 * midT)) + "," + Convert.ToString(midT + (0.5 * midT)) + ");abline(h=0,col=\"black\");abline(v=0,col=\"black\");grid()", pictureBox1);
                        FileStream stream = new FileStream("rplot.jpg", FileMode.Open, FileAccess.Read);
                        pictureBox1.Image = Image.FromStream(stream);
                        stream.Dispose();
                        File.Delete(@"rplot.jpg");
                        #endregion
                    }
                    catch (Exception d)
                    {
                        MessageBox.Show("Insira todos os valores corretamente");
                    }
                }
                #endregion

                #region POSIÇÃO FALSA
                else if (comboBox1.SelectedItem.Equals("Método da Posição Falsa"))
                {
                    float  x = 0;
                    string rExpA, rExpB, rExpX;
                    int    count = 0;

                    try
                    {
                        input = textBox1.Text;
                        a     = float.Parse(s: textBox2.Text);
                        b     = float.Parse(s: textBox3.Text);
                        if (a > b)
                        {
                            aux = a;
                            a   = b;
                            b   = aux;
                        }
                        Stop = float.Parse(textBox5.Text);

                        if (comboBox2.SelectedItem.Equals("EA"))
                        {
                            its += input + bl;
                            its += "[a,b] = [" + Convert.ToString(a) + "," + Convert.ToString(b) + "]" + bl;

                            do
                            {
                                its += bl + "-> Iteração " + Convert.ToString(count) + bl + bl;
                                count++;
                                rExpA = "x=" + Convert.ToString(a) + ";" + input;
                                rExpB = "x=" + Convert.ToString(b) + ";" + input;
                                its  += "((" + Convert.ToString(a) + " x " + Convert.ToString(Math.Round(float.Parse(rExe(engine, rExpB)), 4)) + ") - (" + Convert.ToString(b) + " x " + Convert.ToString(Math.Round(float.Parse(rExe(engine, rExpA)), 4)) + "))/(" + Convert.ToString(Math.Round(float.Parse(rExe(engine, rExpB)), 4)) + " - " + Convert.ToString(Math.Round(float.Parse(rExe(engine, rExpA)), 4)) + ") =";
                                x     = (a * float.Parse(rExe(engine, rExpB)) - b * float.Parse(rExe(engine, rExpA))) / (float.Parse(rExe(engine, rExpB)) - float.Parse(rExe(engine, rExpA)));
                                its  += Convert.ToString(x) + bl;
                                rExpX = "x=" + Convert.ToString(x) + ";" + input;
                                its  += "[a,b] = [" + Convert.ToString(Math.Round(a, 3)) + " , " + Convert.ToString(Math.Round(b, 3)) + "]" + bl;
                                if (x < b)
                                {
                                    a = x;
                                }
                                else
                                {
                                    a = b;
                                    b = x;
                                }

                                its += "Erro absoluto: " + (Math.Abs(x - valorReal)) + bl;
                            }while ((Math.Abs(x - valorReal)) >= Stop);
                        }

                        else if (comboBox2.SelectedItem.Equals("ER"))
                        {
                            its += input + bl;
                            its += "[a,b] = [" + Convert.ToString(a) + "," + Convert.ToString(b) + "]" + bl;
                            do
                            {
                                its += bl + "-> Iteração " + Convert.ToString(count) + bl + bl;
                                count++;
                                rExpA = "x=" + Convert.ToString(a) + ";" + input;
                                rExpB = "x=" + Convert.ToString(b) + ";" + input;
                                its  += "((" + Convert.ToString(a) + " x " + Convert.ToString(Math.Round(float.Parse(rExe(engine, rExpB)), 4)) + ") - (" + Convert.ToString(b) + " x " + Convert.ToString(Math.Round(float.Parse(rExe(engine, rExpA)), 4)) + "))/(" + Convert.ToString(Math.Round(float.Parse(rExe(engine, rExpB)), 4)) + " - " + Convert.ToString(Math.Round(float.Parse(rExe(engine, rExpA)), 4)) + ") =";
                                x     = (a * float.Parse(rExe(engine, rExpB)) - b * float.Parse(rExe(engine, rExpA))) / (float.Parse(rExe(engine, rExpB)) - float.Parse(rExe(engine, rExpA)));
                                its  += Convert.ToString(x) + bl;
                                rExpX = "x=" + Convert.ToString(x) + ";" + input;
                                its  += "[a,b] = [" + Convert.ToString(Math.Round(a, 3)) + " , " + Convert.ToString(Math.Round(b, 3)) + "]" + bl;
                                if (x < b)
                                {
                                    a = x;
                                }
                                else
                                {
                                    a = b;
                                    b = x;
                                }

                                its += "Erro relativo: " + Convert.ToString((Math.Abs(x - valorReal) / valorReal)) + bl;
                            }while ((Math.Abs(x - valorReal) / valorReal) > Stop);
                        }

                        else if (comboBox2.SelectedItem.Equals("Nº de iterações"))
                        {
                            its += input + bl;
                            its += "[a,b] = [" + Convert.ToString(a) + "," + Convert.ToString(b) + "]" + bl;
                            int i;
                            for (i = 0; i < Stop; i++)
                            {
                                its += bl + "-> Iteração " + Convert.ToString(count) + bl + bl;
                                count++;
                                rExpA = "x=" + Convert.ToString(a) + ";" + input;
                                rExpB = "x=" + Convert.ToString(b) + ";" + input;
                                its  += "((" + Convert.ToString(a) + " x " + Convert.ToString(Math.Round(float.Parse(rExe(engine, rExpB)), 4)) + ") - (" + Convert.ToString(b) + " x " + Convert.ToString(Math.Round(float.Parse(rExe(engine, rExpA)), 4)) + "))/(" + Convert.ToString(Math.Round(float.Parse(rExe(engine, rExpB)), 4)) + " - " + Convert.ToString(Math.Round(float.Parse(rExe(engine, rExpA)), 4)) + ") = ";
                                x     = (a * float.Parse(rExe(engine, rExpB)) - b * float.Parse(rExe(engine, rExpA))) / (float.Parse(rExe(engine, rExpB)) - float.Parse(rExe(engine, rExpA)));
                                its  += Convert.ToString(x) + bl;
                                its  += "[a,b] = [" + Convert.ToString(Math.Round(a, 3)) + " , " + Convert.ToString(Math.Round(b, 3)) + "]" + bl;
                                if (x < b)
                                {
                                    a = x;
                                }
                                else
                                {
                                    a = b;
                                    b = x;
                                }
                            }
                        }

                        else if (comboBox2.SelectedItem.Equals("Valor no ponto"))
                        {
                            its += input + bl;
                            its += "[a,b] = [" + Convert.ToString(a) + "," + Convert.ToString(b) + "]" + bl;
                            do
                            {
                                its += bl + "-> Iteração " + Convert.ToString(count) + bl + bl;
                                count++;
                                rExpA = "x=" + Convert.ToString(a) + ";" + input;
                                rExpB = "x=" + Convert.ToString(b) + ";" + input;
                                its  += "((" + Convert.ToString(a) + " x " + Convert.ToString(Math.Round(float.Parse(rExe(engine, rExpB)), 4)) + ") - (" + Convert.ToString(b) + " x " + Convert.ToString(Math.Round(float.Parse(rExe(engine, rExpA)), 4)) + "))/(" + Convert.ToString(Math.Round(float.Parse(rExe(engine, rExpB)), 4)) + " - " + Convert.ToString(Math.Round(float.Parse(rExe(engine, rExpA)), 4)) + ") =";
                                x     = (a * float.Parse(rExe(engine, rExpB)) - b * float.Parse(rExe(engine, rExpA))) / (float.Parse(rExe(engine, rExpB)) - float.Parse(rExe(engine, rExpA)));
                                its  += Convert.ToString(x) + bl;
                                rExpX = "x=" + Convert.ToString(value: x) + ";" + input;
                                its  += "[a,b] = [" + Convert.ToString(Math.Round(a, 3)) + " , " + Convert.ToString(Math.Round(b, 3)) + "]" + bl;
                                if (x < b)
                                {
                                    a = x;
                                }
                                else
                                {
                                    a = b;
                                    b = x;
                                }

                                its += "f(" + Convert.ToString(x) + ") = " + rExe(engine, rExpX) + bl;
                            }while (Math.Abs(float.Parse(rExe(engine, rExpX))) > Stop);
                        }

                        #region PLOT
                        pictureBox1.Show();
                        rPlot(engine, "curve(" + textBox1.Text + "," + Convert.ToString(x - (0.5 * x)) + "," + Convert.ToString(x + (0.5 * x)) + ");abline(h=0,col=\"black\");abline(v=0,col=\"black\");grid()", pictureBox1);
                        FileStream stream = new FileStream("rplot.jpg", FileMode.Open, FileAccess.Read);
                        pictureBox1.Image = Image.FromStream(stream);
                        stream.Dispose();
                        File.Delete(@"rplot.jpg");
                        #endregion
                        //MessageBox.Show(Convert.ToString(value: x));
                        its += bl + "Resultado: " + Convert.ToString(x);
                        textBox6.Show();
                        textBox6.Text = its;
                    }
                    catch (Exception g)
                    {
                        MessageBox.Show("Insira todos os valores corretamente eu");
                    }
                }
                #endregion

                #region Metodo de Newton
                if (comboBox1.SelectedItem.Equals("Método de Newton-Raphson"))
                {
                    int    count = 1;
                    float  midT = 0, mid = 0;
                    float  f_linha;
                    float  f;
                    string valT;
                    input = textBox1.Text;
                    string aT = textBox2.Text;

                    try
                    {
                        Stop = float.Parse(textBox5.Text);
                        midT = float.Parse(aT);
                        its += input + bl;
                        if (comboBox2.SelectedItem.Equals("EA"))
                        {
                            do
                            {
                                its += bl + "-> Iteração " + Convert.ToString(count) + bl + bl;
                                count++;

                                rExp    = "f = function(x) " + input + ";(f(" + Convert.ToString(midT) + "+0.0000001)-f(" + Convert.ToString(midT) + "))/(0.0000001);";
                                f_linha = float.Parse(rExe(engine, rExp));

                                rExp = "f(" + midT + ");";
                                f    = float.Parse(rExe(engine, rExp));

                                its += "x" + Convert.ToString(count - 2) + " = " + midT + bl;
                                its += "f(x" + Convert.ToString(count - 2) + ") = " + Convert.ToString(f) + bl;
                                its += "f'(x" + Convert.ToString(count - 2) + ") = " + Convert.ToString(f_linha) + bl;
                                its += "x" + Convert.ToString(count - 1) + " = " + Convert.ToString(midT) + " - ( " + Convert.ToString(f) + " / " + Convert.ToString(f_linha) + " )" + bl;
                                midT = midT - (f / f_linha);
                                its += "x" + Convert.ToString(count - 1) + " = " + Convert.ToString(midT) + bl;

                                its += "Erro absoluto: " + Convert.ToString(Math.Abs(valorReal - midT)) + bl;
                            }while (Math.Abs(valorReal - midT) > Stop);
                            its += bl + "Resultado: " + midT + bl;
                            its += bl;
                        }
                        else if (comboBox2.SelectedItem.Equals("ER"))
                        {
                            do
                            {
                                its += bl + "-> Iteração " + Convert.ToString(count) + bl + bl;
                                count++;

                                rExp    = "f = function(x) " + input + ";(f(" + Convert.ToString(midT) + "+0.0000001)-f(" + Convert.ToString(midT) + "))/(0.0000001);";
                                f_linha = float.Parse(rExe(engine, rExp));

                                rExp = "f(" + midT + ");";
                                f    = float.Parse(rExe(engine, rExp));



                                its += "x" + Convert.ToString(count - 2) + " = " + midT + bl;
                                its += "f(x" + Convert.ToString(count - 2) + ") = " + Convert.ToString(f) + bl;
                                its += "f'(x" + Convert.ToString(count - 2) + ") = " + Convert.ToString(f_linha) + bl;
                                its += "x" + Convert.ToString(count - 1) + " = " + Convert.ToString(midT) + " - ( " + Convert.ToString(f) + " / " + Convert.ToString(f_linha) + " )" + bl;
                                midT = midT - (f / f_linha);
                                its += "x" + Convert.ToString(count - 1) + " = " + Convert.ToString(midT) + bl;

                                its += "Erro relativo: " + Convert.ToString((Math.Abs(valorReal - midT)) / valorReal) + bl;
                            }while ((Math.Abs(valorReal - midT) / valorReal) > Stop);
                            its += bl + "Resultado: " + midT + bl;
                            its += bl;
                        }
                        else if (comboBox2.SelectedItem.Equals("Nº de iterações"))
                        {
                            do
                            {
                                its += bl + "-> Iteração " + Convert.ToString(count) + bl + bl;
                                count++;

                                rExp    = "f = function(x) " + input + ";(f(" + Convert.ToString(midT) + "+0.0000001)-f(" + Convert.ToString(midT) + "))/(0.0000001);";
                                f_linha = float.Parse(rExe(engine, rExp));

                                rExp = "f(" + midT + ");";
                                f    = float.Parse(rExe(engine, rExp));

                                its += "x" + Convert.ToString(count - 2) + " = " + midT + bl;
                                its += "f(x" + Convert.ToString(count - 2) + ") = " + Convert.ToString(f) + bl;
                                its += "f'(x" + Convert.ToString(count - 2) + ") = " + Convert.ToString(f_linha) + bl;
                                its += "x" + Convert.ToString(count - 1) + " = " + Convert.ToString(midT) + " - ( " + Convert.ToString(f) + " / " + Convert.ToString(f_linha) + " )" + bl;
                                midT = midT - (f / f_linha);
                                its += "x" + Convert.ToString(count - 1) + " = " + Convert.ToString(midT) + bl;
                            }while (count <= Stop);
                            its += bl + "Resultado: " + midT + bl;
                            its += bl;
                        }
                        else if (comboBox2.SelectedItem.Equals("Valor no ponto"))
                        {
                            do
                            {
                                its += bl + "-> Iteração " + Convert.ToString(count) + bl + bl;
                                count++;

                                rExp    = "f = function(x) " + input + ";(f(" + Convert.ToString(midT) + "+0.0000001)-f(" + Convert.ToString(midT) + "))/(0.0000001);";
                                f_linha = float.Parse(rExe(engine, rExp));

                                rExp = "f(" + midT + ");";
                                f    = float.Parse(rExe(engine, rExp));
                                its += "x" + Convert.ToString(count - 2) + " = " + midT + bl;
                                its += "f(x" + Convert.ToString(count - 2) + ") = " + Convert.ToString(f) + bl;
                                its += "f'(x" + Convert.ToString(count - 2) + ") = " + Convert.ToString(f_linha) + bl;

                                its += "x" + Convert.ToString(count - 1) + " = " + Convert.ToString(midT) + " - ( " + Convert.ToString(f) + " / " + Convert.ToString(f_linha) + " )" + bl;
                                midT = midT - (f / f_linha);

                                its += "x" + Convert.ToString(count - 1) + " = " + Convert.ToString(midT) + bl;

                                rExp = "x=" + Convert.ToString(midT) + ";" + input;
                                mid  = float.Parse(rExe(engine, rExp));

                                its += "Valor no ponto: " + Convert.ToString(mid) + bl;
                            }while (Math.Abs(mid) > Stop);
                            its += bl + "Resultado: " + midT + bl;
                            its += bl;
                        }

                        textBox6.Show();
                        textBox6.Text = its;
                        #region PLOT
                        pictureBox1.Show();
                        rPlot(engine, "curve(" + textBox1.Text + "," + Convert.ToString(midT - (0.5 * midT)) + "," + Convert.ToString(midT + (0.5 * midT)) + ");abline(h=0,col=\"black\");abline(v=0,col=\"black\");grid()", pictureBox1);
                        FileStream stream = new FileStream("rplot.jpg", FileMode.Open, FileAccess.Read);
                        pictureBox1.Image = Image.FromStream(stream);
                        stream.Dispose();
                        File.Delete(@"rplot.jpg");
                        #endregion */
                    }
                    catch (Exception d)
                    {
                        MessageBox.Show("Insira todos os valores corretamente");
                    }
                }
                #endregion

                #region Metodo de Halley
                if (comboBox1.SelectedItem.Equals("Método de Halley"))
                {
                    int    count = 1;
                    float  midT = 0, mid = 0;
                    float  f_2linha;
                    float  f_linha;
                    float  f;
                    string valT;
                    input = textBox1.Text;
                    string aT = textBox2.Text;

                    try
                    {
                        Stop = float.Parse(textBox5.Text);
                        midT = float.Parse(aT);
                        its += input + bl;
                        if (comboBox2.SelectedItem.Equals("EA"))
                        {
                            do
                            {
                                its += bl + "-> Iteração " + Convert.ToString(count) + bl + bl;
                                count++;

                                rExp     = "f = function(x) " + input + ";(f(" + Convert.ToString(midT) + "+0.0000001)-2*f(" + Convert.ToString(midT) + ")+f(" + Convert.ToString(midT) + "-0.0000001))/(0.0000001^2);";
                                f_2linha = float.Parse(rExe(engine, rExp));

                                rExp    = "f = function(x) " + input + ";(f(" + Convert.ToString(midT) + "+0.0000001)-f(" + Convert.ToString(midT) + "))/(0.0000001);";
                                f_linha = float.Parse(rExe(engine, rExp));

                                rExp = "f(" + midT + ");";
                                f    = float.Parse(rExe(engine, rExp));

                                its += "x" + Convert.ToString(count - 2) + " = " + midT + bl;
                                its += "f(x" + Convert.ToString(count - 2) + ") = " + Convert.ToString(f) + bl;
                                its += "f'(x" + Convert.ToString(count - 2) + ") = " + Convert.ToString(f_linha) + bl;

                                its += "f''(x" + Convert.ToString(count - 2) + ") = " + Convert.ToString(f_2linha) + bl;

                                its += "x" + Convert.ToString(count - 1) + " = " + Convert.ToString(midT) + " - ( (2* " + Convert.ToString(f) + "* " + Convert.ToString(f_linha) + ") / ( 2*(" + Convert.ToString(f_linha) + ")^2 - (" + Convert.ToString(f) + "* " + Convert.ToString(f_2linha) + ") ) )" + bl;

                                midT = midT - ((2 * f * f_linha) / (2 * (f_linha * f_linha) - f * f_2linha));
                                its += "x" + Convert.ToString(count - 1) + " = " + Convert.ToString(midT) + bl;

                                its += "Erro absoluto: " + Convert.ToString(Math.Abs(valorReal - midT)) + bl;
                            }while (Math.Abs(valorReal - midT) > Stop);
                            its += bl + "Resultado: " + midT + bl;
                            its += bl;
                        }
                        else if (comboBox2.SelectedItem.Equals("ER"))
                        {
                            do
                            {
                                its += bl + "-> Iteração " + Convert.ToString(count) + bl + bl;
                                count++;
                                rExp     = "f = function(x) " + input + ";(f(" + Convert.ToString(midT) + "+0.0000001)-2*f(" + Convert.ToString(midT) + ")+f(" + Convert.ToString(midT) + "-0.0000001))/(0.0000001^2);";
                                f_2linha = float.Parse(rExe(engine, rExp));

                                rExp    = "f = function(x) " + input + ";(f(" + Convert.ToString(midT) + "+0.0000001)-f(" + Convert.ToString(midT) + "))/(0.0000001);";
                                f_linha = float.Parse(rExe(engine, rExp));

                                rExp = "f(" + midT + ");";
                                f    = float.Parse(rExe(engine, rExp));

                                its += "x" + Convert.ToString(count - 2) + " = " + midT + bl;
                                its += "f(x" + Convert.ToString(count - 2) + ") = " + Convert.ToString(f) + bl;
                                its += "f'(x" + Convert.ToString(count - 2) + ") = " + Convert.ToString(f_linha) + bl;

                                its += "f''(x" + Convert.ToString(count - 2) + ") = " + Convert.ToString(f_2linha) + bl;

                                its += "x" + Convert.ToString(count - 1) + " = " + Convert.ToString(midT) + " - ( (2* " + Convert.ToString(f) + "* " + Convert.ToString(f_linha) + ") / ( 2*(" + Convert.ToString(f_linha) + ")^2 - (" + Convert.ToString(f) + "* " + Convert.ToString(f_2linha) + ") ) )" + bl;

                                midT = midT - ((2 * f * f_linha) / (2 * (f_linha * f_linha) - f * f_2linha));
                                its += "x" + Convert.ToString(count - 1) + " = " + Convert.ToString(midT) + bl;

                                its += "Erro relativo: " + Convert.ToString((Math.Abs(valorReal - midT)) / valorReal) + bl;
                            }while ((Math.Abs(valorReal - midT) / valorReal) > Stop);
                            its += bl + "Resultado: " + midT + bl;
                            its += bl;
                        }
                        else if (comboBox2.SelectedItem.Equals("Nº de iterações"))
                        {
                            do
                            {
                                its += bl + "-> Iteração " + Convert.ToString(count) + bl + bl;
                                count++;
                                rExp     = "f = function(x) " + input + ";(f(" + Convert.ToString(midT) + "+0.0000001)-2*f(" + Convert.ToString(midT) + ")+f(" + Convert.ToString(midT) + "-0.0000001))/(0.0000001^2);";
                                f_2linha = float.Parse(rExe(engine, rExp));

                                rExp    = "f = function(x) " + input + ";(f(" + Convert.ToString(midT) + "+0.0000001)-f(" + Convert.ToString(midT) + "))/(0.0000001);";
                                f_linha = float.Parse(rExe(engine, rExp));

                                rExp = "f(" + midT + ");";
                                f    = float.Parse(rExe(engine, rExp));

                                its += "x" + Convert.ToString(count - 2) + " = " + midT + bl;
                                its += "f(x" + Convert.ToString(count - 2) + ") = " + Convert.ToString(f) + bl;
                                its += "f'(x" + Convert.ToString(count - 2) + ") = " + Convert.ToString(f_linha) + bl;

                                its += "f''(x" + Convert.ToString(count - 2) + ") = " + Convert.ToString(f_2linha) + bl;

                                its += "x" + Convert.ToString(count - 1) + " = " + Convert.ToString(midT) + " - ( (2* " + Convert.ToString(f) + "* " + Convert.ToString(f_linha) + ") / ( 2*(" + Convert.ToString(f_linha) + ")^2 - (" + Convert.ToString(f) + "* " + Convert.ToString(f_2linha) + ") ) )" + bl;

                                midT = midT - ((2 * f * f_linha) / (2 * (f_linha * f_linha) - f * f_2linha));
                                its += "x" + Convert.ToString(count - 1) + " = " + Convert.ToString(midT) + bl;
                            }while (count <= Stop);
                            its += bl + "Resultado: " + midT + bl;
                            its += bl;
                        }
                        else if (comboBox2.SelectedItem.Equals("Valor no ponto"))
                        {
                            do
                            {
                                its += bl + "-> Iteração " + Convert.ToString(count) + bl + bl;
                                count++;

                                rExp     = "f = function(x) " + input + ";(f(" + Convert.ToString(midT) + "+0.0000001)-2*f(" + Convert.ToString(midT) + ")+f(" + Convert.ToString(midT) + "-0.0000001))/(0.0000001^2);";
                                f_2linha = float.Parse(rExe(engine, rExp));

                                rExp    = "f = function(x) " + input + ";(f(" + Convert.ToString(midT) + "+0.0000001)-f(" + Convert.ToString(midT) + "))/(0.0000001);";
                                f_linha = float.Parse(rExe(engine, rExp));

                                rExp = "f(" + midT + ");";
                                f    = float.Parse(rExe(engine, rExp));

                                its += "x" + Convert.ToString(count - 2) + " = " + midT + bl;
                                its += "f(x" + Convert.ToString(count - 2) + ") = " + Convert.ToString(f) + bl;
                                its += "f'(x" + Convert.ToString(count - 2) + ") = " + Convert.ToString(f_linha) + bl;

                                its += "f''(x" + Convert.ToString(count - 2) + ") = " + Convert.ToString(f_2linha) + bl;

                                its += "x" + Convert.ToString(count - 1) + " = " + Convert.ToString(midT) + " - ( (2* " + Convert.ToString(f) + "* " + Convert.ToString(f_linha) + ") / ( 2*(" + Convert.ToString(f_linha) + ")^2 - (" + Convert.ToString(f) + "* " + Convert.ToString(f_2linha) + ") ) )" + bl;

                                midT = midT - ((2 * f * f_linha) / (2 * (f_linha * f_linha) - f * f_2linha));
                                its += "x" + Convert.ToString(count - 1) + " = " + Convert.ToString(midT) + bl;

                                rExp = "x=" + Convert.ToString(midT) + ";" + input;
                                mid  = float.Parse(rExe(engine, rExp));

                                its += "Valor no ponto: " + Convert.ToString(mid) + bl;
                            }while (Math.Abs(mid) > Stop);
                            its += bl + "Resultado: " + midT + bl;
                            its += bl;
                        }

                        textBox6.Show();
                        textBox6.Text = its;
                        #region PLOT
                        pictureBox1.Show();
                        rPlot(engine, "curve(" + textBox1.Text + "," + Convert.ToString(midT - (0.5 * midT)) + "," + Convert.ToString(midT + (0.5 * midT)) + ");abline(h=0,col=\"black\");abline(v=0,col=\"black\");grid()", pictureBox1);
                        FileStream stream = new FileStream("rplot.jpg", FileMode.Open, FileAccess.Read);
                        pictureBox1.Image = Image.FromStream(stream);
                        stream.Dispose();
                        File.Delete(@"rplot.jpg");
                        #endregion */
                    }
                    catch (Exception d)
                    {
                        MessageBox.Show("Insira todos os valores corretamente");
                    }
                }

                #endregion


                /*
                 * if (textBox1.Text.Contains("curve") || textBox1.Text.Contains("plot"))
                 * {
                 *  //PLOT SECTION
                 #region PLOT
                 *  pictureBox1.Show();
                 *  rPlot(engine, textBox1.Text, pictureBox1);
                 *  FileStream stream = new FileStream("rplot.jpg", FileMode.Open, FileAccess.Read);
                 *  pictureBox1.Image = Image.FromStream(stream);
                 *  stream.Dispose();
                 *  File.Delete(@"rplot.jpg");
                 #endregion
                 * }
                 * else
                 * {
                 *
                 * }*/
                /*
                 * else if(!textBox1.Text.Equals(""))
                 * {
                 *  if(!rExe(engine, textBox1.Text).Equals(""))
                 *  {
                 *      MessageBox.Show(rExe(engine, textBox1.Text));
                 *  }
                 * }*/
            }
            else if (comboBox2.SelectedItem.Equals("ER") && float.Parse(textBox4.Text) == 0)
            {
                MessageBox.Show("Para erros relativos, insira um \"Valor Real\" diferente de 0");
            }

            engine.ClearGlobalEnvironment();
        }
예제 #4
0
파일: RClass.cs 프로젝트: waiterlyq/BaseLib
 public void Dispose()
 {
     engine.ClearGlobalEnvironment();
 }