Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » sci.math.* » sci.math.symbolic.independent

Topic: How to calculate the partial derivative?
Replies: 6   Last Post: Nov 21, 2012 5:17 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Nasser Abbasi

Posts: 5,661
Registered: 2/7/05
Re: How to calculate the partial derivative?
Posted: Nov 21, 2012 4:58 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On 11/21/2012 2:17 AM, Nasser M. Abbasi wrote:
> On 11/21/2012 12:15 AM, Tang Laoya wrote:
>> Dear all,
>>
>> I am trying to calculate the partial derivative by mathematica, I have the following commands:
>> L1=a1+b1*x+c1*y;
>> L2=a2+b2*x+c2*y;
>> L3=a3+b3*x+c3*y;
>>
>> NN=L1*L2;
>>
>> DNx=D[NN,x];
>>
>> I got the following result:
>> DNex=b2 (a1+b1 x+c1 y)+b1 (a2+b2 x+c2 y)
>>
>> How to do to have the following result?
>>
>> DNex=b2*L1 + b1 * L2
>>

>


> The problem is that you did not use equations, but used expresions.
> Hence in your case L1 is now 'a1+b1*x+c1*y' and similarly with L2.
>
> To do this right, I'd write things as equations, and use rules, like
> this:
>
> --------------------------
> Clear[L1, L2, a1, b2, x, c1, y, a2, b2, c2, lhs, rhs]
> eq1 = L1 == a1 + b1*x + c1*y;
> eq2 = L2 == a2 + b2*x + c2*y;
> lhs[eq_] := eq /. (lhs_ == rhs_) -> lhs;
> rhs[eq_] := eq /. (lhs_ == rhs_) -> rhs;
>
> NN = rhs[eq1]*rhs[eq2];
> DNx = D[NN, x]
> ----------------------------
>
> which gives what you showed
>
> b2 (a1+b1 x+c1 y)+b1 (a2+b2 x+c2 y)
>
> Now it is easy to use the rules to get what you want
>
> ------------------
> DNx/.{rhs[eq1]->lhs[eq1],rhs[eq2]->lhs[eq2]}
> ---------------------
>
> which gives
>
> b2 L1+b1 L2
>


After thinking more about this, another way to do this,
and to keep from having to write rhs[eq] and rhs[eq] all
the time, which is not as nice looking as L1 and L2 etc..
and can be harder to read if there are many equations, is
to use rules.

Like this:

-----------------------------
Clear["Global`*"];

rule1 = {L1 -> a1 + b1*x + c1*y, L2 -> a2 + b2*x + c2*y}
rule2 = (Last[#] -> First[#]) & /@ rule1
NN = (L1*L2) /. rule1;
DNx = D[NN, x]
-----------------------

Which gives

b2 (a1 + b1 x + c1 y) + b1 (a2 + b2 x + c2 y)

Now to get back, use rule2 above, which is the reverse
of rule1

-------------------------------
In[185]:= DNx/.rule2
Out[185]= b2 L1+b1 L2
-----------------------------

The advantage of this method again is that one used L1 and L2,
instead of rhs[eq1], rhs[eq2] etc.. which is more clear.

--Nasser






Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.