1
$\begingroup$

I was trying to get an element of a matrix, and use in further computation of my program. Problem was that its not reading the value unless and until I specify it again.

My code is:

     G = k1 Log[k1] + (1.1 + k2) Log[k2] + (k1 + k2) Log[k1 + k2]

   (* k1 Log[k1] + (1.1 + k2) Log[k2] + (k1 + k2) Log[k1 + k2]*)

    M1 = FullSimplify[{{D[G, {k1, 2}], D[G, k2, k1]}, {D[G, k1, k2], 
        D[G, {k2, 2}]}}]

    (*{{1/k1 + 1/(k1 + k2), 1/(k1 + k2)}, {1/(
      k1 + k2), (-1.1 + k2)/k2^2 + 1/(k1 + k2)}}*)

    M2 = FullSimplify[{D[G, k1], D[G, k2]}]

    (*{2 + Log[k1] + Log[k1 + k2], 2 + 1.1/k2 + Log[k2] + Log[k1 + k2]}*)

    M3 =
     PowerExpand[Solve[M1.{d1, d2} == -M2, {d1, d2}]]

    (*{{d1 -> -((-1. ((-1.1 + k2)/k2^2 + 1/(k1 + k2)) (2. + Log[k1] + 
            Log[k1 + k2]) + (2. + 1.1/k2 + Log[k2] + Log[k1 + k2])/(
         k1 + k2))/(
        1/(k1 + k2)^2 - 
         1. (1/k1 + 1/(k1 + k2)) ((-1.1 + k2)/k2^2 + 1/(k1 + k2)))), 
      d2 -> -(k1 + k2) (2. + Log[k1] + Log[k1 + k2]) + (
        1. (k1 + k2) (1/k1 + 1/(
           k1 + k2)) (-1. ((-1.1 + k2)/k2^2 + 1/(k1 + k2)) (2. + Log[k1] +
               Log[k1 + k2]) + (2. + 1.1/k2 + Log[k2] + Log[k1 + k2])/(
           k1 + k2)))/(
        1/(k1 + k2)^2 - 
         1. (1/k1 + 1/(k1 + k2)) ((-1.1 + k2)/k2^2 + 1/(k1 + k2)))}}*)

    G1 = G /. {k1 -> k1 + d1, k2 -> k2 + d2}

    (*(d1 + k1) Log[d1 + k1] + (1.1 + d2 + k2) Log[
       d2 + k2] + (d1 + d2 + k1 + k2) Log[d1 + d2 + k1 + k2]*)

