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.
#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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment