二分法求方程2x³-4x²+3x-6=0的近似解
/* * Copyright (c) 2016, 安徽大学计算机科学与技术学院 * All rights reserved. * 作 者: 杨磊 * 完成日期:2016 年 12 月 28 日 * 问题描述:二分法求方程2x³-4x²+3x-6=0的近似解(方程需单调) */ #include<cstdio> #include<iostream> #include<stdlib.h> #include<string.h> #include<algorithm> #include<math.h> using namespace std; int main(){ double x1, x2, x0, fx1,fx0,fx2; while(1){ printf("输入x1和x2: "); cin>>x1>>x2; fx1 = 2 * x1 * x1 * x1 - 4 * x1 * x1 + 3 * x1 - 6; fx2 = 2 * x2 * x2 * x2 - 4 * x2 * x2 + 3 * x2 - 6; do{ x0 = (x1 + x2) / 2; fx0 = 2 * x0 * x0 * x0 - 4 * x0 * x0 + 3 * x0 - 6; if ((fx0 * fx1) < 0) { x2 = x0; fx2 = fx0; } else { x1 = x0; fx1 = fx0; } } while (fabs(fx0) > 1e-5); printf("近似根为X=%.4f\n", x0);} return 0;}