Алгоритм решения
20.03.2022, 19:08. Показов 1118. Ответов 0
Есть данные коды решения, нужна помощь в создании алгоритмов решения.
1.
| C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
| #include <iostream>
using namespace std;
int main()
{
setlocale(LC_ALL, "Russian");
const double EPS = 1E-9;
int n;
cout << "Порядок: " << endl;
cin >> n;
double** a = new double* [n];
for (int i = 0; i <= n; i++)
{
a[i] = new double[n];
}
double* b = new double[n];
double* x = new double[n];
cout << "Введите коэфициенты и собственные члены " << endl;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
cout << "a[" << i << "," << j << "]= ";
cin >> a[i][j];
}
cout << "b[" << i << "]= ";
cin >> b[i];
}
double det = 1;
for (int i = 0; i < n; ++i)
{
int k = i;
for (int j = i + 1; j < n; ++j)
{
if (abs(a[j][i]) > abs(a[k][i]))
{
k = j;
}
}
if (abs(a[k][i]) < EPS)
{
det = 0;
break;
}
swap(a[i], a[k]);
if (i != k)
{
det = -det;
}
det *= a[i][i];
for (int j = i + 1; j < n; ++j)
{
a[i][j] /= a[i][i];
}
for (int j = 0; j < n; ++j)
{
if (j != i && abs(a[j][i]) > EPS)
{
for (int k = i + 1; k < n; ++k)
{
a[j][k] -= a[i][k] * a[j][i];
}
}
}
}
cout << det;
} |
|
2.
| C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
| #include<iostream>
#include<stdlib.h>
#include<time.h>
#include<Windows.h>
#include <iomanip>
using namespace std;
void inversionMatrix(double**, int);
void printMatrix(double**, int);
void deleteMatrix(double**, int);
double** allocatMatrix(int);
int main()
{
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
int N;
cout << "Введите размер матрицы ";
cin >> N;
double** matrix = allocatMatrix(N);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
cout << "a[" << i << "," << j << "]= ";
cin >> matrix[i][j];
}
}
cout << " Исходная матрица " << "\n\n";
printMatrix(matrix, N);
inversionMatrix(matrix, N);
cout << "\n\n";
cout << " Обратная матрица " << "\n\n";
printMatrix(matrix, N);
deleteMatrix(matrix, N);
return 0;
}
void inversionMatrix(double** A, int N)
{
double temp;
double** B = allocatMatrix(N);
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
B[i][j] = 0.0;
if (i == j)
B[i][j] = 1.0;
}
}
for (int k = 0; k < N; k++)
{
temp = A[k][k];
for (int j = 0; j < N; j++)
{
A[k][j] /= temp;
B[k][j] /= temp;
}
for (int i = k + 1; i < N; i++)
{
temp = A[i][k];
for (int j = 0; j < N; j++)
{
A[i][j] -= A[k][j] * temp;
B[i][j] -= B[k][j] * temp;
}
}
}
for (int k = N - 1; k > 0; k--)
{
for (int i = k - 1; i >= 0; i--)
{
temp = A[i][k];
for (int j = 0; j < N; j++)
{
A[i][j] -= A[k][j] * temp;
B[i][j] -= B[k][j] * temp;
}
}
}
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
A[i][j] = B[i][j];
}
}
deleteMatrix(B, N);
}
void printMatrix(double** matrix, int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << setw(8) << setprecision(3) << setiosflags(ios::fixed | ios::showpoint) << matrix[i][j];
}
cout << "\n";
}
}
void deleteMatrix(double** matrix, int n)
{
for (int i = 0; i < n; i++)
{
delete[] matrix[i];
}
delete[] matrix;
}
double** allocatMatrix(int n)
{
double** matrix = new double* [n];
for (int i = 0; i < n; i++)
{
matrix[i] = new double[n];
}
return matrix;
} |
|
3.
| C# | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
| namespace Lagrange
{
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
var xValues = new List<decimal>();
var yValues = new List<decimal>();
for (decimal x = 1; x <= 2; x += 0.1m)
{
xValues.Add(x);
yValues.Add(Function(x));
}
var lagrange = new Dictionary<decimal, decimal>();
for (int i = 0; i < 20; i++)
{
var gridStep = GridStep(i);
lagrange[gridStep] = InterpolateLagrangePolynomial(gridStep, xValues, yValues);
}
foreach (var item in lagrange)
{
var y = item.Value.ToString("F");
Console.WriteLine($"x={item.Key}, y={y}");
}
}
static decimal InterpolateLagrangePolynomial(decimal x, List<decimal> xValues, List<decimal> yValues)
{
decimal lagrangePol = 0;
for (int i = 0; i < xValues.Count; i++)
{
decimal basicsPol = 1;
for (int j = 0; j < xValues.Count; j++)
{
if (j != i)
{
basicsPol *= (x - xValues[j]) / (xValues[i] - xValues[j]);
}
}
lagrangePol += basicsPol * yValues[i];
}
return lagrangePol;
}
static decimal Function(decimal x)
{
return x * (decimal)Math.Log((double)x);
}
static decimal GridStep(int j)
{
decimal h = 0.1m;
return 1 + j * h / 2;
}
}
} |
|
4.
| C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
| #include <iostream>
#include <math.h>
using namespace std;
double f(double x)
{
return x / sqrt(pow(x, 2) + 1);
}
int main()
{
double eps = 0.001;
double a, b;
setlocale(LC_ALL, "Russian");
cout << "Введите границы интегрирования [a, b] = ";
cin >> a >> b;
double I = eps + 1; // I-предыдущее вычисленное значение интеграла
double I1 = 0; // I1-новое, с большим N.
int nodeCount = 0;
for (int N = 2; (N <= 4) || (fabs(I1 - I) > eps); N *= 2)
{
double h, sum2 = 0, sum4 = 0, sum = 0;
h = (b - a) / (2 * N); // Шаг интегрирования.
for (int i = 1; i <= 2 * N - 1; i += 2)
{
sum4 += f(a + h * i); //Значения с нечётными индексами, которые нужно умножить на 4.
sum2 += f(a + h * (i + 1)); //Значения с чётными индексами, которые нужно умножить на 2.
}
sum = f(a) + 4 * sum4 + 2 * sum2 - f(b);//Отнимаем значение f(b) так как ранее прибавили его дважды.
I = I1;
I1 = (h / 3) * sum;
nodeCount++;
}
cout << "Значение интеграла: " << I1 << endl;
cout << "Конечное количество узлов:" << nodeCount << endl;
cout << "Точность интегрирования:" << eps << endl;
return 0;
} |
|
5.
| C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
| #include <iostream>
#include <conio.h>
#include <math.h>
using namespace std;
const int n = 4;
const int N = 100;
const double eps = 0.001;
double f(double x)
{
return x / sqrt(pow(x, 2) + 1);
}
double Gauss(double a, double b)
{
const double ti[n] = { -0.86113631, -0.33998104, 0.33998104, 0.86113631 };
const double Ai[n] = { 0.34785484, 0.65214516, 0.65214516, 0.34785484 };
double ra = (b - a) / 2;
double su = (a + b) / 2;
double Q, S = 0.0;
for (int i = 0; i < n; i++)
{
Q = su + ra * ti[i];
S += Ai[i] * f(Q);
}
return ra * S;
}
int main()
{
setlocale(LC_ALL, "Russian");
double a, b;
cout << "Введите границы интегрирования [a, b] = ";
cin >> a >> b;
double s = 0.0;
for (int i = 0; i < N; ++i)
{
s += Gauss(a + i * (b - a) / N, a + (i + 1) * (b - a) / N);
}
cout << "Значение интеграла: " << s << endl;
cout << "Точность интегрирования:" << eps << endl;
return 0;
} |
|
6.
| C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
| #include <iostream>
#include <vector>
#include <cmath>
using namespace std;
double f(double x)
{
return x * log(x);
}
double df(double x)
{
return log(x) + 1;
}
vector<double> differentiationNewton(double (*functionDiff)(double), double a, double b, int n)
{
double h = (b - a) / n;
vector<double> output;
for (int i = 0; i < n; i++)
{
double check = (functionDiff((b * i) / h + a + h) - functionDiff((b * i) / h + a)) / n;
output.push_back(check);
}
return output;
}
int main()
{
setlocale(LC_ALL, "Russian");
double a, b;
cout << "Введите границы диференциирования [a, b] = ";
cin >> a >> b;
int steps;
cout << "Введите количество шагов сетки = ";
cin >> steps;
vector<double> result = differentiationNewton(df, a, b, steps);
double h = (b - a) / steps;
double border = a;
for (int i = 0; i < result.size(); i++)
{
cout << border << " => " << result[i] << endl;
border += h;
}
} |
|
0
|