Demo entry 6627589

bisection

   

Submitted by anonymous on Jun 29, 2017 at 11:08
Language: C++. Code size: 358 Bytes.

double bisection(double a, double b, double error)
{
	steps++;
	auto p = (a + b) / 2;
	//输出每一次的a,f(a)和b,f(b)
	cout << "a = " << a << ",     f(a) = " << f1(a) << endl
		<< "b = " << b << ",    f(b) = " << f1(b) << endl << endl;
	if (b - a < error)
		return p;
	if (f1(a)*f1(p) < 0)
		b = p;
	else
		a = p;
	return bisection(a, b, error);//递归计算
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).