1
$\begingroup$

I am trying to solve a set of equations in the form ui+vj=cij up until now I have done a rather complicated work around which is not entirely satisfactory.

What I would like to do is to use the vectors u and v inside the Solve command instead of the ugly solution I've come up with. I want to be able to write something like u[[i]]+v[[j]]==c[[i,j]]

Is is possible?

c = {{36, 32, 33, 19}, {10, 8, 7, 20}, {12, 17, 16, 29}, {23, 15, 16, 
    28}} // MatrixForm

uivj = Solve[{ 0 + v1 == c[[1, 1, 1]],
   0 + v4 == c[[1, 1, 4]],
   u2 + v1 == c[[1, 2, 1]],
   u2 + v2 == c[[1, 2, 2]],
   u2 + v3 == c[[1, 2, 3]],
   u3 + v1 == c[[1, 3, 1]], 
   u4 + v2 == c[[1, 4, 2]]}, {u2, u3, u4, v1, v2, v3, v4}]

u = {{0}, {uivj[[1, 1, 2]]}, {uivj[[1, 2, 2]]}, {uivj[[1, 3, 2]]}}
v = {{uivj[[1, 4, 2]], uivj[[1, 5, 2]], uivj[[1, 6, 2]], 
   uivj[[1, 7, 2]]}}

$\endgroup$
2
  • 2
    $\begingroup$ Don't use MatrixForm. It is a display wrapper and interferes with evaluation. $\endgroup$ – Roman Feb 15 at 15:54
  • $\begingroup$ It get's a little bit easier, but the main problem remains. I want to be able to write something like u[[i]]+v[[j]]==c[[i,j]] $\endgroup$ – Lisch64 Feb 15 at 16:02
4
$\begingroup$

You can use Table:

c = {{36, 32, 33, 19}, {10, 8, 7, 20}, {12, 17, 16, 29}, {23, 15, 16, 28}};

eq = Table[Subscript[u, i] + Subscript[v, j] == c[[i, j]], {i, 1, 4}, {j, 1, 4}]

uivj = Solve[
  Flatten[eq], {Subscript[v, 1], Subscript[v, 2], Subscript[v, 3], 
   Subscript[v, 4], Subscript[u, 1], Subscript[u, 2], Subscript[u, 3],
    Subscript[u, 4]}]

Plain code may look complicated, this is the image of the code:

enter image description here

Use Ctrl - to type subscript $i$ , $j$ , ...


Update:

As CA Trevillian mentioned, using subscript is not the best practice, so the following code is a better version to use and easier to change:

eq = Table[u[i] + v[j] == c[[i, j]], {i, 1, 4}, {j, 1, 4}];

Solve[Flatten[eq], Flatten[Table[{u[i], v[i]}, {i, 1, 4}]]]
$\endgroup$
2
  • $\begingroup$ This solution is very elegant, also generic for any matrix size! Thanks a lot! $\endgroup$ – Lisch64 Feb 15 at 18:32
  • 3
    $\begingroup$ It’s generally not advised to use subscripts. I’d recommend using something like a Format wrapper around v[j] and u[i]. $\endgroup$ – CA Trevillian Feb 15 at 19:05

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.