using Symbolics
@variables x
D = Differential(x)
Z = 2.1309 + 0.0515x - 0.0009(x^2)
ZZ = expand_derivatives(D(Z))
# Derivative is 0.0515 - 0.0018x
println("Derivative is ",ZZ)
# How to get the coefficient of x from equation ZZ
# Question 1, how do I get the coefficient of x^0 which is 0.0515
#coefficient_x0 = ???
# Question 2, how do I get the coefficient of x^1 which is -0.0018
#coefficient_x1 = ???
# question 3, How do I find the root of the derivative which is 28.611
# derivative_root = ???
How to extract the coefficients of x from ZZ dynamically?
I think someone recently asked for a coeff function but it doesn’t exist yet.
Linear rootfinding is done via solve_for
.
In math the solution to get the coefficients of a variable is to take the derivative with respect to the variable and set any residues of the variable to zero. Hopefully there is a better method without resorting to taking derivatives but until then that was my solution:
function mycoeffs(f,var,order)
if order == 0
return substitute(f,Dict(var=>0))
end
D=Differential(var^order)
newf=expand_derivatives(D(f))
return substitute(newf,Dict(var=>0))
end
You can test it with something like:
testfun=x*y+y^2+2*z*x
display(mycoeffs(testfun,y,0))
2 Likes