V
V
Vladlen982016-10-12 00:51:12
C++ / C#
Vladlen98, 2016-10-12 00:51:12

Please help me figure out the error.

I need to find the roots of the equation by simple iteration, but as a result of running my program, an infinite number is obtained.

#include <iostream>
#include<cmath>
#include<cstdlib>
#include<stdio.h>
using namespace std;
void main() {
  double xn,x0,eps,b;
  eps = 0.0001;//точность вычисления корня
  x0= 1;//начальное приближение

  do {
    xn = -(log(x0/3));//вычисляем очередное приближение 
    b = abs(xn - x0);//находим разницу между предыдущей итерацией и текущей
    xn = x0;//предыдущую итерацию ставим равной текущей
  } while (b > eps);//если условие верно,то продолжаем поиск
cout << "xn="<<xn<<'\n';
  system("pause");
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Moseychuk, 2016-10-12
@fshp

#include <iostream>
#include <cmath>

using namespace std;

int main() {

  const double eps = 0.0001;

  double x0 = 1;
  double b = 0;

  do {
    const double xn = -log(x0 / 3);
    b = abs(xn - x0);
    x0 = xn;
  } while (b > eps);

  cout << "x = " << x0 << endl;

  return 0;
}

x = 1.04996
You have an assignment errorxn = x0

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question