我的程序无限循环,我需要最好的解决方案。
我所拥有的功能实际上是彼此相关的,所以看起来很复杂。
其中dt = 0.04,dx = 1,a = 0.7,b = 0.4
问题是搜索p(x,t + 1)需要p(x,t),p(x-1,t)和p(x + 1,t) 它也发生在其他功能上。
public partial class Form1 : Form
{
double a,b,dt,dx;
public void JudulKolom() //to print in table, cause I planned to make p,q,n graphic using zedGraph
{
listView1.Columns.Add("t").Width =100;
listView1.Columns.Add("x").Width =100;
listView1.Columns.Add("p(x,t)").Width =100;
listView1.Columns.Add("q(x,t)").Width =100;
listView1.Columns.Add("n(x,t)").Width =100;
listView1.Columns.Add("r(x,t)").Width =100;
listView1.Columns.Add("c(x,t)").Width =100;
listView1.Columns.Add("f(c)").Width =100;
listView1.Columns.Add("g(c)").Width =100;
listView1.Columns.Add("h(c)").Width =100;
}
//fungsi p(x,t)
double fP (int x, double t)
{
double hasil = 0;
a = Convert.ToDouble(textBoxAlfa.Text);
if (t == 0) //for every t=0 and any x
{
hasil = 0.01 * Math.Exp(-0.1 * x);
}
else //for t=0.04 or t+1
{
if (x > 0) //for p when x=1..10 and t=0.04
{
hasil = fP(x,t-dt) + fD() * (fP(x+1,t-dt) - 2 * fP(x,t-dt) + fP(x-1,t-dt)) +
dt * fG(x,t-dt) * (1 - fR(x,t-dt)) - fF(x,t-dt) * fP(x,t-dt) + fP(x,t-dt) * Math.Sqrt(dt) * wiener(x);
}
else if (x == 0) //only for p when x=0 and t=0.04
{
hasil = fP(x,t-dt) + fD() * (fP(x+1,t-dt) - 2 * fP(x,t-dt) + fP(x,t-dt)) +
dt * fG(x,t-dt) * (1 - fR(x,t-dt)) - fF(x,t-dt) * fP(x,t-dt) + fP(x,t-dt) * Math.Sqrt(dt) * wiener(x);
}
}
return Math.Round(hasil, 6);
}
//fungsi q(x,t)
double fQ (int x, double t)
{
double hasil = 0;
if (t == 0) //for every q when t=0 and any x
{
hasil = 0;
}
else
{
if (x > 0) //for q when x=1..10 and t=0.04
{
hasil = fQ(x,t-dt) + fD() * (fQ(x+1,t-dt) - 2*fQ(x,t-dt) + fQ(x-1,t-dt)) +
dt * fF(x,t-dt) * fP(x,t-dt) -
(0.5 * fF(x,t-dt)) * fQ(x,t-dt);
}
else if (x == 0) //only for q when x=0 and t=0.04
{
hasil = fQ(x,t-dt) + fD() * (fQ(x+1,t-dt) - 2*fQ(x,t-dt) + fQ(x,t-dt)) +
dt * fF(x,t-dt) * fP(x,t-dt) -
(0.5 * fF(x,t-dt)) * fQ(x,t-dt);
}
}
return Math.Round(hasil, 6);
}
//fungsi n(x,t)
double fN (int x, double t)
{
double hasil=0;
if (t == 0) //for every n when t=0 and any x
{
hasil = 0;
}
else
{
if (x > 0) //only for n when x=1..10 and t=0.04
{
hasil = fN(x,t-dt) + dt * (0.5 * fF(x,t-dt)) * fQ(x,t-dt);
}
else if (x == 0) //only for n when x=0 and t=0.04
{
hasil = fN(x,t-dt) + dt * (0.5 * fF(x,t-dt)) * fQ(x,t-dt);
}
}
return Math.Round(hasil, 6);
}
//fungsi r(x,t)
double fR (int x, double t)
{
double hasil = 0;
hasil = fP(x,t) + fQ(x,t) + fN(x,t); //for every any x and t
return Math.Round(hasil, 6);
}
//fungsi c(x,t)
double fC (int x, double t)
{
double hasil = 0;
a = Convert.ToDouble(textBoxAlfa.Text);
if (t == 0) //for every C when t=0 and any x
{
hasil = ((1.0 * 10) / (10 + fP(x,t)))*(1.0 - a * fR(x,t)); //the matter is the R function that I have to use fR value in fC(c,t=0) when I look for fC(x,t=0.04)
}
else if (t != 0) //for every C when t is not 0 and any x
{
hasil = ((1.0 * 10) / (10 + fP(x,t-dt)))*(1.0 - a * fR(x,t-dt));
}
return Math.Round(hasil, 6);
}
//fungsi f(c)
double fF (int x, double t)
{
double hasil = 0;
if (t == 0) //for every F when t=0 and any x
{
hasil = 0.5 * (1-Math.Tanh(4 * fC(x,t) - 2));
}
else if (t != 0) //for every F when t is not 0 and any x
{
hasil = 0.5 * (1-Math.Tanh(4 * fC(x,t-dt) - 2));
}
return Math.Round(hasil, 6);
}
//fungsi g(c)
double fG (int x, double t)
{
double hasil = 0;
b = Convert.ToDouble(textBoxBeta.Text);
if (t == 0) //for every G when t=0 and any x
{
hasil = b * Math.Exp(b * fC(x,t));
}
else if (t != 0) //for every G when t is not 0 and any x
{
hasil = b * Math.Exp(b * fC(x,t-dt));
}
return Math.Round(hasil, 6);
}
//fungsi h(c)
double fH (double f) //only for q when x=0 and t=0.04
{
double hasil = 0;
hasil = 0.5 * f;
return Math.Round(hasil, 6);
}
double fD ()
{
double hasil = 0;
hasil = 1.0 * dt / (dx * dx);
return Math.Round(hasil, 6);
}
double wiener (int x)
{
double hasil = 0, dt = 0.04, Pi = 3.14159265358979;
hasil = 1.0 / Math.Sqrt(2 * Pi * dt) * Math.Exp(-1.0 * Math.Pow(x,2) / (2 * dt));
return Math.Round(hasil, 6);
}
private void button1_Click(object sender, EventArgs e)
{
if (textBoxAlfa.Text == "" || textBoxBeta.Text == "" || tbXvalue.Text == "" || tbTvalue.Text == "")
{
MessageBox.Show("Semua input harus diisi angka!");
}
else
{
double p=0,q=0,n=0;
double r,a,c,f,g,h,t;
int x;
a = Convert.ToDouble(textBoxAlfa.Text);
b = Convert.ToDouble(textBoxBeta.Text);
t = Convert.ToDouble(tbTvalue.Text);
x = Convert.ToInt32(tbXvalue.Text);
dt=0.04;
dx=1;
int indeks=-1;
while (t<=0.08)
{
x = 0;
for(int i=0; i<=10; i++)
{
listView1.Items.Add(t.ToString());
indeks=indeks+1;
p=fP(x,t);
q=fQ(x,t);
n=fN(x,t);
r=fR(x,t);
c=fC(x,t);
f=fF(x,t);
g=fG(x,t);
h=fH(f);
listView1.Items[indeks].SubItems.Add(x.ToString());
listView1.Items[indeks].SubItems.Add(p.ToString());
listView1.Items[indeks].SubItems.Add(q.ToString());
listView1.Items[indeks].SubItems.Add(n.ToString());
listView1.Items[indeks].SubItems.Add(r.ToString());
listView1.Items[indeks].SubItems.Add(c.ToString());
listView1.Items[indeks].SubItems.Add(f.ToString());
listView1.Items[indeks].SubItems.Add(g.ToString());
listView1.Items[indeks].SubItems.Add(h.ToString());
}
t=t+0.04;
JudulKolom();
}
}
}
答案 0 :(得分:0)
你不应该通过等同来比较浮点数。你需要允许一些不精确。类似if (Math.Abs(a - b) < epsilon) {}
之类的内容将您的结算if (t == 0)
更改为if (Math.Abs(t) < 0.00001)
或将某些允许的小号更改为dt
。
我认为,公式中的if(!$config['readonly'])
{
$db->query('UPDATE `someTable` SET `whatever` = "whocares"');
}
是为此定义的。