C/C++:二分法查找近似根
来源:互联网 发布:淘宝发展史宣传片 编辑:程序博客网 时间:2024/05/17 04:55
问题
求出y=2*x^3-4*x^2+3*x-6在[-10,10]上的根
#include <iostream>#include <stdlib.h>#include <math.h>using namespace std;double Function(double x){ return 2.0*pow(x, 3) - 4.0*pow(x, 2) + 3.0*x - 6.0;}int main(void){ double fEps,fAccuracy,fLeftEndPoint,fRightEndPoint,fMiddlePoint, fLeftEndPointFunction, fRightEndPointFunction,fMiddlePointFunction; cout << "请输入你想要的精度" << endl; cin >> fEps; fLeftEndPoint = -10.0; fRightEndPoint = 10.0; fAccuracy = fRightEndPoint - fLeftEndPoint; fMiddlePoint = (fRightEndPoint + fLeftEndPoint) / 2.0; if (abs(fEps) > fAccuracy) cout << "您想找的近似根为" << fMiddlePoint << endl; while (abs(fEps)<fAccuracy) { fLeftEndPointFunction = Function(fLeftEndPoint); if (fLeftEndPointFunction==0) { cout << "这是个精确根" << fLeftEndPoint << endl; break; } fRightEndPointFunction = Function(fRightEndPoint); if (fRightEndPointFunction==0) { cout << "这是个精确根" << fRightEndPoint << endl; break; } fMiddlePointFunction = Function(fMiddlePoint); if (fLeftEndPointFunction*fMiddlePointFunction<0) { fRightEndPoint = fMiddlePoint; fAccuracy = fRightEndPoint - fLeftEndPoint; fMiddlePoint = (fRightEndPoint + fLeftEndPoint) / 2.0; if (abs(fEps) > fAccuracy) { cout << "您想找的近似根为" << fMiddlePoint << endl; break; } } if (fRightEndPointFunction*fMiddlePointFunction<0) { fLeftEndPoint = fMiddlePoint; fAccuracy = fRightEndPoint - fLeftEndPoint; fMiddlePoint = (fRightEndPoint + fLeftEndPoint) / 2.0; if (abs(fEps) > fAccuracy) { cout << "您想找的近似根为" << fMiddlePoint << endl; break; } } } system("pause"); return 0;}
注意:使用二分法求方程根需要 左侧的函数表达式在该区间上是单调的
0 0
- C/C++:二分法查找近似根
- 二分法查找(C/C++)
- C代码:二分法求三次方程近似根
- C++: 二分法查找
- C二分法查找
- C 递归二分法查找
- 【C语言】二分法查找
- C语言二分法查找
- 二分法查找(C)
- C语言之基本算法23—二分法求方程近似根
- C语言的二分法查找
- 二分法查找(C语言实现)
- C语言实现二分法查找
- C语言二分法查找数组
- 二分法查找C语言实现
- c语言 折半查找 (二分法查找)
- 二分法求方程近似根
- c语言 二分法查找 修正版 漏洞 二分法 32位
- api文档生成器apidoc的安装和使用
- 【智库2861】大数据制造失业or创造就业?
- poj2155二维线段树
- Github上十大C#开源项目排行榜
- [NOI2015] BZOJ4195 程序自动分析-离散化-并查集
- C/C++:二分法查找近似根
- 【智库2861】大数据就是占有数据?错,如何使用更重要!
- C919简介
- 创意视觉应用︱基于深度学习的CVaaS计算机视觉即服务案例(Computer Vision as a Service)
- 抽象类
- 三个博弈-巴什博奕、威佐夫博弈、尼姆博弈。acm博弈算法笔记HDU 2149,1850,1527
- 【错误日志/debug】二叉树remove函数指针delete后赋NULL
- logstash5.x output 顺序问题
- matplotlib给子图添加图例