"Newton's Iterative method for root" of OJ brush problem

Source: Internet
Author: User

Title Description

The root is obtained by Newton iterative method. The equation is ax3+bx2+cx+d=0. The value of the coefficient a,b,c,d is 1,2,3,4 once and is entered by the main function. Ask for an x in a real roots near 1. After finding the root, it is output by the main function. The result retains two decimal places.

Input

The value of the coefficient a,b,c,d

Output

X a real roots near the 1

Sample input
1 2 3 4
Sample output
-1.65
Tips

The main function is given below, and it is not required to include the following main function when committing

/* C code */

int main ()

{

Double Solut (double, double, double, double);

Double a,b,c,d;

scanf ("%lf%lf%lf%lf", &a,&b,&c,&d);

printf ("%.2f", Solut (A,b,c,d));;

return 0;

}

/* C + + code */

int main ()

{

Double Solut (double, double, double, double);

Double a,b,c,d;

cin>>a>>b>>c>>d;

Cout<<setiosflags (ios::fixed);

Cout<<setprecision (2);

Cout<<solut (a,b,c,d) <<endl;

return 0;

}




The code is as follows:

#include <iostream> #include <iomanip> #include <cmath>using namespace std;double solut (double A, Double b,double c,double D) {    double x=1,x0,f,f1;    Do    {        x0=x;        F= ((a*x0+b) *x0+c) *x0+d;        f1= (3*a*x0+2*b) *x0+c;        X=X0-F/F1;    } while (Fabs (x-x0) >=1e-2);    return x;} int main () {double Solut (double, double, double, double), double a,b,c,d; cin>>a>>b>>c>>d; cout& Lt;<setiosflags (ios::fixed); Cout<<setprecision (2); Cout<<solut (a,b,c,d) <<endl; return 0;}

Operation Result:



Still some do not understand, not the code, but the problem of mathematics, high number Ah, I should take you how to do

"Newton's Iterative method for root" of OJ brush problem

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.