Skip to content

Instantly share code, notes, and snippets.

@Yegorsh
Last active January 5, 2022 21:09
Show Gist options
  • Select an option

  • Save Yegorsh/e190c787d7c222fec153faac8b59e8ea to your computer and use it in GitHub Desktop.

Select an option

Save Yegorsh/e190c787d7c222fec153faac8b59e8ea to your computer and use it in GitHub Desktop.

Revisions

  1. Yegorsh revised this gist Jan 5, 2022. No changes.
  2. Yegorsh revised this gist Dec 21, 2021. No changes.
  3. Yegorsh created this gist Dec 21, 2021.
    34 changes: 34 additions & 0 deletions Secant_method.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    #include <iostream>
    #include <math.h>
    using namespace std;

    double foo(double x){
    return log(x) - 5;
    }

    double SecantMethod(double x0, double x1, const double epsilon){
    double x2;
    do {
    x2 = x0 - ((foo(x0) * (x1 - x0)) / (foo(x1) - foo(x0)));
    x0 = x1;
    x1 = x2;
    } while (fabs(x1 - x0) > epsilon);
    return x2;
    }

    int main(){
    double eps = 0.0000001;
    double x0, x1;
    cout << "x0 = ";
    cin >> x0;
    cout << "x1 = ";
    cin >> x1;
    if (foo(x0) * foo(x1)>0){
    cout << "F(x0)*F(x1)>0!";
    exit(0);
    }

    cout << SecantMethod(x0, x1, eps);

    return 0;
    }