Айк Бадалян
S:20:59:43 25.12
R:21:00:14 25.12
Оба метода в одной программе
Если алгоритм выполнения может сильно различаться, то вот пример как делать в одной программе метод :
#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;
double f(double x)
{
return x * x * x - x;
}
double f1(double x)
{
return 3 * x * x - 1;
}
double f2(double x)
{
return 3 * 2 * x;
}
double g(double x)
{
return x * x * x;
}
double h(double x)
{
return pow(x,1/3);
}
double g1(double x)
{
return 3 * x * x;
}
int main() {
setlocale(LC_ALL, *Russian*);
double e, x0, x00, x1, dx;
int n, n1 = 0;
cout « *Enter error: *;
cin » e;
cout « *Enter an initial approximation: *;
cin » x00;
cout « *Enter the number of iterations: *;
cin » n;
cout « *Simple-iteration method* « endl;
x0 = x00;
if (abs(f1(x0))<1)
{
do
{
x1 = g(x0);
dx = x1 - x0;
x0 = x1;
n1++;
} while ((abs(dx)>e)&&(n1<n));
if (n1 == n)
{
cout « *Could not find a root in * « n « * iterations* « endl;
}
else
cout « *Found a root in * « n1 « * iterations: * « x1 « endl;
}
else if (abs(f1(x0))>1)
{
do
{
x1 = h(x0);
dx = x1 - x0;
x0 = x1;
n1++;
} while ((abs(dx)>e)&&(n1<n));
if (n1 == n)
{
cout « *Could not find a root in * « n « * iterations for x0 = * « x0 « endl;
}
else
cout « *Found a root in * « n1 « * iterations: * « x1 « * (for x0 = * « x0 « *)* « endl;
}
else
{
cout « *Method cannot be applied for x0 = * « x0 « endl;
}
cout « endl « *Newton*s method (using tangents)* « endl;
x0 = x00;
n1 = 0;
if (f(x0) * f2(x0) > 0)
{
do
{
x1 = x0 - f(x0) / f1(x0);
dx = x1 - x0;
x0 = x1;
n1 += 1;
} while ((abs(dx) > e)&&(n1<n));
if (n1 == n)
{
cout « *Could not find a root in * « n « * iterations for x0 = * « x0 « endl;
}
else
cout « *Found a root in * « n1 « * iterations: * « x1 « * (for x0 = * « x0 « *)* « endl;
}
else
{
cout « *Method cannot be applied for x0 = * « x0 « endl;
}
}
ВОТ ОБРАЗЕЦ ВЫПОЛНЕНИЯ