tags:

views:

68

answers:

2

Hello

The output to my mathematica cell is just a long algebraic equation produced from Solve[%, Subscript[u, i, j + 1]].

{{Subscript[u, i, 1 + j] -> (1/(
   2 h^2))(-2 h^2 k Subscript[f, i, j] + 2 h^2 Subscript[u, i, j] + 
     2 b h^2 k Subscript[u, i, j] - 
     h k Subscript[u, -1 + i, j] Subscript[\[Mu], i, j] + 
     h k Subscript[u, 1 + i, j] Subscript[\[Mu], i, j] + 
     2 k Subscript[u, -1 + i, j] Subscript[\[Sigma], i, j] - 
     4 k Subscript[u, i, j] Subscript[\[Sigma], i, j] + 
     2 k Subscript[u, 1 + i, j] Subscript[\[Sigma], i, j])}}

I want to collect the terms $u_{i,j}$ on the right side of this expression, but I dont know how to get a handle on the output of Solve[]. i.e I want to do something like

Collect[%, {Subscript[u, i, j - 1], Subscript[u, i, j], Subscript[u, 
  i, j + 1]}]

But here % doesn't quite do the trick. Any ideas on how I might achieve this?

+1  A: 

If figured it out, Mathematica returns nested lists, which can be accessed by %[[1]][[1]]

aukm
+3  A: 

Mathematica returns the nested list, because in general there will be multiple solutions with multiple variables.

The most robust way of extracting the RHS of a replacement rule is to actually use it, e.g.

In[1]:= x/.Solve[x - m b - m c == 0,x]
        Collect[First[%],m]
Out[1]= {b m + c m}
Out[2]= (b + c) m

That said, the command Part[] (or [[]]) can take more than one argument, so it does not need to be repeated:

In[3]:= Solve[x-m b-m c==0,x]
        Collect[%[[1,1,2]],m]
Out[3]= {{x->b m+c m}}
Out[4]= (b + c) m
Simon