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

Select an option

Save Yegorsh/76d5c6bafe51a3ce6c0adf0044ea306d to your computer and use it in GitHub Desktop.
#include <iostream>
#include <math.h>
using namespace std;
double f(double x){
return log(x) - 5;
}
double rightDerivative (double x, double h){
double rightDer = (f(x+h) - f(x))/h;
return rightDer;
}
double NewtonsMethod(double x0, double epsilon){
double x1;
for (int j=0; ; j++){
x1 = x0 - ( f(x0) / rightDerivative(x0, 0.000001) );
if (fabs(x1 - x0) > epsilon){
x0 = x1;
}
else {
return x1;
}
}
}
int main(){
double x0;
double eps = 0.000001;
cout << "Нулевое приближение: ";
cin >> x0;
double result = NewtonsMethod(x0, eps);
cout << result;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment