In my program I’ve defined some variables in “button1_Click” and also there is a “for” loop in “button1_Click” function. In that loop I want those variables to be changed, but they don’t.
What should I be doing…passing them by reference? If yes, how?
private void button1_Click(object sender, EventArgs e)
{
double t=0;
double x = double.Parse(X0.Text);
double z = double.Parse(Z0.Text);
double y = double.Parse(Y0.Text);
double u = double.Parse(U0.Text);
double tn = double.Parse(Tn.Text);
double h = double.Parse(textbox_h.Text);
for (int i = 0; i < (tn / h); i++)
{
double K1x = h * fx(t, x, y, z, u);
double K2x = h * fx(t + h / 2, x + K1x / 2, y + K1y / 2, z + K1z / 2, u + K1u / 2);
double K3x = h * fx(t + h / 2, x + K2x / 2, y + K2y / 2, z + K2z / 2, u + K2u / 2);
double K4x = h * fx(t + h, x + K3x, y + K3y, z + K3z, u + K3u);
x =x+ (1 / 6)*(K1x + 2 * K2x + 2 * K3x + K4x);
richTextBox1.Text += "X(" + (h * (i + 1)).ToString() + ")=" + x.ToString();
}
All things are right but at the last line, new values of x doesn’t go in x, and the old ones remain.
Note that fx, fz,fy,fu are functions that I’ve defined before.
The problem is that
1 / 6is0, because both1and6areint, so the result will also be anint. Anintcan’t have any decimals, so they are simply dropped, there is no rounding happening. And0.16666666...without the decimals is0.To fix it, make at least one of the two a double or float:
The complete line should now look like this: