Example #1
0
        private void button3_Click(object sender, EventArgs e)
        {
            kalimat = textBox6.Text;
            bool input_sah = true;

            try
            {
                manipulasi_turunan();
            }
            catch { input_sah = false; }
            label9.Visible = false;
            board2.Rows.Clear();
            label7.Visible = true;
            label7.Text    = "f'(x) = " + turunan;

            if (!validasi_x(textBox5.Text))
            {
                label9.Visible = true;
            }

            if (validasi_x(textBox5.Text) && input_sah)
            {
                fungsi f = new fungsi();
                f.kalimat = kalimat;
                f.manipulasi();

                fungsi t = new fungsi();
                t.kalimat = turunan;
                t.manipulasi();

                decimal x0 = Convert.ToDecimal(textBox5.Text), xi;
                board2.Rows.Add();
                board2[0, 0].Value = 0;
                board2[1, 0].Value = x0;
                int i = 1;
                while (i < 200)
                {
                    xi = x0 - (decimal)(f.fx((double)x0) / t.fx((double)x0));
                    decimal selisih = Math.Abs(xi - x0);
                    x0 = xi;

                    board2.Rows.Add();
                    board2[0, i].Value = i.ToString();
                    board2[1, i].Value = xi.ToString();
                    board2[2, i].Value = selisih.ToString();

                    if (selisih == 0.0m)
                    {
                        board2[1, i - 1].Style.BackColor = Color.Yellow;
                        board2[1, i].Style.BackColor     = Color.Yellow;
                        board2[2, i].Style.ForeColor     = Color.Red;
                        break;
                    }
                    i++;
                }
                textBox4.Text = Math.Round(Convert.ToDecimal(board2[1, i - 1].Value), 15).ToString();
            }
        }
Example #2
0
        private void button4_Click(object sender, EventArgs e)
        {
            kalimat         = textBox7.Text;
            label14.Visible = false; label16.Visible = false;
            dataGridView1.Rows.Clear();
            if (!validasi_x(textBox8.Text))
            {
                label14.Visible = true;
            }
            if (!validasi_x(textBox9.Text))
            {
                label16.Visible = true;
            }
            bool   input_sah = true;
            fungsi f         = new fungsi();

            try
            {
                f.kalimat = kalimat;
                f.manipulasi();
            }
            catch { input_sah = false; }

            if (input_sah && validasi_x(textBox8.Text) && validasi_x(textBox9.Text))
            {
                decimal x0 = Convert.ToDecimal(textBox8.Text);
                decimal x1 = Convert.ToDecimal(textBox9.Text);
                decimal xi = 0;
                dataGridView1.Rows.Add();
                dataGridView1[0, 0].Value = 0;
                dataGridView1[1, 0].Value = x0;
                dataGridView1.Rows.Add();
                dataGridView1[0, 1].Value = 1;
                dataGridView1[1, 1].Value = x1;
                dataGridView1[2, 1].Value = (x1 - x0).ToString();
                bool berhenti = false;

                int i = 2;
                while (i <= 200)
                {
                    if (Math.Abs((f.fx((double)x1) - f.fx((double)x0))) == 0.000000000000)
                    {
                        berhenti = true; break;
                    }
                    try
                    {
                        xi = x1 - (((decimal)f.fx((double)x1) * (x1 - x0)) / (decimal)(f.fx((double)x1) - f.fx((double)x0)));
                    }
                    catch { berhenti = true; break; }
                    decimal selisih = Math.Abs(xi - x1);
                    x0 = x1;
                    x1 = xi;

                    dataGridView1.Rows.Add();
                    dataGridView1[0, i].Value = i.ToString();
                    dataGridView1[1, i].Value = xi.ToString();
                    dataGridView1[2, i].Value = selisih.ToString();

                    if (selisih == 0.0m)
                    {
                        dataGridView1[1, i - 1].Style.BackColor = Color.Yellow;
                        dataGridView1[1, i].Style.BackColor     = Color.Yellow;
                        dataGridView1[2, i].Style.ForeColor     = Color.Red;
                        break;
                    }
                    i++;
                }
                string s;
                if (berhenti)
                {
                    s = "Divided by Zero";
                }
                else if (i > 200)
                {
                    s = "Divergen";
                }
                else
                {
                    s = Math.Round(xi, 10).ToString();
                }
                textBox10.Text = s;
            }
        }