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/1ff6da554bec8b23dc5b7b52197c438a to your computer and use it in GitHub Desktop.

Select an option

Save Yegorsh/1ff6da554bec8b23dc5b7b52197c438a to your computer and use it in GitHub Desktop.
#include <iostream>
#include <math.h>
using namespace std;
double f(double x){
return log(x)-3; //use your function
}
double BisectionMethod(double Xl, double Xr, double epsilon){
if (fabs(f(Xl)) < epsilon){
return Xl;
}
else if (fabs(f(Xr)) < epsilon){
return Xr;
}
else{
if (f(Xr)*f(Xl) < 0){
double Xm;
do{
Xm = (Xl + Xr)/2;
if (f(Xm) == 0){
return Xm;
}
else if (f(Xl)*f(Xm) <= 0){
Xr = Xm;
}
else{
Xl = Xm;
}
} while (fabs(Xl - Xr) > epsilon);
return Xm;
}
else{
cout << "Не возможно найти корень на данном отрезке" << endl;
}
}
}
int main(){
double X_left, X_right, eps = 0.0001;
cout << "X_left = ";
cin >> X_left;
cout << "X_right = ";
cin >> X_right;
double result = BisectionMethod(X_left, X_right, eps);
cout << "X = " << result;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment