-
Notifications
You must be signed in to change notification settings - Fork 0
/
Secant.cs
179 lines (150 loc) · 5.26 KB
/
Secant.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using MathNet.Symbolics;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Z.Expressions;
namespace Numerics_Project
{
public partial class Secant : Form
{
double Xold = 0;
double xi = 0;
double error = 0;
int counter = 0;
double xnew = 0;
public Secant()
{
InitializeComponent();
}
private void btn_Exit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private double f(double x)
{
string equationString = txt_fx.Text;
SymbolicExpression equation = SymbolicExpression.Parse(equationString);
Dictionary<string, FloatingPoint> variables = new Dictionary<string, FloatingPoint> { { "x", x } };
double result = equation.Evaluate(variables).RealValue;
return result;
}
private void btn_back_Click(object sender, EventArgs e)
{
Home h = new Home();
h.Show();
this.Hide();
}
private void btn_clear_Click(object sender, EventArgs e)
{
txt_error.Text = "";
txt_Ferror.Text = "";
txt_fx.Text = "";
txt_root.Text = "";
txt_x0.Text = "";
txt_xold.Text = "";
dataGridView1.Rows.Clear();
}
private void btn_sum_Click(object sender, EventArgs e)
{
double xol = Convert.ToDouble(txt_xold.Text);
double x0 = Convert.ToDouble(txt_x0.Text);
Secan(xol , x0);
}
void Secan (double xol , double x0 )
{
if (counter == 0)
{
Xold = xol;
xi = x0;
dataGridView1.Rows.Add(counter.ToString(), Xold.ToString(), Math.Round(f(Xold),4).ToString() , xi.ToString() , Math.Round(f(xi),4).ToString() , "---" );
counter++; Secan(Xold , xi );
}
else
{
Xold = xol;
xi = x0;
xnew = xi - ((f(xi) * (Xold - xi)) / (f(Xold) - f(xi)));
error = Math.Abs((xnew - xi) / xnew) * 100;
dataGridView1.Rows.Add(counter.ToString(),Math.Round(xi,4).ToString(), Math.Round(f(xi),4).ToString(), Math.Round(xnew, 4).ToString(), Math.Round(f(xnew),4).ToString(), Math.Round(error, 4).ToString());
counter++;
double Err = Convert.ToDouble(txt_error.Text);
if (error > Err)
Secan(xi , xnew);
}
txt_root.Text = Math.Round(xnew,4).ToString();
txt_Ferror.Text = Math.Round(error,4).ToString();
}
private void txt_fx_TextChanged(object sender, EventArgs e)
{
string equationString = txt_fx.Text;
}
private void enter_focus(object sender, EventArgs e)
{
if (txt_fx.Text == "please enter the function")
txt_fx.Text = "";
}
private void enter_not_focus(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txt_fx.Text))
txt_fx.Text = "please enter the function";
}
private void panel2_Paint(object sender, PaintEventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void txt_error_TextChanged(object sender, EventArgs e)
{
}
private void enterERROR_focus(object sender, EventArgs e)
{
if (txt_error.Text == "please enter the ERROR")
txt_error.Text = "";
}
private void enter_notERROR_focus(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txt_error.Text))
txt_error.Text = "please enter the ERROR";
}
private void txt_xold_TextChanged(object sender, EventArgs e)
{
}
private void enterxu_focus(object sender, EventArgs e)
{
if (txt_xold.Text == "Enter the X(i-1) value")
txt_xold.Text = "";
}
private void enter_notxu_focus(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txt_xold.Text))
txt_xold.Text = "Enter the X(i-1) value";
}
private void label3_Click(object sender, EventArgs e)
{
}
private void txt_x0_TextChanged(object sender, EventArgs e)
{
}
private void enterx0_focus(object sender, EventArgs e)
{
if (txt_x0.Text == "Enter the x0 value")
txt_x0.Text = "";
}
private void enter_notx0_focus(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txt_x0.Text))
txt_x0.Text = "Enter the x0 value";
}
private void button1_Click(object sender, EventArgs e)
{
Home h = new Home();
h.Show();
this.Hide();
}
}
}