If I have a vector
using ForwardDiff
r=1-.9^2/(1-.9cos(x)
p(x)=r(x)*[sin(x), cos(x),0]
ForwardDiff.gradient(p, [1:2pi])
tells me that I have a type miss match. How do I fix this?
what is g
? what is x
?
I changed it.
julia> ForwardDiff.gradient(f, 3)
ERROR: DimensionMismatch: gradient(f, x) expects that x is an array. Perhaps you meant derivative(f, x)?
Perhaps you meant ForwardDiff.derivative(f, 3)
?
why isn't it an array?
say I have a vector
r=1-.9^2/(1-.9cos(x)
r(x)*[sin(x), cos(x),0]
how would I get the derivative?
the x
you're passing is just a 3
, not an array
I changed the problem
It looks like you're still halfway through editing the question
Your line defining r
doesn't have balanced parentheses, and doesnt define what x
is. Later on, you call r(x)
, so I'm guessing it's mean to be a function?
When you write ForwardDiff.gradient(p, [1:2pi])
, what are you actually trying to do here? This is saying that you're asking for the gradient of p
at the point [1:2pi]
which is a vector containing a unit range, which doesn't seem like a valid input.
I think probably what you want to do here is something like this?
julia> using ForwardDiff
julia> r(x) = 1 - 0.9^2 / (1 - 0.9 * cos(x));
julia> p(x) = r(x) * [sin(x), cos(x), 0];
julia> [ForwardDiff.derivative(p, x) for x in 1:2pi]
6-element Vector{Vector{Float64}}:
[1.6442699017595095, 1.7411330053365992, 0.0]
[0.14811354293589507, -0.5194621159954294, 0.0]
[-0.5618728649816087, -0.10915361349834958, 0.0]
[-0.15477901047354534, 0.5137982587545297, 0.0]
[1.1838568224428756, -0.4416363739969377, -0.0]
[-1.680831194586201, -11.984723260384289, -0.0]
But it's hard to tell what you're actually trying to do
Sorry I'm way off I,m trying to create vectors that reflect positions on this function
orbit(x)=-1*sqrt((a-(x+c)^2)*(1-e^2))
orbitm(x)=-orbit(x)
And, I'd like to show 12 vectors from 0 to 2pi
Last updated: Nov 22 2024 at 04:41 UTC