Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- from multiprocessing import Process
- import numpy
- from matplotlib import pyplot as plt
- x_start, x_finish, x_delta = -100, 100, 0.1
- class Linear():
- def __init__(self, a, b):
- self.a = a
- self.b = b
- def value(self, x):
- return self.a * x + self.b
- def derivative(self, *args):
- return self.a
- class Sinus():
- def __init__(self):
- pass
- def value(self, x):
- return math.sin(x)
- def derivative(self, x):
- return math.cos(x)
- class Cosinus():
- def __init__(self):
- pass
- def value(self, x):
- return math.cos(x)
- def derivative(self, x):
- return math.sin(x)
- class Superposition():
- def __init__(self, f, g):
- self.f = f
- self.g = g
- def value(self, x):
- return self.f.value(self.g.value(x))
- def derivative(self, x):
- return self.f.derivative(self.g.value(x)) * self.g.derivative(x)
- class Power():
- def __init__(self, a):
- self.a = a
- def value(self, x):
- return math.pow(x, self.a)
- def derivative(self, x):
- return self.a * math.pow(x, self.a-1)
- def isfloat(var):
- try:
- float(var)
- return True
- except ValueError:
- return False
- def input_float(msg):
- while True:
- ret = input(msg)
- if not isfloat(ret):
- print("Параметр введён некорректно, повторите ввод\n")
- else:
- return float(ret)
- def plot_back(class_, seg):
- segment = numpy.linspace(seg[0], seg[1], (seg[1] - seg[0]) / seg[2])
- fig = plt.figure()
- fig.suptitle(" " + class_.__class__.__name__ + '\n', fontsize=20, )
- plt.subplot(2, 1, 1)
- plt.title("Function")
- plt.xlabel("X")
- plt.ylabel("Y")
- plt.grid(color='g', linestyle='--')
- x = segment
- y = [class_.value(i) for i in segment]
- plt.axis([seg[0] - (seg[1] - seg[0])*0.02,
- seg[1] + (seg[1] - seg[0])*0.02,
- min(y) - (max(y) - min(y))*0.06,
- max(y) + (max(y) - min(y))*0.06])
- plt.plot(x, y, "r-", linewidth=2.0)
- plt.subplot(2, 1, 2)
- plt.title("Derivative")
- plt.xlabel("X")
- plt.ylabel("Y'")
- plt.grid(color='g', linestyle='--')
- y = [class_.derivative(i) for i in segment]
- plt.axis([seg[0] - (seg[1] - seg[0])*0.02,
- seg[1] + (seg[1] - seg[0])*0.02,
- min(y) - (max(y) - min(y))*0.06,
- max(y) + (max(y) - min(y))*0.06])
- plt.plot(x, y, "r-", linewidth=2.0)
- plt.show()
- def plot(class_, seg):
- Process(target=plot_back, args=(class_, seg,)).start()
- def interactive_plot(class_):
- global x_start, x_finish, x_delta
- print("Построение графиков.\n"
- "Текущие параметры:\n"
- "X начальное: " + str(x_start) + "\n"
- "X конечное : " + str(x_finish) + "\n"
- "Шаг X : " + str(x_delta) + "\n")
- while True:
- ans = input("Хотите использовать текущие параметры? Y/n || Д/н: ")
- if ans == 'n' or ans == "н" or \
- ans == 'N' or ans == "Н":
- x_start = input_float("Введите X начальное: ")
- x_finish = input_float("Введите X конечное : ")
- x_delta = input_float("Введите шаг : ")
- elif ans == 'y' or ans == "д" or \
- ans == 'Y' or ans == "Д" or ans == "":
- break
- plot(class_, (x_start, x_finish, x_delta))
- while True:
- print("1. Линейная\n"
- "2. Синус\n"
- "3. Косинус\n"
- "4. Суперпозиция\n"
- "5. Степенная функция\n")
- number_of_func = input_float("Введите номер желаемой функции: ")
- if number_of_func == 1.0:
- interactive_plot(Linear(input_float("Введите параметр a: "),
- input_float("Введите параметр b: ")))
- elif number_of_func == 2.0:
- interactive_plot(Sinus())
- elif number_of_func == 3.0:
- interactive_plot(Cosinus())
- elif number_of_func == 4.0:
- # while True:
- # ans = input_float("Выбирете функцию f:\n"
- # "1. Линейная\n"
- # "2. Синус\n"
- # "3. Косинус\n"
- # "4. Степенная функция\n")
- # interactive_plot(Superposition())
- pass
- elif number_of_func == 5.0:
- interactive_plot(Power(input_float("Введите параметр a: ")))
- print("\n")
Advertisement
Add Comment
Please, Sign In to add comment