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'm writing a program that will solve the value of 1 variable in a 4-variable equality equation, but the variable to be solved will depend on the user's input to the program.

This is what I've come up with: Create a Mathematica script that reads in 4 parameters from the commandline. Read in the given 4 values and assign it to 4 variables. The variable that needs to be solved will have a specific value (say -100). I need to identify that variable and Remove it from the Mathematica namespace, so that 3 variables remain.

I can then call the Solve function that contains n variables, but since I've used Remove on 1 of those, Mathematica solves the value for that variable.

The only part that I don't get to work, is to identify which symbol's value is -100 so that I can call Remove on the symbol.

Can anyone suggest a solution? I've tried putting all my variables in a list and find the symbol with value -100 using Position or Case, but it only returns the -100 numerical value and not the symbol name. As so:

(* Values for a, b, c and d have been read from the commandline using *)
(* a = ToExpression[$CommandLine[[4]]] etc  *)

list = {a, b, c, d}
(* {1, 2, 3, -100} *)

Position[list, -100]
(* {{4}} *)

Cases[list, -100]
(* {-100} *)

(* I can't call Remove on the answers given by Position and Cases to remove variable d *)

I've tried some other solutions as well but with no luck. In case it's relevant - I'm new to Mathematica.

share|improve this question
    
Can you show us the code you've come up with so far? People will be able to give you much better suggestions that way. –  mfvonh Jun 24 at 10:40
    
As I understand your question, it would be better to call Clear on the variable you want to be value-free than to call Remove. –  m_goldberg Jun 24 at 12:34

3 Answers 3

up vote 0 down vote accepted

Here's one way of doing this:

(* names of variables *)
syms = {"a", "b", "c", "d"}

(* values (these would be from user input in your case) *)
vals = {1, 2, -100, 3}

(* value *not* to assign to a symbol *)
noset = -100

(* clean out our symbols to be safe *)
Remove @@ syms

(* do the appropriate assignments *)
MapThread[If[#2 != noset, Evaluate[ToExpression[#1]] = #2] &, {syms, vals}];

(* what's there? *)
{a, b, c, d}

(* {1, 2, c, 3} *)

(* use them in some solve... *)
Solve[a + b == c + d]

(* {{c -> 0}} *)
share|improve this answer
syms = {a, b, c, d};
vals = {1, 2, -100, 3};

Table[If[vals[[i]] != -100, syms[[i]] = vals[[i]], syms[[i]]], {i, 1, Range@Length@syms}]

{1, 2, c, 3}

share|improve this answer

I'd suggest something like...

Block[{a, b, c, d}, 
  With[{a = 1, b = 1, c = 1}, 
    Solve[a + b + c + d == 0, First[Cases[{a, b, c, d}, _Symbol]]]]]

Block localizes the symbols. With gives some of them values. The First[Cases... finds the one without a value.

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.