0

Newton's Method to find polynomial solution

Hi everyone,

this is my first post and I am new to C++. I am trying to figure out how to do this project. Newton’s method is an algorithm that can be used to solve an equation of the form f(x) = 0 using a sequence of approximations. Here’s how it works: You make a reasonable guess for a solution, x0.
You should create a function which calculates the value of the polynomial at a specific x value (the inputs for the function should be the array representing the polynomial, its size, and the x-value to evaluate at), and a function which calculates the value of the derivative at a specific x value (same inputs as the previous function).
I have been trying to figure this out for 4 hours already but can't go past cout statements. Please help! this is urgent!
here is what I have done so far. However i am having so much trouble with the functions.

#include<iostream>
#include<cmath>
using namespace std;
double Newtonf(double[], int);
double Newtonf(double polynomial[], int order)
    {
        double b;
        double constant;
        double total;
        double x;
    for(int b=1; b<=order; b++)
        {
            total+=polynomial[b]*pow(x, b)+ constant;
            return total;
        }
    }
double Newtonfd(double[], int);
double Newtonfd(double polynomial[], int order)
    {
        double b;
        int c;
        double total;
        double x;
    for(int b=1; b<=order; b++)
        {
            total+=polynomial[b]*c*pow(x,c-1);
            return total;
        }
    }
int main()
{
    double guess;
    int n;
    cout << "What is the degree of the polynomial equation of the form f(x)=0? ";
    cin >> n;
    int i=1;
    double polynomial[n];
    while(i<=n)
    {
        cout << "Enter the coefficient of the x^" << i << " term: ";
        cin >> polynomial[n-1];
        i++;
    }
    double constant;
    cout << "Enter the constant term: ";
    cin >> constant;
    cout << "Enter a guess for the solution: ";
    cin >> guess;
    cout << Newtonf(polynomial, n) << endl;
    cout << Newtonfd (polynomial, n) << endl;
    system ("pause");
    return 1;
}

Edited by pyTony: C++ code, mover to C++ forum

1
Contributor
1
Reply
2
Views
5 Years
Discussion Span
Last Post by mochiboo5
0

The formula that goes with it is:
You make a reasonable guess for a solution, x0.
To get the next term in the sequence, we evaluate x1 = x0 −f(x0)/f′(x0). To get the next term in the sequence, we evaluate x2 = x1 −f(x1)/f′(x1). In general, the (i + 1)st approximation is given by:
xi+1 = xi − f(xi)/f′(xi) where f′(x) is the derivative of f.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.