Skip to content

Commit

Permalink
Added finite difference control gradient calculation, for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
leespen1 committed Mar 29, 2024
1 parent 9d532f7 commit 28b7726
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Controls/Control.jl
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,18 @@ function eval_q_single(controls, t, pcof, control_index)
local_pcof = get_control_vector_slice(pcof, controls, control_index)
return eval_q(local_control, t, local_pcof)
end

function eval_grad_p_derivative_fin_diff(control::AbstractControl, t::Real,
pcof::AbstractVector{<: Real}, order::Int64)
pcof_copy = copy(pcof)
grad = zeros(length(pcof))
for i in 1:length(pcof)
pcof_copy .= pcof
pcof_copy[i] += 1e-5
pval_r = eval_p_derivative(control, t, pcof_copy, order)
pcof_copy[i] -= 2e-5
pval_l = eval_p_derivative(control, t, pcof_copy, order)
grad[i] = (pval_r - pval_l)/2e-5
end
return grad
end

0 comments on commit 28b7726

Please sign in to comment.