Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

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

3 Answers 3

up vote 6 down vote accepted

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...

share|improve this answer
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
share|improve this answer

Edit Try this

Eliminate[ToExpression[ToString[{"x1"+"x3"-"x4"==0,"x1"+"x2"+"x3"==0}]],{x1}]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.