Obtain the root of the equation and the root of the string truncation equation by means of the string truncation method.

Source: Internet
Author: User

Obtain the root of the equation and the root of the string truncation equation by means of the string truncation method.

THE SECANT METHOD

In numerical analysis, the secant method is a root-finding algorithm that uses a succession of roots of secant lines to better approximate a root of a function f. the secant method can be thought of as a finite difference approximation of Newton's method. however, the method was developed independently of Newton's method, and predated the latter by over 3,000 years.

1/* 2 * ========================================== ========================================================== =========== 3*4 * Filename: secant_method.cc 5*6 * Description: secant method 7*8 * Version: 1.0 9 * Created: July 16, 2015 minutes 26 seconds 10 * Revision: none11 * Compiler: g ++ 12*13 * Author: your name (), 14 * Organization: 15*16 * ============================================ ========================================================== ========== 17 */18 # include <iostream> 19 # include <cmath> 20 using namespace std; 21 22 double f (double x) // function formula 23 {24 return x * x-3 * x-1; 25} 26 27 double point (double a, double B) // calculates the intersection of the string and the X axis 28 {29 return (a * f (B)-B * f ()) /(f (B)-f (a); 30} 31 32 double root (double a, double B) // obtain the equation in [, b] root 33 {34 double x, y, y1; 35 y1 = f (a); 36 do {37 x = point (a, B ); // calculates the intersection x coordinate 38 y = f (x); // calculates y39 if (y * y1> 0) 40 y1 = y, a = x; 41 else42 B = x; 43} while (fabs (y)> = 0.000001); // calculate precision 44 return x; 45} 46 47 int main () 48 {49 double a, B; 50 cin> a> B; 51 cout <"root =" <root (a, B) <endl; 52 return 0; 53}

 

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.