public class Main {
public static void main(String[] args) {
//初始数据
double x1=2.0;
double x2=6.0;
double x3=10.0;
double miu=0.01;
interpolation h=new interpolation(x1,x2,x3,miu);
h.SuanFa();
}
}
class interpolation {
double miu;
double x1;
double x2;
double x3;
public interpolation(double x1,double x2,double x3,double miu){
this.miu=miu;
this.x1=0.0;
this.x2=0.0;
this.x3=0.0;
}
public void SuanFa() {
int k=0;
fun f=new fun();
while(true) {
k++;
x1=a+0.382*(b-a);
x2=a+0.618*(b-a);
// 未完成
System.out.println("x1:"+x1+"x2:"+x2);
if((b-a)<miu) break;
else {
if(f.F(x1)>f.F(x2)) {
System.out.println("左边f(x1):"+f.F(x1)+"f(x2):"+f.F(x2));
a=x1;
x1=x2;
x2=a+0.618*(b-a);
}
else {
System.out.println("右边f(x1):"+f.F(x1)+"f(x2):"+f.F(x2));
b=x2;
x2=x1;
x1=a+0.382*(b-a);
}
}
System.out.println("迭代了"+k+"次,区间[a,b]=["+a+","+b+"]"+'\n'+"f(a)="+f.F(a)+'\n'+"f(b)="+f.F(b));
}
double f1=f.F(a);
double f2=f.F(b);
System.out.println("迭代了"+k+"次,最优区间[a,b]=["+a+","+b+"]"+'\n'+"f(a)="+f1+"f(b)="+f2);
}
}
class fun {
public double F(double x) {
//目标函数
x=x+(20.0/x);
return x;
}
}
05-18
06-11
306
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交