-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_plot.py
More file actions
executable file
·65 lines (54 loc) · 1.82 KB
/
Copy pathcreate_plot.py
File metadata and controls
executable file
·65 lines (54 loc) · 1.82 KB
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
import matplotlib.pyplot as plt
import numpy as np
import math
# Параметры построения графиков функций
N = 3000
a1, b1 = -12.0, 120.0
h1 = abs(a1 - b1) / N
x1 = [a1 + h1]
if x1[0] > 0.0:
y1 = [x1[0] ** (1.0 / 3.0)]
else:
y1 = [-((-x1[0]) ** (1.0 / 3.0))]
a2, b2 = -2.0, 120.0
h2 = abs(a2 - b2) / N
x2 = [a2 + h2]
y2 = [math.log(x2[0] + 2.0)]
for i in range(1, N):
x1.append(x1[i-1] + h1)
val = x1[i]
if val > 0.0:
y1.append(val ** (1.0 / 3.0))
else:
y1.append(-((-val) ** (1.0 / 3.0)))
x2.append(x2[i-1] + h2)
y2.append(math.log(x2[i] + 2.0))
roots_x = [-1.696595, 97.07366]
roots_y = [-1.192686, 4.59586]
# Функция невязки для линий уровня
def F(x, y):
return (x - y**3)**2 + (np.log(x + 2) - y)**2
# Область вокруг первого корня
res_x1 = np.linspace(-1.89569, 5.0, 200)
res_y1 = np.linspace(-1.7, 2.0, 200)
X1, Y1 = np.meshgrid(res_x1, res_y1)
Z1 = F(X1, Y1)
# Область вокруг второго корня
res_x2 = np.linspace(91.0, 102.0, 200)
res_y2 = np.linspace(4.1, 5.0, 200)
X2, Y2 = np.meshgrid(res_x2, res_y2)
Z2 = F(X2, Y2)
fig, ax = plt.subplots(figsize=(10, 6))
ax.contour(X1, Y1, Z1, levels=25, cmap='viridis', alpha=0.6)
ax.contour(X2, Y2, Z2, levels=25, cmap='viridis', alpha=0.6)
ax.plot(x1, y1, color='green', linewidth=1.5, label='y = x^(1/3)')
ax.plot(x2, y2, color='red', linewidth=1.5, label='y = ln(x+2)')
ax.plot(roots_x, roots_y, 'o', color='black', markersize=8, label='roots')
ax.set_xlabel('x', fontsize=12)
ax.set_ylabel('y', fontsize=12)
ax.set_title('Graphs of functions and level lines of residual', fontsize=14)
ax.grid(True, linestyle='--', alpha=0.5)
ax.legend(loc='best')
plt.savefig('plot.png', dpi=150, bbox_inches='tight')
print("Plot saved as plot.png")
plt.show()