最优化方法之牛顿法(Java)
算法原理
题目
试用Newton法求函数f(x)=x^4-4*x^3-6*x^2-16*x+4
的最优化解。(x0=6,sgm=10^-2)
代码
主类
package Newton;
public class main {
public static void main(String []args) {
double x=6;
double miu=0.01;
newton f=new newton(x,miu);
f.N_ton();
}
}
子类1
package Newton;
import java.nio.DoubleBuffer;
public class newton {
double x1;
double miu;
public newton(double x1,double miu) {
this.x1=x1;
this.miu=miu;
}
public void N_ton() {
fun f=new fun();
int k=0;
while(true) {
k++;
double f1=f.F1(x1);
double f2=f.F2(x1);
if(f1<miu) break;
else {
x1=x1-f1/f2;
}
}
double f3=f.F3(x1);
System.out.println("极值点为x="+x1+"最优值为f(x)="+f3);
}
}
子类2
package Newton;
public class fun {
public double F1(double x) {
double a=4*Math.pow(x,3)-12*Math.pow(x,2)-12*x-16;
return a;
}
public double F2(double x) {
double b=12*Math.pow(x, 2)-24*x-12;
return b;
}
public double F3(double x) {
return Math.pow(x, 4)-4*Math.pow(x, 3)-6*Math.pow(x, 2)-16*x+4;
}
}