Skip to content

Commit 44cc22e

Browse files
committed
Первая лаба
0 parents  commit 44cc22e

20 files changed

+966
-0
lines changed

lab1/CMakeLists.txt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
cmake_minimum_required(VERSION 3.25)
2+
project(Lab1)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
set(optimizations "-Ofast")
7+
8+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${optimizations}")
9+
10+
add_executable(Lab1
11+
src/main.cpp
12+
src/task1.h
13+
src/task2.h
14+
src/task3.h
15+
src/task4.h
16+
src/task5.h
17+
src/task6.h
18+
)

lab1/data/Maple разложение в ряд.mw

+173
Large diffs are not rendered by default.
541 KB
Binary file not shown.
270 KB
Binary file not shown.
Binary file not shown.

lab1/data/отчет.docx

256 KB
Binary file not shown.
36.9 KB
Binary file not shown.

lab1/src/main.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "task1.h"
2+
#include "task2.h"
3+
#include "task3.h"
4+
#include "task4.h"
5+
#include "task5.h"
6+
#include "task6.h"
7+
8+
9+
int main() {
10+
SetConsoleOutputCP(CP_UTF8);
11+
/* int n=1;
12+
while (n!=0){
13+
switch (n) {
14+
case 1:
15+
task1_main(10);
16+
cin >> n;
17+
break;
18+
case 2:
19+
task2_main(10);
20+
cin >> n;
21+
break;
22+
case 3:
23+
task3_main(10);
24+
cin >> n;
25+
break;
26+
case 6:
27+
task6_main(5808);
28+
cin >> n;
29+
break;
30+
}
31+
}*/
32+
// int n0 = task1_main(20);
33+
// task2_main(n0);
34+
// task3_main(n0);
35+
//task4_main();
36+
task5_main();
37+
// task6_main();
38+
return 0;
39+
}

lab1/src/plot/plotTask1.py

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import matplotlib.pyplot as pl
2+
import pandas as pd
3+
4+
5+
def plot1():
6+
data = pd.read_csv("D:/5sem/numerical/Lab1/cmake-build-debug/task1.txt", sep=" ", names=[1, 2])
7+
pl1 = pl
8+
pl1.plot(data[1], data[2])
9+
pl1.axhline(y=0.0002, color='red', linestyle='--')
10+
pl1.xlabel("Количесто узлов, n")
11+
pl1.ylabel("Tочность")
12+
pl1.title("Зависимость Δn")
13+
pl1.grid(True)
14+
pl1.show()
15+
16+
17+
def plot1_2():
18+
data2 = pd.read_csv("D:/5sem/numerical/Lab1/cmake-build-debug/task1_2.txt", sep=" ", names=[1, 2])
19+
pl2 = pl
20+
pl2.figure(figsize=(10, 5))
21+
pl2.plot(data2[1], data2[2])
22+
pl2.xlabel("x")
23+
pl2.ylabel("Tочность")
24+
pl2.title("Ошибка приближения")
25+
pl2.grid(True)
26+
pl2.show()
27+
28+
29+
def plot2():
30+
data3 = pd.read_csv("D:/5sem/numerical/Lab1/cmake-build-debug/task2.txt", sep=" ", names=[1, 2])
31+
pl3 = pl
32+
pl3.figure(figsize=(10, 5))
33+
pl3.plot(data3[1], data3[2])
34+
pl3.xlabel("x")
35+
pl3.ylabel("Tочность")
36+
pl3.title("Ошибка приближения")
37+
pl3.grid(True)
38+
pl3.show()
39+
40+
41+
def check_accur():
42+
data2 = pd.read_csv("D:/5sem/numerical/Lab1/cmake-build-debug/task1_2.txt", sep=" ", names=[1, 2])
43+
data3 = pd.read_csv("D:/5sem/numerical/Lab1/cmake-build-debug/task2.txt", sep=" ", names=[1, 2])
44+
pl.figure(figsize=(10, 5))
45+
pl.subplot(121)
46+
pl.plot(data2[1], data2[2])
47+
pl.subplot(122)
48+
pl.plot(data3[1], data3[2])
49+
pl.show()
50+
51+
def plot3():
52+
data1 = pd.read_csv("D:/5sem/numerical/Lab1/cmake-build-debug/task1_2.txt", sep=" ", names=[1, 2])
53+
data2 = pd.read_csv("D:/5sem/numerical/Lab1/cmake-build-debug/task3.txt", sep=" ", names=[1, 2])
54+
pl.figure(figsize=(11, 5))
55+
pl.subplot(121)
56+
pl.plot(data1[1], data1[2])
57+
pl.subplot(122)
58+
pl.plot(data2[1], data2[2])
59+
pl.show()
60+
61+
62+
def plot6():
63+
data = pd.read_csv("D:/5sem/numerical/Lab1/cmake-build-debug/task6.txt", sep=" ", names=[1, 2])
64+
pl6 = pl
65+
pl6.plot(data[1], data[2])
66+
pl6.xlabel("x")
67+
pl6.ylabel("Tочность")
68+
pl6.title("Ошибка приближения")
69+
pl6.grid(True)
70+
pl6.show()
71+
72+
plot3()

