using Plots
using LaTeXStrings
theme(:dark)
#default(size = (800, 600)) # set default plot size
# Define objective function f
f(x) = -0.01548523*x^4 + 0.09320228*x^3 + 0.21426876*x^2 + -0.83797152*x + 0.90722445
xmin = -0.654
xmax = 4.074
x = range( xmin, xmax, length=100)
x1 = 0.56
x2 = 3.284
theta = 0.3
x_comb = theta * x1 + (1 - theta) * x2
# Plot function f
plot(x, f, color=:darkblue, linewidth=4,
legend=false,
xticks = ([x1, x2], ["x₁", "x₂"]),
yticks = ([f(x1), f(x2)], ["f(x₁)", "f(x₂)"]))
# Draw the line segment connecting (x1, f(x1)) and (x2, f(x2))
plot!([x1, x2], [f(x1), f(x2)],
lw = 1.5, ls = :dash, color = :green)
# Plot horizontal and vertical lines
plot!([xmin, x1, x1], [f(x1), f(x1), 0],
lw = 1.5, ls = :dash, color = :gray )
plot!([x2, x2, xmin], [0, f(x2), f(x2)],
lw = 1.5, ls = :dash, color = :gray )
# Plot points (x1, f(x1)) and (x2, f(x2))
scatter!([x1, x2, x_comb], [f(x1), f(x2), f(x_comb)],
markersize = 6, color = :red)
# Annotations
annotate!(x_comb + 0.05, f(x_comb) - 0.05, text(L"f(\theta x_1 + (1-\theta) x_2)", :white, :left, :top, 15))
annotate!(x_comb + 0.05, theta * f(x1) + (1-theta) * f(x2), text(L"\theta f(x_1) + (1-\theta) f(x_2)", :white, :right, 15))