问题描述
解决方案
http://wenku.baidu.com/link?url=wjIO2baxEfcSucwWaY_acszu0735F0QeyzjGf-Vrkr6R0xLvWnExVQSjPHb-4AX541nAiEoAaJGrH7V2aGTaoURkYFxRmydDCzkk5XcVgTS
http://download.csdn.net/detail/u012724893/8289089
解决方案二:
public class demo{
public static void main(String[] str){
double a1 = 1, b1 = 2;
double x1 = 1;
double x2 = (a1 + b1) / 2;
//c存储前一个x2值
double c = 0 ;
double check = 1;
while(check >= 0.00005){
x2 = (a1 + b1)/2.0;
check = Math.abs(x2 - c);
double result = Math.pow(x2, 3) + 4 * Math.pow(x2, 2) - 10;
if(result > 0){
b1 = x2; a1 = x1; c = x2;
}
else {
a1 = x2;
c = x2;
}
}
System.out.println(x2);
}
}
输出为
1.3652290927316866
解决方案三:
可能不同的编译器对数据的处理不一样,你这明显是c语言书。。程序逻辑有问题
解决方案四:
public class demo {
public static void main(String[] str) {
double a1 = 1, b1 = 2;
double x1 = 1;
double x2 = (a1 + b1) / 2;
// c存储前一个x2值
double c = 0;
while (Math.abs(x2 - c) >= 0.00005) {
double result = Math.pow(x2, 3) + 4 * Math.pow(x2, 2) - 10.0;
if (result > 0) {
b1 = x2;
a1 = x1;
c = x2;
} else {
a1 = x2;
c = x2;
}
x2 = (a1 + b1) / 2.00;
}
System.out.println(x2);
}
}
稍微改了一下
解决方案五:
其实你输出的d是方程的值,并不是根,我们要输出的是根值,比较的也是根的灵敏度,对二分法没有理解啊!!
解决方案六:
当然我的做法也有一点不严谨,具体哪一点可以自己思考,也可以参考@caozhy 提供的链接
解决方案七:
public static void main(String[] args) {
double a1 = 1.0, b1 = 2.0;
double x1 = 1.0;
double x2 ;
while (Math.abs(b1 - a1) >= 0.0005) {
x2 = (a1 + b1) / 2;
double result = Math.pow(x2, 3) + 4 * Math.pow(x2, 2) - 10.0;
if (result > 0) {
b1 = x2;
} else {
a1 = x2;
}
}
System.out.println(a1);
}
又仔细看了一下,发现以前理解错了,这次输出是1.36474609375
这个程序你可以自己在完善下
时间: 2024-10-30 14:52:40