计算方法(数值分析)实验:方程求根加速代码实现牛顿迭代法 埃特金加速法 割线方法 弦截法 java

计算方法(数值分析)实验:方程求根加速代码实现牛顿迭代法 埃特金加速法 割线方法 弦截法 java,第1张

计算方法(数值分析)实验:方程求根加速代码实现牛顿迭代法 埃特金加速法 割线方法 弦截法 java

实验目的:加深对方程求根加速算法,牛顿法和割线方法的理解,掌握算法;并能对不同方法进行比较。

实验要求:使用上述四种方法求方程:f(x)=x3+4x2-10=0在[1,2]内的一个实根,且要求满足精度|x*-xn|<0.5×10-5

package com.code;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        double resultNewton = FuncDao.NewtonIterativeCalculation(1.5);
        double resultAitejin = FuncDao.AitejinIterativeCalculation(1.5);
        double resultSongchi = FuncDao.songChiIterativeCalculation(1.5);
        double resultXianjie = FuncDao.xianJieFaCalculation(1,2);
        System.out.println("牛顿迭代法求得的根为:" + resultNewton);
        System.out.println("埃特金加速法求得的根为:" + resultAitejin);
        System.out.println("松弛迭代法求得的根为:" + resultSongchi);
        System.out.println("弦截法求得的根为:" + resultXianjie);
    }
}

package com.code;

public class FuncDao {
    //求函数的结果f(x)
    public static double getResult(double X){
        double result = 0;
        result = X * X * X + 4 * X * X - 10;
        return result;
    }

    public static double getAitejinInitResult(double X){
        double result = 0;
        result = Math.pow(((10/(X+4))),0.5);
        return result;
    }

    public static double getAitejinIterativeResult(double X, double X1, double X2){
        double result = 0;
        result = X2 - (X2 - X1) * (X2 - X1) / (X2 - 2 * X1 + X);
        return result;
    }

    public static double getNewtonIterativeResult(double X){
        double e = Math.E;
        double result = 0;
        result = X - (X * X * X + 4 * X * X- 10) / (3 * X * X + 8 * X);
        //System.out.print("f(x)为:");
        //System.out.println( +X*X*X-3*X-Math.pow(e,X)+2);
        //System.out.print("f'(x)为:");

        //System.out.println(3*X*X-3-Math.pow(e,X));
        return result;
    }

    public static double getSongChiInitResult(double X){
        double result = 0;
        result = Math.pow(10/(X+4),0.5);
        return result;
    }
    public static double getSongChiIterativeResult(double X, double X1, double X2){
        double result = 0;
        result = X2 - (X2 - X1) * (X2 - X1) / (X2 - 2 * X1 + X);
        return result;
    }

    public static double getXianJieFaResult(double X0, double X1,double F0, double F1){
        double result = X1 - (X1 - X0) / (F1 - F0) * F1;
        return result;
    }

    public static double getMedian(double left, double right){
        return (left +right) / 2;
    }

    public static double dichotomyCalculation(double left, double right) {
        int num = 0;
        double middle = 0;
        double leftResult = 0;
        double rightResult = 0;
        double middleResult = 0;
        middle = getMedian(left,right);
        System.out.println("二分法第" + num++ + "次结果为:" + middle);
        while (Math.abs(middleResult = getResult(middle)) > 0.5*10e-5 ){
            leftResult = getResult(left);
            rightResult = getResult(right);
            if (leftResult * middleResult < 0){
                right = middle;
                middle = getMedian(left,right);
            }else if (rightResult * middleResult < 0){
                left = middle;
                middle = getMedian(left,right);

            } else{
                throw new ErrorException("该区间左右两侧符号值相同");
            }
            System.out.println("二分法第" + num++ + "次结果为:" + middle);
        }
        System.out.println("-------------------------------------------");
        return middle;
    }

    public static double NewtonIterativeCalculation(double result){
        int num = 0;
        double resultAfter = getNewtonIterativeResult(result);
        System.out.println("牛顿迭代法第" + num++ + "次结果为:" + result);
        while (Math.abs(resultAfter - result) > 5e-6){
            result = resultAfter;
            resultAfter = getNewtonIterativeResult(result);
            System.out.println("牛顿迭代法第" + num++ + "次结果为:" + resultAfter);
        }

        System.out.println("-------------------------------------------");
        return resultAfter;

    }

    public static double AitejinIterativeCalculation(double result){
        int num = 0;
        double x = result;
        double temp;
        double x1 = getAitejinInitResult(result);
        double x2 = getAitejinInitResult(x1);
        System.out.println("埃特金加速法第" + num++ + "次结果为:" + x);
        System.out.println("埃特金加速法第" + num++ + "次结果为:" + x1);
        System.out.println("埃特金加速法第" + num++ + "次结果为:" + x2);
        while (Math.abs(getAitejinIterativeResult(x,x1,x2) - x2 )> 5e-6){
            temp = getAitejinIterativeResult(x,x1,x2);
            x = x1;
            x1 = x2;
            x2 = temp;
            System.out.println("埃特金加速法第" + num++ + "次结果为:" + x2);
        }

        System.out.println("-------------------------------------------");
        return x2;
    }
    public static double getSongChiWn(double X){
        double temp = -0.5 * Math.pow(X + 4,-1.5) * Math.sqrt(10);
        double result = 1 / (1 - temp);
        return result;
    }

    public static double getSongChiXn(double X0,double Wn){
        double result = (1 - Wn) * X0 + Wn * getAitejinInitResult(X0);
        return result;
    }

    public static double songChiIterativeCalculation(double result){
        double wn;
        double X0,X1;
        int num = 0;
        X0 = result;
        wn = getSongChiWn(X0);
        X1 = getSongChiXn(X0,wn);
        System.out.println("松弛迭代法第" + num++ + "次结果为:" + X0);
        System.out.println("松弛迭代法第" + num++ + "次结果为:" + X1);

        while (Math.abs(X1-X0) > 5e-6){
            X0 = X1;
            wn = getSongChiWn(X0);
            X1 = getSongChiXn(X0,wn);
            System.out.println("松弛迭代法第" + num++ + "次结果为:" + X1);
        }
        System.out.println("-------------------------------------------");

        return X1;
    }


    public static double xianJieFaCalculation(double X0, double X1){
        double F0 = getResult(X0);
        double F1 = getResult(X1);
        double X2 = getXianJieFaResult(X0, X1, F0, F1);
        int num = 1;
        System.out.println("弦截法迭代的初始值为:" + X0 + "     " + X1);
        System.out.println("弦截法第" + num++ + "次结果为:" + X2);
        while (Math.abs(X2-X1) > 5e-6){
            X0 = X1;
            X1 = X2;
            F0 = F1;
            F1 = getResult(X1);
            X2 = getXianJieFaResult(X0, X1, F0, F1);
            System.out.println("弦截法第" + num++ + "次结果为:" + X2);
        }

        System.out.println("-------------------------------------------");
        return X2;
    }


}

package com.code;

public class ErrorException extends RuntimeException{

    public ErrorException(String message) {
        super(message);
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5697438.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存