程序
#include <iostream>
#include <cmath>
using namespace std;
double epsilon = 1E-15;
double a, b, c;
double f(double x)
{
return a * x * x + b * x + c;
}
double derivative(double x)
{
return (2 * a * x + b);
}
double calculation_once(double& x)
{
x -= f(x) / derivative(x);
return f(x);
}
int main()
{
cout << "This is a programme using Newton's method to solve formulas." << endl;
cout << "Input a,b,c for a*x^2 + b*x + c = 0." << endl;
cout << "a = ";
cin >> a;
cout << "b = ";
cin >> b;
cout << "c = ";
cin >> c;
cout << "------------------------------" << endl;
if (a == 0)
{
if (b == 0)
{
if (c == 0)
cout << "Infinite solutions!" << endl;
else cout << "No solution!" << endl;
}
else cout << "x = " << -c / b << endl;
}
else // a != 0
{
double difference;
double x = 1 - b / (2 * a); // the right side
do
{
difference = calculation_once(x);
if (x < -b / (2 * a)) // no real solution
{
// x = m + ni
double m = -b / (2 * a);
double n = sqrt(f(m) / a);
cout << "x1 = " << m << "-" << n << "i , x2 = " << m << "+" << n << "i" << endl;
return 0;
}
else if (x == -b / (2 * a))
{
if (fabs(difference) < epsilon) cout << "x = " << -b / (2 * a) << endl;
else cout << "x = " << x << endl;
return 0;
}
} while (fabs(difference) > epsilon);
if (x + b / (2 * a) <= sqrt(epsilon)) cout << "x = " << ((-b / (2 * a) == -0) ? 0 : -b / (2 * a)) << endl;
else cout << "x1 = " << -b / a - x << " , x2 = " << x << endl;
}
return 0;
}
输出示例
分析
- 注意无实根情况的判定,且注意和恰好相切情况的处理。比如68行的
sqrt(epsilon)
可以想想为什么要加根号。 - 一开始第3张输出图片输出
-0
,经过调整,使-0
变为0
。关于-0和0详见我的博客 关于 C++中 -0和0的思考(包括变量内存的查看)。
ALL RIGHTS RESERVED © 2020 Teddy van Jerry
欢迎转载,转载请注明出处。