2
$\begingroup$

Say I have a set of equations where the unknown variables are the elements of a vector $c=(c_1,c_2,\ldots)$ and want to solve it for $c$. Is there a way to do this with Solve without typing out all the elements?

In other words: Instead of writing something like

Solve[{c1*x + c2*y == 1, -c1*x + 2*c2*y == -2}, {c1, c2}]

which works and gives

{{c1 -> 4/(3 x), c2 -> -(1/(3 y))}}

I'd like to write something like

Solve[{c[[1]]*x + c[[2]]*y == 1, -c[[1]]*x + 2*c[[2]]*y == -2}, c]

which doesn't work

$\endgroup$
  • $\begingroup$ What workflow do you have in mind, exactly? $\endgroup$ – Igor Rivin Oct 20 '14 at 17:35
  • $\begingroup$ Workflow? In my real computation I will solve much more complicated sets of equations where it would be nice to try different numbers of elements in $c$ without typing each element every time. Is that what you mean? $\endgroup$ – jorgen Oct 20 '14 at 17:45
  • 1
    $\begingroup$ What I have in mind is that the solution I had in mind (which is similar to the ones offered, one of which you had accepted) saves very little effort. $\endgroup$ – Igor Rivin Oct 20 '14 at 18:41
2
$\begingroup$
 cv = Array[ c , 2 ]
 Solve[
    { cv[[1]] x + cv[[2]] y == 1 ,  -cv[[1]] x  + 2 cv[[2]] y  == -2} , cv]

{{c[1] -> 4/(3 x), c[2] -> -(1/(3 y))}}

 cv /. First@% 

{4/(3 x), -(1/(3 y))}

$\endgroup$
2
$\begingroup$

And another completely different approach is to use LinearSolve

Clear[c1, c2, x, y]
eqs = {c1*x + c2*y == 1, -c1*x + 2*c2*y == -2};
vars = {c1, c2};
{b, mat} = CoefficientArrays[eqs, vars];
LinearSolve[mat, -b]

Mathematica graphics

$\endgroup$
1
$\begingroup$

Another option:

vars = {c1, c2};
eqs = MapThread[{#1*x + #2*y == 1, -#1*x + 2*#2*y == -2} &, Map[List, vars]];
Solve[First@eqs, vars]

Mathematica graphics

$\endgroup$

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.