I am attempting to graph the local approximation of a function using the gradient. Here is my code:
using Random
using LinearAlgebra
using Plots
using LaTeXStrings
Random.seed!(0)
x = rand(5,1)
v = rand(5,1)
alphas = LinRange(-1,1,100)
f(x) = log(sum(exp.(x)))
∇f(x) = (1/log(sum(exp.(x))))*[exp(x[1]),exp(x[2]),exp(x[3]),
exp(x[4]), exp(x[5])]
plot(alphas, [f(x+alpha*v) for alpha in alphas], color = :black,
label = L"f(x + \alpha v)")
plot!(alphas, [f(x) + alpha*dot(∇f(x), v) for alpha in alphas],
color=:red, label=L"f(x) + \alpha \nabla f(x)^T v")
xlabel!(L"\alpha")
From my understanding, the red line should be tangent to the curve at alpha = 0, however this is not the case. I am hoping to identify whether this is an issue with my calculation of the gradient, or if it is something incorrect I did in Julia.
The function in question is f(x) = log(Σe^x_i).
