Newton tangent method

本文分享了作者使用Java实现牛顿迭代公式的过程,详细展示了如何输入多项式系数并求解方程的迭代解。通过实例演示了如何构造函数并进行数值逼近,适合初学者理解算法应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天高数课讲的牛顿迭代公式,觉得可以用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;
            }
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值