1

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]]}}

1 Answers1

4

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}]]]

Ben Izd
  • 9,229
  • 1
  • 14
  • 45