Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _USE_MATH_DEFINES
- #include <iostream>
- #include <math.h>
- using namespace std;
- const int n = 10;
- const double Lambda = M_PI;
- const double a = 0;
- const double b = 1;
- double K(double x, double s) {
- return (1 - x) * sin(2 * M_PI * s);
- }
- double f(double x) {
- return (1 - x) / 2;
- }
- double* Gauss(double** a, double* y, int n)
- {
- double* x, max;
- int k, index;
- const double eps = 0.000001;
- x = new double[n];
- k = 0;
- for (; k < n; k++)
- {
- max = abs(a[k][k]);
- index = k;
- for (int i = k + 1; i < n; i++)
- {
- if (abs(a[i][k]) > max)
- {
- max = abs(a[i][k]);
- index = i;
- }
- }
- if (max < eps) return 0;
- for (int j = 0; j < n; j++)
- {
- double temp = a[k][j];
- a[k][j] = a[index][j];
- a[index][j] = temp;
- }
- double temp = y[k];
- y[k] = y[index];
- y[index] = temp;
- for (int i = k; i < n; i++)
- {
- double temp = a[i][k];
- if (abs(temp) < eps) continue;
- for (int j = 0; j < n; j++)
- a[i][j] = a[i][j] / temp;
- y[i] = y[i] / temp;
- if (i == k) continue;
- for (int j = 0; j < n; j++)
- a[i][j] = a[i][j] - a[k][j];
- y[i] = y[i] - y[k];
- }
- }
- for (k = n - 1; k >= 0; k--)
- {
- x[k] = y[k];
- for (int i = 0; i < k; i++)
- y[i] = y[i] - a[i][k] * x[k];
- }
- return x;
- }
- double* Fredholm() {
- double h = (b - a) / n;
- double** mass = new double* [n + 1];
- double* y = new double[n];
- double xi = a;
- for (int i = 0; i <= n; i++, xi += h) {
- mass[i] = new double[n + 2];
- double xj = a;
- for (int j = 0; j <= n; j++, xj += h) {
- mass[i][j] = -Lambda * (j != 0 && j != n ? h : h / 2) * K(xi, xj);
- }
- mass[i][i]++;
- y[i] = f(xi);
- }
- double *y2 = Gauss(mass, y, n+1);
- for (int i = 0; i <= n; i++)
- delete[] mass[i];
- delete[] mass;
- return y2;
- }
- double* Volterra() {
- double h = (b - a) / n;
- double* y = new double[n + 1];
- double xi = a;
- y[0] = f(xi);
- xi += h;
- y[1] = (f(xi) + Lambda * h * K(xi, a) * y[0] / 2) / (1 - Lambda * h * K(xi, xi) / 2);
- xi += h;
- for (int i = 2; i <= n; i++, xi += h) {
- double xj = a, summ = 0;
- for (int j = 0; j <= i - 1; j++, xj += h)
- summ = summ + K(xi, xj) * y[j];
- y[i] = (f(xi) + Lambda * h * (K(xi, a) * y[0] + 2 * summ) / 2) / (1 - Lambda * h * K(xi, xi) / 2);
- }
- return y;
- }
- int main() {
- setlocale(LC_ALL, "RUS");
- cout << "Уравнение Фредгольма второго рода:" << endl;
- double* res = Fredholm();
- for (int i = 0; i <= n; i++)
- cout << floor(res[i]*100000000) / 100000000 << endl;
- cout << endl << "Уравнение Вольтерра второго рода:" << endl;
- double* res2 = Volterra();
- for (int i = 0; i <= n; i++)
- cout << floor(res2[i] * 100000000) / 100000000 << endl;
- delete[] res;
- delete[] res2;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement