Q9
unknown
python
a year ago
810 B
4
Indexable
import numpy as np import matplotlib.pyplot as plt from scipy.optimize import minimize # Função objetivo original def f(x): return (x - 1)**2 # Função de penalidade def P(x, r): return r * (max(0, 2 - x)**2 + max(0, x - 4)**2) # Função objetivo penalizada def f_p(x, r): return f(x) + P(x, r) # Valores de r r_values = [1, 10, 100, 1000, 10000] # Soluções iniciais x0 = 10.0 # Escolhemos um ponto inicial # Armazenar as soluções solutions = [] for r in r_values: result = minimize(f_p, x0, args=(r,), bounds=[(None, None)]) solutions.append(result.x[0]) # Plotar as soluções plt.plot(r_values, solutions, marker='o') plt.xscale('log') plt.xlabel('r (log scale)') plt.ylabel('x') plt.title('Evolução da solução com diferentes valores de r') plt.grid(True) plt.show()
Editor is loading...
Leave a Comment