Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <algorithm>
- using namespace std;
- double f(double x) {
- return 1 / ((x + 1) * (log(abs(x + 1)) + 1));
- }
- double g(double x, double y) {
- return -y / (x + 1) - y * y;
- }
- int main() {
- setlocale(LC_ALL, "rus");
- ofstream wr1;
- wr1.open("data.txt");
- int n = 200;
- double a = 0, b = 1, h = (b - a) / (n - 1), x, y;
- for (int i = 0; i < n; i++) {
- x = a + i * h;
- y = f(x);
- wr1 << x << ' ' << y << endl;
- }
- wr1.close();
- ofstream wr2;
- wr2.open("Максимальные невязки при N = 5, 20, 100.txt");
- cout << "n = ";
- cin >> n;
- h = (b - a) / (n - 1);
- wr1.open("data" + to_string(n) + ".txt");
- x = a;
- y = 1;
- double mx = abs(y - f(x));
- wr1 << x << ' ' << y << endl;
- for (int i = 1; i < n; i++) {
- y = y + h * g(x, y);
- x = a + i * h;
- mx = max(mx, abs(y - f(x)));
- wr1 << x << ' ' << y << endl;
- }
- wr2 << "Максимальная невязка при N = " + to_string(n) + ": " << mx << endl;
- wr1.close();
- wr1.open("instr.txt");
- wr1 << "# инструкции для gnuplot" << endl;
- wr1 << "set xzeroaxis # нарисовать ось x" << endl;
- wr1 << "set yzeroaxis # нарисовать ось y" << endl;
- wr1 << "set size ratio - 1 # масштаб по осям 1:1" << endl;
- wr1 << "plot \"data.txt\" with lines title \"Точное решение\", \"data1.txt\" with lines title \"N = 5\"" << endl;
- wr1.close();
- wr2.close();
- system("start_gnuplot.bat");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement