main.java

来自「Algorithm of Golden Ratio Method」· Java 代码 · 共 56 行

JAVA
56
字号
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package labzsechenie;/** * * @author r_sattaruly */public class Main {    /**     * @param args the command line arguments     */    public static void main(String[] args) {        // TODO code application logic here        double a=-1;        double b=1;        double e=0.0001;        double t=1.618;        double x1, x2, xz;        double f1, f2, f;                x1=a+(1/(t*t))*(b-a);        x2=a+(1/t)*(b-a);        f1=(x1*x1-x1-1)/(x1*x1+x1+5);        f2=(x2*x2-x2-1)/(x2*x2+x2+5);                        while ((Math.abs(b-a))>e)        {            if(f1<f2)            {                b=x2;                x2=x1;                f2=f1;                x1=a+(1/(t*t))*(b-a);                f1=(x1*x1-x1-1)/(x1*x1+x1+5);            }            else            {                a=x1;                x1=x2;                f1=f2;                x2=a+(1/t)*(b-a);                f2=(x2*x2-x2-1)/(x2*x2+x2+5);             }                                     }        xz=(a+b)/2;        f=(xz*xz-xz-1)/(xz*xz+xz+5);        System.out.println("x* = "+xz+" f(x*) = "+f);    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?