Szymon Żak
Szymon Żak

Reputation: 561

Partial derivatives in Julia

I'm trying to numerically solve a nonlinear system of equations in Julia. I'm using Newthon method. The only thing I don't know how to do, is to compute an Jacobian matrix. So far I couldn't find the function to compute partial derivatives.

My system:

f(x1, x2) = 2*x2^2+x1^2
g(x1, x2) = (x1-1)^2 + (x2-1/2)^2

Thanks for your support, Best regards, Szymon.

Upvotes: 0

Views: 2008

Answers (1)

carstenbauer
carstenbauer

Reputation: 10127

Let me write as an answer what I already mentioned in the comments. You could use automatic differentiation to calculate the partial derivatives:

julia> using ForwardDiff

julia> f(x) = 2*x[2]^2+x[1]^2 # f must take a vector as input
f (generic function with 2 methods)

julia> g = x -> ForwardDiff.gradient(f, x); # g is now a function representing the gradient of f

julia> g([1,2]) # evaluate the partial derivatives (gradient) at some point x
2-element Array{Int64,1}:
 2
 8

Upvotes: 3

Related Questions