Kod: Zaznacz cały
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
double f(double x)
{
return sin(3*x)-x+1;
}
double df(double x)
{
return 3*cos(3*x)-1;
}
double newton(const double x0, int &iters)
{
double x, h;
const int max_iterations = 50; // liczba iteracji
x = x0;
for (iters=0; iters<max_iterations; iters++)
{
cout << "x=" << x << "
";
h = f(x)/df(x);
x = x-h;
if ( abs(h) < 1e-6 ) break;
}
return x;
}
int main()
{
cout << "Rozwiazanie rownania sin(3*x)-x+1 = 0 jest nastepujace:
";
cout.precision(10);
int iter;
double x = newton( 0.0, iter );
cout << "x=" << x << " (" << iter << ")
";
return 0;
}