I threw the code, but there is no right answer. Where can there be a mistake?)

#include "pch.h" #include <iostream> #include <cmath> using namespace std; double fx, f, fa, x, e; int main() { setlocale(LC_ALL, "rus"); cout << "Веедите x= "; cin >> x; cout << endl; cout << "Введите e= "; cin >> e; cout << endl; //x=1; //e=0.00001; fx = log(x); cout << endl << fx; while (abs(f) > e) { f = fx + x - 0.5; fa = 1 / x + 1; x = x - f / fa; cout << "x= " << x << "\n"; } cout << "Конечное решение х= " << x << "\n"; system("pause"); return 0; } 

Closed due to the fact that off-topic participants Vlad from Moscow , Xander , aleksandr barakin , HolyBlackCat , 0xdb Jan 13 at 8:08 pm .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • " Learning tasks are allowed as questions only on the condition that you tried to solve them yourself before asking a question . Please edit the question and indicate what caused you difficulties in solving the problem. For example, give the code you wrote, trying to solve the problem "- Vlad from Moscow, Xander, aleksandr barakin, HolyBlackCat
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Study assignments are allowed as questions only under the condition that you tried to solve them yourself before asking a question. Please edit the question and indicate what caused you difficulties in solving the problem. For example, provide the code you wrote when trying to solve a problem. - cpp questions
  • one
    And what to work with him? The derivative of ln(x) is 1 / x, calculated using the function log(x) from <cmath> ... What else do you need? - Harry
  • I dealt with this task before I wrote this post, while I wrote it and after I wrote it. Just the code was not ready, or rather it was just awful, I corrected it a little - aquarus91

1 answer 1

Well, see for yourself - here is Newton's method:

enter image description here

Here is the derivative of your function:

enter image description here

We divide the function by the derivative, we calculate ... It is assumed that when the difference between two points is less than ε, the solution is found. Everything!

 int main() { const double eps = 1e-8; double dx, x = 1.0; // Начальное приближение do { dx = -x*(log(x)+x-0.5)/(1+x); x += dx; } while(abs(dx) >= eps); cout << x << endl; } 

Your main mistake is you do NOT calculate the logarithm, but always use its value at some starting point. Those. trying to solve the equation fx - x = 0.5 , where fx is the value of the logarithm. Could not suffer and just display fx-0.5 :)