Julia Differentiation

96 Views Asked by At

I have been looking on many different pages for a suitable Julia package to compute a similar (but more complicated) differentiation to the one below.

Say we have a function f(x)=5x[1]^4 - 2x[2]^2 is there a way to compute the partial derivative of this function with respect to x[1] say, but I would want the result in Julia gives simply as 20x[1]^3 to be used onwards in the code as the result would be if x[1] and x[2] were to be say x and y specifically?

I have looked into ForwardDiff as this deals with vector notation thus far but only encountered errors when implementing it. I am using the vector notation as x[1] and x[2] and the vector representations used later on in the code for the hcubature package for double integrations.

1

There are 1 best solutions below

0
crazyraptor On

It seems like you're looking for something that can be handled by Symbolics.jl. This snippet does what you're looking for:

using Symbolics
@variables x[1:2]
D₁ = Differential(x[1])
eq = 5x[1]^4 - 2x[2]^2
expand_derivatives(D₁(eq))

This will return 20(x[1]^3). You can use this as a symbolic expression in other equations. This resource shows how symbolic arrays are declared in Julia, and this one shows how the Differential operator is used.