forked from JuliaSmoothOptimizers/OptimizationProblems.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariational.jl
More file actions
31 lines (24 loc) · 742 Bytes
/
variational.jl
File metadata and controls
31 lines (24 loc) · 742 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
export variational
function variational(; n::Int = default_nvar, type::Type{T} = Float64, kwargs...) where {T}
h = 1 // (n + 1)
x0 = T[(i * h) * (1 - i * h) for i = 1:n]
function f(x; n = length(x))
Ti = eltype(x)
term1 = zero(Ti)
term2 = zero(Ti)
@inbounds for k = 1:n
xi = x[k]
xip = (k < n) ? x[k + 1] : zero(Ti)
term1 += xi * (xi - xip) / h
a_prev = (k == 1) ? zero(Ti) : x[k - 1]
b_prev = xi
term2 += (exp(b_prev) - exp(a_prev)) / (b_prev - a_prev)
if k == n
a_last, b_last = xi, zero(Ti)
term2 += (exp(b_last) - exp(a_last)) / (b_last - a_last)
end
end
return 2 * (term1 + 2 * h * term2)
end
return ADNLPModels.ADNLPModel(f, x0, name = "variational"; kwargs...)
end