lab1/src/plot/plot_1.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import matplotlib.pyplot as pl
2+
import pandas as pd
3+
4+
5+
def plot1():
6+
data = pd.read_csv("D:/5sem/numerical/practice/labs/Lab1/cmake-build-debug/task1_1.txt", sep=" ", names=[1, 2])
7+
pl1 = pl
8+
pl1.plot(data[1], data[2],color='red')
9+
# pl1.axhline(y=0.0002, color='red', linestyle='--')
10+
pl1.xlabel("Количесто узлов, n")
11+
pl1.ylabel("Tочность")
12+
pl1.title("Зависимость точности от количества узлов")
13+
pl1.grid(True)
14+
pl1.show()
15+
16+
17+
def plot1_2():
18+
data2 = pd.read_csv("D:/5sem/numerical/practice/labs/Lab1/cmake-build-debug/task1_2.txt", sep=" ", names=[1, 2])
19+
pl2 = pl
20+
pl2.figure(figsize=(10, 5))
21+
pl2.plot(data2[1], data2[2])
22+
pl2.xlabel("x")
23+
pl2.ylabel("Tочность")
24+
pl2.title("Ошибка приближения")
25+
pl2.grid(True)
26+
pl2.show()
27+
28+
plot1();plot1_2();

lab1/src/plot/plot_2.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import matplotlib.pyplot as pl
2+
import pandas as pd
3+
4+
data_equals = pd.read_csv("D:/5sem/numerical/practice/labs/Lab1/cmake-build-debug/task1_plot.txt", sep=" ", names=[1, 2])
5+
data_notequals = pd.read_csv("D:/5sem/numerical/practice/labs/Lab1/cmake-build-debug/task2_plot.txt", sep=" ", names=[1, 2])
6+
data_notequalsnode=pd.read_csv("D:/5sem/numerical/practice/labs/Lab1/cmake-build-debug/notequalsnode.txt", sep=" ", names=[1, 2])
7+
8+
pl.plot(data_equals[1],data_equals[2],linewidth=4,color='green',label="Равномерная сетка")
9+
pl.plot(data_notequals[1],data_notequals[2],linewidth=1,color='black',label="Неравномерная сетка")
10+
pl.scatter(data_notequalsnode[1],data_notequalsnode[2],color='red',label="Узлы")
11+
pl.title("График интерполяции")
12+
pl.xlabel("X")
13+
pl.ylabel("Y")
14+
pl.legend(loc='upper left')
15+
pl.show()

lab1/src/plot/plot_3.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import matplotlib.pyplot as pl
2+
import pandas as pd
3+
4+
5+
def plot1():
6+
data = pd.read_csv("D:/5sem/numerical/practice/labs/Lab1/cmake-build-debug/task3.txt", sep=" ", names=[1, 2])
7+
pl1 = pl
8+
pl1.plot(data[1], data[2],color='red')
9+
# pl1.axhline(y=0.0002, color='red', linestyle='--')
10+
pl1.xlabel("x")
11+
pl1.ylabel("Tочность")
12+
pl1.title("Точность на n=10")
13+
pl1.grid(True)
14+
pl1.show()
15+
16+
plot1();

lab1/src/plot/plot_5.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import matplotlib.pyplot as pl
2+
import pandas as pd
3+
4+
5+
def plot1():
6+
data = pd.read_csv("D:/5sem/numerical/practice/labs/Lab1/cmake-build-debug/buap_n_vs_err.txt", sep=" ", names=[1, 2])
7+
pl1 = pl
8+
pl1.plot(data[1], data[2],color='red')
9+
# pl1.axhline(y=0.0002, color='red', linestyle='--')
10+
pl1.xlabel("n")
11+
pl1.ylabel("Tочность")
12+
pl1.title("Зависимость количества узлов от точности")
13+
pl1.grid(True)
14+
pl1.show()
15+
16+
def plot2():
17+
data = pd.read_csv("D:/5sem/numerical/practice/labs/Lab1/cmake-build-debug/node_task5.txt", sep=" ", names=[1, 2])
18+
data_line = pd.read_csv("D:/5sem/numerical/practice/labs/Lab1/cmake-build-debug/task1_plot.txt", sep=" ", names=[1, 2])
19+
pl1 = pl
20+
pl1.plot(data_line[1],data_line[2])
21+
pl1.scatter(data[1], data[2],color='red')
22+
# pl1.axhline(y=0.0002, color='red', linestyle='--')
23+
pl1.xlabel("n")
24+
pl1.ylabel("Tочность")
25+
pl1.title("Зависимость количества узлов от точности")
26+
pl1.grid(True)
27+
pl1.show()
28+
plot2()

