#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define eps 1e-2
/*
* Created by HarvestWu on 2018/07/10.
*/
using namespace std;
//原方程
double f(double x)
{
return x*x*x + x - 1;
}
//导函数
double f1(double x)
{
return 3*x*x + 1;
}
//平分法
double binary(double a, double b)
{
double x;
do
{
x = (a + b) / 2;
if (f(a)*f(x) > 0)
a = x;
else b = x;
} while (fabs(f(x)) > eps);
return x;
}
//试位法
double false_pos(double a, double b)
{
double x;
do
{
x = (a*f(b) - b*f(a)) / (f(b) - f(a));
if (f(x)*f(a) > 0)
a = x;
else b = x;
} while (fabs(f(x))>eps);
return x;
}
//牛顿法
double newton(double x)
{
do
{
x = x - f(x) / f1(x);
} while (fabs(f(x)) > eps);
return x;
}
int main()
{
double a, b;
a = -10.0;
b = 10.0;
printf("%lf\n", binary(a, b));
printf("%lf\n", false_pos(a, b));
printf("%lf\n", newton(3));
return 0;
}
C++ 平分法、试位法、牛顿法求方程近似解
猜你喜欢
转载自blog.csdn.net/harvestwu/article/details/80991087
今日推荐
周排行