# %% Import: import numpy as np # %% Def of objective function: def fun_f(x1, x2): f = (x1 - 1)**2 + (x2 - 2)**2 + 0.5 # Constraint: if not(x2 - x1 - 1.5 >= 0): f = np.inf return f # %% Optimal solution: x1_opt = 0.74747475 x2_opt = 2.25252525 f_min = fun_f(x1_opt, x2_opt) print('-----------------') print(f'x1_opt = {x1_opt:.8f}') print(f'x2_opt = {x2_opt:.8f}') print(f'f_min = {f_min:.8f}') # %% Perturbation of optim variables: kx1 = 0.99 kx2 = 1.00 x1_pert = x1_opt * kx1 x2_pert = x2_opt * kx2 f_pert = fun_f(x1_pert, x2_pert) df = f_pert - f_min print('-----------------') print(f'x1_pert = x1_opt*{kx1:.2f}') print(f'x2_pert = x2_opt*{kx2:.2f}') print(f'f_pert - f_min = {df:.8f}') print('-----------------') print('Test ok?', (df > 0))