Reputation: 175
I have a function of several variables,
W := (x__A, x__B, x__C, lambda, mu) -> 25*x[A]^2 + 100*x[B]^2 - lambda(6*x__A + 11*x__B + 4*x__C - E__P) - mu(x__A + x__B + x__C - 1)
However, when I try to find the partial derivative of the function W with respect to x_A, I am getting
-6*D(lambda)(6*x__A + 11*x__B + 4*x__C - E__P) - D(mu)(x__A + x__B + x__C - 1)
However, the answer is supposed to be ∂W/∂xA=50x_A - 6λ - μ
. Below is the MWE showing my code and the undesired output:
> W := (x__A, x__B, x__C, lambda, mu) -> 25*x[A]^2 + 100*x[B]^2 - lambda(6*x__A + 11*x__B + 4*x__C - E__P) - mu(x__A + x__B + x__C - 1)
> diff(W(x__A, x__B, x__C, lambda, mu), x__A)
How to solve this problem?
Upvotes: 0
Views: 94
Reputation: 7271
Firstly, there are some typos in your definition of W
.
There should be multiplication symbols between names and the opening brackets, otherwise you're denoting function applications of lambda
and mu
instead of products of terms (assuming you're intending the brackets as subexpression separators, to group terms).
Also, you use x__A
and x__B
in the left hand-side of the definition, but you use x[A]
and x[B]
at two places in the right hand-side. Both render as subscripted items when pretty-printed as output, but they are different things.
So, adjusting those we get,
W := (x__A,x__B,x__C,lambda,mu) -> 25*x__A^2 + 100*x__B^2
- lambda*(6*x__A + 11*x__B + 4*x__C - E__P)
- mu*(x__A + x__B + x__C - 1):
Now, how to differentiate? We can perform a functional differentiation of operator W
with respect to its first argument as follows:
D[1](W);
(x__A,x__B,x__C,lambda,mu) -> 50*x__A-6*lambda-mu
Note that -- similar to W
itself -- the result is also an operator.
You could also call W
with names as arguments, and then differentiate that resulting expression.
diff(W(x__A,x__B,x__C,lambda,mu), x__A);
50 x__A - 6 lambda - mu
Note that doing so worked, but then there's little point to defining W
as an operator; you could more simply have assigned it as the resulting expression. Ie,
W := 25*x__A^2 + 100*x__B^2
- lambda*(6*x__A + 11*x__B + 4*x__C - E__P)
- mu*(x__A + x__B + x__C - 1):
diff(W, x__A);
50 x__A - 6 lambda - mu
Upvotes: 1