lab1/src/plot/plot_6.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import matplotlib.pyplot as pl
2+
import pandas as pd
3+
4+
5+
def plot6_1():
6+
data = pd.read_csv("D:/5sem/numerical/practice/labs/Lab1/cmake-build-debug/test6.txt", sep=" ", names=[1, 2],nrows=100)
7+
pl6 = pl
8+
pl6.plot(data[1], data[2])
9+
pl6.xlabel("Количество сплайнов")
10+
pl6.ylabel("Tочность")
11+
pl6.title("Минимальное количество сплайнов")
12+
pl6.grid(True)
13+
pl6.show()
14+
def plot6_2():
15+
data = pd.read_csv("D:/5sem/numerical/practice/labs/Lab1/cmake-build-debug/task6.txt", sep=" ", names=[1, 2])
16+
pl6 = pl
17+
pl6.plot(data[1], data[2])
18+
pl6.xlabel("x")
19+
pl6.ylabel("Tочность")
20+
pl6.title("Ошибка приближения")
21+
pl6.grid(True)
22+
pl6.show()
23+
24+
plot6_1();plot6_2();

lab1/src/task1.h

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#include <iostream>
2+
#include <cmath>
3+
#include <vector>
4+
#include <windows.h>
5+
#include <fstream>
6+
#include <cstdlib>
7+
#include <string>
8+
9+
using namespace std;
10+
11+
const double a = 0, b = 1;
12+
const double pi = acos(-1.0);
13+
const double epsilon = 2 * 1e-4;
14+
15+
double acos_foo(double x) {
16+
return x*x*acos(0.9*x);
17+
}
18+
19+
double lagrange(vector<double> &x, vector<double> &y, double _x, int n) {
20+
double result = 0.0;
21+
22+
for (int i = 0; i <= n; i++) {
23+
double P = 1.0;
24+
25+
for (int j = 0; j <= n; j++)
26+
if (j != i)
27+
P *= (_x - x[j]) / (x[i] - x[j]);
28+
29+
result += P * y[i];
30+
}
31+
return result;
32+
}
33+
34+
void nodeFillEquals(vector<double> &x, vector<double> &y, int n) {
35+
double a = 0, b = 1;
36+
double h = (b - a) / n;
37+
x[0] = a;
38+
y[0] = acos_foo(x[0]);
39+
for (int i = 1; i <= n; i++) {
40+
x[i] = x[i - 1] + h;
41+
y[i] = acos_foo(x[i]);
42+
}
43+
}
44+
45+
double checkAccuracy(vector<double> &x, vector<double> &y, int n) {
46+
double a = 0, b = 1;
47+
double h_accurace = (b - a) / (double) 10e5;
48+
double sup = -1000, diff, current = 0;
49+
while (current <= b) {
50+
diff = abs(acos_foo(current) - lagrange(x, y, current, n));
51+
if (diff > sup) {
52+
sup = diff;
53+
}
54+
current += h_accurace;
55+
}
56+
return sup;
57+
}
58+
59+
int task1_1(int n) {
60+
int bestNode = 0;
61+
double current_rasn = 0;
62+
bool node_opt = 0;
63+
vector<double> x(n + 1, 0), y(n + 1, 0);
64+
ofstream out1_1;
65+
out1_1.open("task1_1.txt");
66+
for (int i = 1; i <= n; i++) {
67+
nodeFillEquals(x, y, i);
68+
current_rasn = checkAccuracy(x, y, i);
69+
out1_1 << i << " " << current_rasn << endl;
70+
if (current_rasn < epsilon and !node_opt) {
71+
bestNode = i;
72+
node_opt = 1;
73+
}
74+
}
75+
out1_1.close();
76+
return bestNode;
77+
}
78+
79+
double task1_2(int n) {
80+
ofstream out1_2, out1_2_3;
81+
out1_2.open("task1_2.txt");
82+
out1_2_3.open("task1_plot.txt");
83+
double a = 0, b = 1;
84+
vector<double> x(n + 1, 0), y(n + 1, 0);
85+
nodeFillEquals(x, y, n);
86+
double h_accurace = (b - a) / (double) 1e5;
87+
double x_current = 0, diff, sup = -1000;
88+
double y_cur = 0;
89+
while (x_current < b) {
90+
y_cur = lagrange(x, y, x_current, n);
91+
diff = abs(acos_foo(x_current) - y_cur);
92+
out1_2 << x_current << " " << diff << endl;
93+
out1_2_3 << x_current << " " << y_cur << endl;
94+
if (diff > sup) {
95+
sup = diff;
96+
}
97+
x_current += h_accurace;
98+
}
99+
out1_2.close();
100+
out1_2_3.close();
101+
return sup;
102+
}
103+
104+
105+
int task1_main(int n) {
106+
int n0 = task1_1(n);
107+
cout << "n0=" << n0 << endl;
108+
cout << "Интерполяция Лагранжем(равномерная сетка): " << task1_2(n0) << endl;
109+
system("python D:/5sem/numerical/practice/labs/Lab1/src/plot/plot_1.py");
110+
return n0;
111+
}

0 commit comments

Comments
 (0)