用二分法求下面方程在(-10,10)的根: 2x^3 - 4x^2 + 3x - 6= 0
。
#include <stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> int main(void) { float left = -10, right = 10, mid,temp=10;
while (fabs(temp) > 0.00001) { mid = (left + right) / 2; temp = ((2 * mid - 4) * mid + 3) * mid - 6; if (temp > 0) {//大于0说明太大右端点左移 right = mid; } else if((temp < 0)) { left = mid; } } printf("-10到10的根为=%f\n",mid); return 0; }
#include<stdio.h> #include<math.h> int main() { double left = -10, right = 10, mid; double temp = 10; while (fabs(temp) > 1e-5) { mid = (left + right) / 2; temp = ((2 * mid - 4) * mid + 3) * mid - 6; if (temp > 0) { right = mid; } else if (temp < 0) { left = mid; } } printf("在(-10,10)的根为:%lf", mid); return 0; }
double f(double define_range_left,double define_range_right){ double left,right,mid,x; left = define_range_left; right = define_range_right; double fx; mid = (left+right)/2; x = mid; fx = 2*pow(x,3) - 4*pow(x,2) + 3*x - 6; if (fabs(fx)<=1e-5) return x; else{ if (fx>0) return f(left,mid); else if (fx<0) return f(mid,right); } }
答案解析: 将区间划分为两部分,...
用户登录可进行刷题及查看答案
答案解析: 将区间划分为两部分,记录区间左右端点,得到中点。每次运算将中点带入方程进行运算,求得结果,进行分析:
结果 > 0:将中位数赋值给右端点
结果 < 0:将中位数赋值给左端点
以此类推…
fabs函数是一个求绝对值的函数,求出x的绝对值,和数学上的概念相同;
le-5:10^(-5) ,即0.00001
#include<stdio.h> #include<math.h> int main() { double left = -10, right = 10, mid; double temp = 10; while (fabs(temp) > 1e-5) { mid = (left + right) / 2; //((2x - 4)*x + 3) * x - 6 ==> 2x^3 - 4x^2 + 3x -6 temp = ((2 * mid - 4) * mid + 3) * mid - 6; if (temp > 0) { right = mid; } else if (temp < 0) { left = mid; } } printf("在(-10,10)的根为:%lf", mid); return 0; }
登录后提交答案