Ultraviolet A 10428-the roots (Newton Iteration Method)

Source: Internet
Author: User

Link to the question: Ultraviolet A 10428-the roots

Given a n-times polynomial, all solutions are obtained.

Solution: Newton Iteration Method. For any given X, the Newton iteration method can be used to obtain the nearest x solution x0. After finding a solution, use polynomial division to remove X? Continue to solve the problem after x0.

Newton Iteration Method: XI + 1 = xi? F (x) f' (X)

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 10;int N;double a[maxn];void div (double* f, double x, int n) {    f[n+1] = 0;    for (int i = n; i >= 0; i--)        f[i] += f[i+1] * x;    for (int i = 0; i < n; i++)        f[i] = f[i+1];}double func (double* f, double x, int n) {    double ret = 0, u = 1;    for (int i = 0; i <= n; i++) {        ret += f[i] * u;        u *= x;    }    return ret;}double newton (double* f, int n) {    double fd[maxn];    for (int i = 0; i < n; i++)        fd[i] = f[i+1] * (i+1);    double x = -100;    for (int i = 0; i < 100; i++)        x -= func(f, x, n) / func(fd, x, n-1);    return x;}void solve () {    for (int i = 0; i < N; i++) {        double x = newton(a, N-i);        printf(" %.4lf", x);        div(a, x, N-i);    }}int main () {    int cas = 1;    while (scanf("%d", &N) == 1 && N) {        for (int i = N; i >= 0; i--)            scanf("%lf", &a[i]);        printf("Equation %d:", cas++);        solve();        printf("\n");    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.