数学二分法 in Java
//Dichotomy.javaclass Function{
//这个类是为了定义函数的计算方法,我还做不到让计算机读懂自然语言
static double cp(double a){
double b = 3.0 * a * a * a * a * a - 2 * a - 4;
return b;
}
}
public class Dichotomy{
boolean cancpt;
double a,b,c,aura;
Dichotomy(double a,double b,double aura){
this.a = a;
this.b = b;
this.aura = aura;
this.fir();
}
void fir(){
if(Function.cp(a) * Function.cp(b) > 0){
System.out.println("区间非法");
}
}
boolean check(){
boolean t;
if(Math.abs((this.a - this.b)) < aura){
t = true;
}else{
t = false;
}
return t;
}
void compute(){
while(this.check() == false){
this.compute_();
}
System.out.println(a + "≤x≤" + b);
}
void compute_(){
c = (a + b) / 2;
if(Function.cp(0.0) == 0.0){
a = c;
b = c;
}else if(Function.cp(a) * Function.cp(c) < 0.0){
b = c;
}else if(Function.cp(b) * Function.cp(c) < 0.0){
a = c;
}
}
}
class MainTest{
public static void main(String[] args){
Dichotomy b = new Dichotomy(0.0,2.0,0.000000000001);
b.compute();
}
}
一般用MATLAB写的算法 这不是查找二分法,是数学函数二分法
落尘之木 发表于 2017-12-8 12:06
一般用MATLAB写的算法
百度了一下才知道MATLAB是啥
页:
[1]