栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Newton tangent method

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Newton tangent method

今天高数课讲的牛顿迭代公式,觉得可以用java实现,就写了一段程序,望大佬指点

import java.util.Scanner;

public class Newtons {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("the highest power of the function");
        int power = in.nextInt();
        double[] storage = new double[power + 1];
        for (int i = 0; i < storage.length; i++) {
            System.out.printf("please input the coefficient of the power %d", i);
            storage[i] = in.nextDouble();
        }//initialize the function form lowest to highest
        System.out.println("the function is");
        for (int i = 0; i < storage.length; i++) {
            if (i != storage.length - 1) System.out.printf("%.2f x^%d +" + "t", storage[i], i);
            else System.out.printf("%.2f x^%d = 0" + "t", storage[i], i);
        }
        System.out.println("please input the x0");
        double x0 = in.nextDouble();
        while (true) {
            double k = 0;
            double y0 = 0;
            double xi;
            for (int i = 1; i < storage.length; i++) {
                k += storage[i] * i * Math.pow(x0, i - 1);
            }
            for (int i = 0; i < storage.length; i++) {
                y0 += storage[i] * Math.pow(x0, i);
            }
            xi = x0 - y0 / k;
            if (Math.abs(xi - x0) < 0.000001) {
                System.out.println(xi);
                break;
            } else {
                x0 = xi;
            }
        }

    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/340063.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号