Last active
June 2, 2026 06:15
-
-
Save PaulCreusy/6be887031052b63405aaef6bf729295a to your computer and use it in GitHub Desktop.
Minimize convex function without gradient
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| n_iter = 0 | |
| res_list = [] | |
| def func(x): | |
| global n_iter, res_list | |
| n_iter += 1 | |
| res = (x + 1)**2 | |
| res_list.append((x, res)) | |
| return res | |
| g = -3 | |
| d = 0 | |
| fg = func(g) | |
| fd = func(d) | |
| c = (g + d) / 2 | |
| fc = func(c) | |
| max_iter = 10 | |
| while n_iter <= max_iter: | |
| if n_iter % 2 == 0: | |
| gc = (g + c) / 2 | |
| fgc = func(gc) | |
| if fgc > fc: | |
| g = gc | |
| fg = fgc | |
| else: | |
| d = c | |
| fd = fc | |
| c = gc | |
| fc = fgc | |
| else: | |
| dc = (d + c) / 2 | |
| fdc = func(dc) | |
| if fdc > fc: | |
| d = dc | |
| fd = fdc | |
| else: | |
| g = c | |
| fg = fc | |
| c = dc | |
| fc = fdc | |
| print(res_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment