|
|
Re: How to calculate the partial derivative?
Posted:
Nov 21, 2012 4:43 AM
|
|
Dear Dr. M. Abbasi,
The following are all the code I want to implement, Besides the DNx, DNy and DNz, I would also replace all "L1^aa*L2^bb*L3^cc" in matrix K by "aa!*bb!*cc!/((aa+bb+cc+3)!)*6*V, how to do?
Thanks, Tang Laoya
Clear["Global`*"]; eq1 = L1 == (a1 + b1 x + c1 y + d1 z)/6/V; eq2 = L2 == (a2 + b2 x + c2 y + d2 z)/6/V; eq3 = L3 == (a3 + b3 x + c3 y + d3 z)/6/V; eq4 = L4 == (a4 + b4 x + c4 y + d4 z)/6/V; lhs[eq_] := eq /. (lhs_ == rhs_) -> lhs; rhs[eq_] := eq /. (lhs_ == rhs_) -> rhs;
nod = 10; NN = Table[0, {nod}]; NN[[1]] = rhs[eq1] (2 rhs[eq1] - 1); NN[[2]] = rhs[eq2] (2 rhs[eq2] - 1); NN[[3]] = rhs[eq3] (2 rhs[eq3] - 1); NN[[4]] = rhs[eq4] (2 rhs[eq4] - 1); NN[[5]] = 4 rhs[eq1] rhs[eq2]; NN[[6]] = 4 rhs[eq2] rhs[eq3]; NN[[7]] = 4 rhs[eq3] rhs[eq1]; NN[[8]] = 4 rhs[eq3] rhs[eq4]; NN[[9]] = 4 rhs[eq4] rhs[eq1]; NN[[10]] = 4 rhs[eq2] rhs[eq4];
NNT = Flatten[Table[NN[[i]], {i, 1, nod}]]; Print["Dimensions of NNT:", Dimensions[NNT]]; DNx = D[NNT, x]; DNx = DNx /. {rhs[eq1] -> lhs[eq1], rhs[eq2] -> lhs[eq2], rhs[eq3] -> lhs[eq3], rhs[eq4] -> lhs[eq4]}; DNy = D[NNT, y]; DNy = DNy /. {rhs[eq1] -> lhs[eq1], rhs[eq2] -> lhs[eq2], rhs[eq3] -> lhs[eq3], rhs[eq4] -> lhs[eq4]}; DNz = D[NNT, z]; DNz = DNz /. {rhs[eq1] -> lhs[eq1], rhs[eq2] -> lhs[eq2], rhs[eq3] -> lhs[eq3], rhs[eq4] -> lhs[eq4]}; DNxx = Transpose[{DNx}].{DNx}; DNyy = Transpose[{DNy}].{DNy}; DNzz = Transpose[{DNz}].{DNz}; Print["Dimensions of DNxx:", Dimensions[DNxx]];
K = DNxx + DNyy + DNzz;
(* I would replace all "L1^aa*L2^bb*L3^cc" in matrix K by "aa!*bb!*cc!/((aa+bb+cc+3)!)*6*V, how to do? *)
K = Expand[K]; Print["Simplifying K"]; K = Simplify[K]
On Wednesday, November 21, 2012 4:17:21 PM UTC+8, 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 > > > > --Nasser
|
|