import numpy as np
import matplotlib.pyplot as plt
from tkinter import Tk, Label, Entry, Button
# 부분 함수 정의
def piecewise_function(x, a, b, c):
if x < a:
return b * x
else:
return c * x + b
# 그래프 그리기 함수
def plot_graph(a, b, c):
x_values = np.linspace(-10, 10, 400)
y_values = [piecewise_function(x, a, b, c) for x in x_values]
plt.figure(figsize=(6, 4))
plt.plot(x_values, y_values, label=f'Piecewise Function')
plt.title(f'Piecewise Function with a={a}, b={b}, c={c}')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.grid(True)
plt.legend()
plt.show()
# 입력값을 GUI에서 받아들이는 함수
def on_button_click():
try:
a = float(entry_a.get())
b = float(entry_b.get())
c = float(entry_c.get())
plot_graph(a, b, c)
except ValueError:
print("Please enter valid numbers.")
# GUI 구성
root = Tk()
root.title("Piecewise Function Plotter")
Label(root, text="Enter value for a:").grid(row=0, column=0)
Label(root, text="Enter value for b:").grid(row=1, column=0)
Label(root, text="Enter value for c:").grid(row=2, column=0)
entry_a = Entry(root)
entry_b = Entry(root)
entry_c = Entry(root)
entry_a.grid(row=0, column=1)
entry_b.grid(row=1, column=1)
entry_c.grid(row=2, column=1)
button = Button(root, text="Plot Graph", command=on_button_click)
button.grid(row=3, column=0, columnspan=2)
root.mainloop()