import random
import matplotlib.pyplot as plt
def convex_function(x):
return 0.5 * x**2 # 예제로 사용할 convex 함수: f(x) = 0.5 * x^2
def convex_function_diff(x):
return x
def plot_convergence(maxiterations = 50, threshold = 0.1):
x_values = np.linspace(-50, 50, 1000)
y_values = convex_function(x_values)
plt.plot(x_values, y_values, label='object Function f(x) = 0.5 * x^2')
plt.title('Convergence of a Convex Function')
plt.xlabel('x')
plt.ylabel('f(x)')
# 초기값 설정
initial_x = random.randint(-50, 50)
learning_rate = 0.1
iterations = 50
new_x = initial_x
x_values_history = []
for i in range(maxiterations):
x_values_history.append(new_x)
gradient = convex_function_diff(new_x) # f'(x) = x for this example
next_x = new_x - learning_rate * gradient
if (abs(new_x - next_x) < threshold):
print(f"iteration complete! {i}")
break
new_x = next_x
plt.scatter(x_values_history, convex_function(np.array(x_values_history)), c='red', label='Convergence Points')
plt.legend()
plt.show()
# 수렴 과정 plotting
plot_convergence(50,0.00001)