If observed the code, its not taking d1 and d2. I tried getting the part this way, G1=G/.{k1->k1+M3[[1,1]],k2->k2+M3[[1,2]]} Still it did not work. I just wanted to know the way we need to write the syntax , so that if I call k1->k1+ d1, it would directly gets summed up with k1 for this problem.

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Michael E2 Jan 26 '15 at 14:28
  • $\begingroup$ Others will appreciate it if the code can be copied, pasted directly into Mathematica, and executed. The In/Out tags make that difficult. If you set the option SetOptions[$FrontEnd, ExportMultipleCellsOptions -> {"IncludeCellLabels" -> False}], then they won't be copied from your notebook. Note using $FrontEnd sets it for all sessions. Use $FrontEndSession if you just want to set it for your current session. Generally people put output inside comments (*...*) so that it won't affect execution when pasted. $\endgroup$ – Michael E2 Jan 26 '15 at 14:32
  • $\begingroup$ G1 = G /. {k1 -> k1 + d1, k2 -> k2 + d2} /. M3[[1]] $\endgroup$ – SquareOne Jan 26 '15 at 14:36
  • $\begingroup$ Thank you!! Am new to this.. I did not know it though!! I will try to do that! $\endgroup$ – Kezia Jan 26 '15 at 14:36
  • 1
    $\begingroup$ You're welcome (if you're responding to me). Thanks for editing the question. If you put @ in front the user name, like @MichaelE2, the user will get notified of your response to their comment. (The author is always notified, so I didn't have to do that here.) $\endgroup$ – Michael E2 Jan 26 '15 at 14:49
2
$\begingroup$

After working a lot I came out with an answer for my own question!!! Answer is this way(in coded form),

     M3 =
     PowerExpand[Solve[M1.{d1, d2} == -M2, {d1, d2}]]

   (* {{d1 -> -((-1. ((-1.1 + k2)/k2^2 + 1/(k1 + k2)) (2. + Log[k1] + 
            Log[k1 + k2]) + (2. + 1.1/k2 + Log[k2] + Log[k1 + k2])/(
         k1 + k2))/(
        1/(k1 + k2)^2 - 
         1. (1/k1 + 1/(k1 + k2)) ((-1.1 + k2)/k2^2 + 1/(k1 + k2)))), 
      d2 -> -((0.5 (2.2 k1^2 k2 + 3.3 k1 k2^2 + 2. k1^2 k2^2 + 1.1 k2^3 + 
              4. k1 k2^3 + 2. k2^4 - 1. k1^2 k2^2 Log[k1] - 
              1. k1 k2^3 Log[k1] + 2. k1^2 k2^2 Log[k2] + 
              3. k1 k2^3 Log[k2] + k2^4 Log[k2] + 
              k1^2 k2^2 Log[k1 + k2] + 2. k1 k2^3 Log[k1 + k2] + 
              k2^4 Log[k1 + k2]))/(-1.1 k1^2 - 1.65 k1 k2 + k1^2 k2 - 
            0.55 k2^2 + 2. k1 k2^2 + k2^3))}}*)

    G1 = G /. {k1 -> k1 + d1, k2 -> k2 + d2} /. M3

   (* {(1.1 + k2 - (0.5 (2.2 k1^2 k2 + 3.3 k1 k2^2 + 2. k1^2 k2^2 + 

          1.1 k2^3 + 4. k1 k2^3 + 2. k2^4 - 1. k1^2 k2^2 Log[k1] - 
          1. k1 k2^3 Log[k1] + 2. k1^2 k2^2 Log[k2] + 
          3. k1 k2^3 Log[k2] + k2^4 Log[k2] + 
          k1^2 k2^2 Log[k1 + k2] + 2. k1 k2^3 Log[k1 + k2] + 
          k2^4 Log[k1 + k2]))/(-1.1 k1^2 - 1.65 k1 k2 + k1^2 k2 - 
        0.55 k2^2 + 2. k1 k2^2 + k2^3)) Log[
    k2 - (0.5 (2.2 k1^2 k2 + 3.3 k1 k2^2 + 2. k1^2 k2^2 + 1.1 k2^3 + 
          4. k1 k2^3 + 2. k2^4 - 1. k1^2 k2^2 Log[k1] - 
          1. k1 k2^3 Log[k1] + 2. k1^2 k2^2 Log[k2] + 
          3. k1 k2^3 Log[k2] + k2^4 Log[k2] + 
          k1^2 k2^2 Log[k1 + k2] + 2. k1 k2^3 Log[k1 + k2] + 
          k2^4 Log[k1 + k2]))/(-1.1 k1^2 - 1.65 k1 k2 + k1^2 k2 - 
        0.55 k2^2 + 2. k1 k2^2 + 
        k2^3)] + (k1 - (-1. ((-1.1 + k2)/k2^2 + 1/(k1 + k2)) (2. + 
         Log[k1] + Log[k1 + k2]) + (
      2. + 1.1/k2 + Log[k2] + Log[k1 + k2])/(k1 + k2))/(
     1/(k1 + k2)^2 - 
      1. (1/k1 + 1/(k1 + k2)) ((-1.1 + k2)/k2^2 + 1/(k1 + k2)))) Log[
    k1 - (-1. ((-1.1 + k2)/k2^2 + 1/(k1 + k2)) (2. + Log[k1] + 
         Log[k1 + k2]) + (2. + 1.1/k2 + Log[k2] + Log[k1 + k2])/(
      k1 + k2))/(
     1/(k1 + k2)^2 - 
      1. (1/k1 + 1/(k1 + k2)) ((-1.1 + k2)/k2^2 + 1/(
         k1 + k2)))] + (k1 + 
     k2 - (0.5 (2.2 k1^2 k2 + 3.3 k1 k2^2 + 2. k1^2 k2^2 + 1.1 k2^3 + 
          4. k1 k2^3 + 2. k2^4 - 1. k1^2 k2^2 Log[k1] - 
          1. k1 k2^3 Log[k1] + 2. k1^2 k2^2 Log[k2] + 
          3. k1 k2^3 Log[k2] + k2^4 Log[k2] + 
          k1^2 k2^2 Log[k1 + k2] + 2. k1 k2^3 Log[k1 + k2] + 
          k2^4 Log[k1 + k2]))/(-1.1 k1^2 - 1.65 k1 k2 + k1^2 k2 - 
        0.55 k2^2 + 2. k1 k2^2 + 
        k2^3) - (-1. ((-1.1 + k2)/k2^2 + 1/(k1 + k2)) (2. + Log[k1] + 
         Log[k1 + k2]) + (2. + 1.1/k2 + Log[k2] + Log[k1 + k2])/(
      k1 + k2))/(
     1/(k1 + k2)^2 - 
      1. (1/k1 + 1/(k1 + k2)) ((-1.1 + k2)/k2^2 + 1/(k1 + k2)))) Log[
    k1 + k2 - (0.5 (2.2 k1^2 k2 + 3.3 k1 k2^2 + 2. k1^2 k2^2 + 
          1.1 k2^3 + 4. k1 k2^3 + 2. k2^4 - 1. k1^2 k2^2 Log[k1] - 
          1. k1 k2^3 Log[k1] + 2. k1^2 k2^2 Log[k2] + 
          3. k1 k2^3 Log[k2] + k2^4 Log[k2] + 
          k1^2 k2^2 Log[k1 + k2] + 2. k1 k2^3 Log[k1 + k2] + 
          k2^4 Log[k1 + k2]))/(-1.1 k1^2 - 1.65 k1 k2 + k1^2 k2 - 
        0.55 k2^2 + 2. k1 k2^2 + 
        k2^3) - (-1. ((-1.1 + k2)/k2^2 + 1/(k1 + k2)) (2. + Log[k1] + 
         Log[k1 + k2]) + (2. + 1.1/k2 + Log[k2] + Log[k1 + k2])/(
      k1 + k2))/(
     1/(k1 + k2)^2 - 
      1. (1/k1 + 1/(k1 + k2)) ((-1.1 + k2)/k2^2 + 1/(k1 + k2)))]}*)

When it was getting the part out when I call d1, giving "/.M3" i.e, referring to the Parent matrix, gives the value of d1 stored in M3 matrix.

probably many must be knowing this, Still few may be benefitted out of it!

$\endgroup$
  • $\begingroup$ It's perfectly ok to accept your own answer. (Click the checkmark below the vote count.) $\endgroup$ – Michael E2 Apr 27 '15 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.