Advertisement
Shiyan12

Untitled

Jun 6th, 2021
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. double f(double x) {
  9.     return 1 / ((x + 1) * (log(abs(x + 1)) + 1));
  10. }
  11.  
  12. double g(double x, double y) {
  13.     return -y / (x + 1) - y * y;
  14. }
  15.  
  16. int main() {
  17.     setlocale(LC_ALL, "rus");
  18.  
  19.     ofstream wr1;
  20.     wr1.open("data.txt");
  21.     int n = 200;
  22.     double a = 0, b = 1, h = (b - a) / (n - 1), x, y;
  23.     for (int i = 0; i < n; i++) {
  24.         x = a + i * h;
  25.         y = f(x);
  26.         wr1 << x << ' ' << y << endl;
  27.     }
  28.     wr1.close();
  29.  
  30.     ofstream wr2;
  31.     wr2.open("Максимальные невязки при N = 5, 20, 100.txt");
  32.  
  33.     cout << "n = ";
  34.     cin >> n;
  35.     h = (b - a) / (n - 1);
  36.     wr1.open("data" + to_string(n) + ".txt");
  37.     x = a;
  38.     y = 1;
  39.     double mx = abs(y - f(x));
  40.     wr1 << x << ' ' << y << endl;
  41.     for (int i = 1; i < n; i++) {
  42.         y = y + h * g(x, y);
  43.         x = a + i * h;
  44.         mx = max(mx, abs(y - f(x)));
  45.         wr1 << x << ' ' << y << endl;
  46.     }
  47.     wr2 << "Максимальная невязка при N = " + to_string(n) + ": " << mx << endl;
  48.     wr1.close();
  49.  
  50.     wr1.open("instr.txt");
  51.     wr1 << "# инструкции для gnuplot" << endl;
  52.     wr1 << "set xzeroaxis # нарисовать ось x" << endl;
  53.     wr1 << "set yzeroaxis # нарисовать ось y" << endl;
  54.     wr1 << "set size ratio - 1 # масштаб по осям 1:1" << endl;
  55.     wr1 << "plot \"data.txt\" with lines title \"Точное решение\", \"data1.txt\" with lines title \"N = 5\"" << endl;
  56.     wr1.close();
  57.  
  58.     wr2.close();
  59.  
  60.     system("start_gnuplot.bat");
  61.     return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement