2
$\begingroup$

I have a list of strings,

{"x1", "x2", "x3", "x4"}

And a list of linear equations:

{"x1" + "x3" - "x4" == 0, "x1" + "x2" + "x3" == 0}

How can I use Eliminate to eliminate "x1" from these equations? It complains that "x1" is not a valid variable. Is there a workaround?

$\endgroup$
  • 4
    $\begingroup$ I'd suggest you apply something like s_String:>v[s] to your list of equations before using Eliminate. $\endgroup$ – Ymareth May 16 '14 at 12:58
  • $\begingroup$ @Ymareth That works. My only complain is that it adds clutter. $\endgroup$ – becko May 16 '14 at 13:14
  • $\begingroup$ You can use Symbol[str] to convert a string str into a symbol, and then work with the symbol in the usual way. $\endgroup$ – Stephen Luttrell May 16 '14 at 15:27
6
$\begingroup$

Unfortunately the various Solve like functions don't show the same behavior concerning string-type variable names. Some do accept strings (e.g. NDSolve in version 9):

NDSolve[{"x"'["t"] == 0.1*"x"["t"], "x"[0] == 1}, "x", {"t", 0, 1}]

but others don't. For your case, Eliminate obviously doesn't, but you only need to convert those variables which you want to eliminate, like so:

Eliminate[{"x1" + "x3" - "x4" == 0, "x1" + "x2" + "x3" == 0} /. "x1" -> x1, x1]

you could even localize that variable, which won't prevent the variable to be generated in the Global`context but at least they won't be affected from any potential values of such a variable...

| improve this answer | |
$\endgroup$
3
$\begingroup$
var = {"x1", "x2", "x3", "x4"};

eq = {"x1" + "x3" - "x4" == 0, "x1" + "x2" + "x3" == 0};

rules = Thread[var -> ToExpression[var]];

Eliminate[eq /. rules, x1]
-x4 == x2
| improve this answer | |
$\endgroup$
2
$\begingroup$

Edit Try this

Eliminate[ToExpression[ToString[{"x1"+"x3"-"x4"==0,"x1"+"x2"+"x3"==0}]],{x1}]
| improve this answer | |
$\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.