I am trying to replicate this using the ForwardDiff module
But this is what I got
julia> using ForwardDiff
julia> f(x,y)=[x^2+y^3-1,x^4 - y^4 + x*y]
f (generic function with 1 method)
julia> a = [1.0,1.0]
2-element Array{Float64,1}:
1.0
1.0
julia> ForwardDiff.jacobian(f, a)
ERROR: MethodError: no method matching f(::Array{ForwardDiff.Dual{ForwardDiff.Tag{typeof(f),Float64},Float64,2},1})
Closest candidates are:
f(::Any, ::Any) at REPL[2]:1
Related question. What am I doing wrong here? It seems like the docs and everything says it should support SVector but it isn’t managing the conversion.
function BicycleDynamics(plant::Bicycle, x::SVector{5, T}, u::SVector{2, T}) where {T <: Real}
# of the traction wheel
turning_radius = plant.wheelbase / tan(x[4])
translational_velocity = x[5]
angular_velocity = x[5] / turning_radius
return SVector(cos(x[3]), sin(x[3]), angular_velocity, u[1], u[2])
end
bike = MakeBike()
ad_adapter(x::SVector{7, T}) where {T <: Real} = BicycleDynamics(bike, x[1:5], x[6:7])
ad_point = SVector{7, Float64}(1.0, 1.0, 0.0, 1.0, 1.0, 1.0, 1.0)
ForwardDiff.jacobian(ad_adapter, ad_point)
Produces the error:
ERROR: LoadError: MethodError: no method matching BicycleDynamics(::Bicycle, ::Array{ForwardDiff.Dual{ForwardDiff.Tag{typeof(ad_adapter),Float64},Float64,7},1}, ::Array{ForwardDiff.Dual{ForwardDiff.Tag{typeof(ad_adapter),Float64},Float64,7},1})
EDIT:
Figured it out. Problem was slicing an SVector with constant indices doesn’t result in another SVector. So I need to convert manually
This is given correctly by your method, by instead writing f as f((x)) = [x[1]^2+x[3], x[1]+x[2], x[2]^2 + x[3]^2]
I’m writing it the first way since my real function is much more sophisticated and would be impossible to write as an anonymous function. I need to iterate over a list of things and do some calculations, and at the end of each loop write F[i] = loop i result