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.

Consider the formula for the area of a rectangle. I can write the formula as area[length_, width_] := length*width and plug in numbers for the dimensions and get the area. Also, I could write a formula for finding each specific dimension in terms of the remaining two. My question is this: Is there a way to write a single expression which will do the whole job, that is I can plug in any two of the components of the area formula and have Mathematica return the third?

share|improve this question

closed as unclear what you're asking by Artes, bobthechemist, Silvia, rasher, ubpdqn Mar 30 at 10:23

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Yes, what you ask for can be done with Manipulate. –  m_goldberg Mar 28 at 13:07
1  
maybe it would help if you showed examples of what you expect the syntax to look like. The question title doesn't reflect at all what you are asking by the way. –  george2079 Mar 28 at 13:26
    
@george2079. The title is my edit, not the OP's original title. It's my best shot at describing what the OP is asking. Since you think it's way off target, please edit it to get it on target. –  m_goldberg Mar 28 at 21:25

2 Answers 2

up vote 2 down vote accepted
rectangle[{r1_Rule, r2_Rule}] :=
     (#~Join~First@Solve[ (e1 e2 == area) /. # , 
      First@Complement[ {area, e1, e2}, First /@ #] ]) &@{r1, r2}

rectangle[{e2 -> 3, e1 -> 2}]

{e2 -> 3, e1 -> 2, area -> 6}

rectangle[{area -> 12, e1 -> 2}]

{area -> 12, e1 -> 2, e2 -> 6}

share|improve this answer

A variation:

solveMissing[eqn_, vars_] := Solve[
  Append[Equal @@@ Cases[vars, {_Symbol, _}], eqn],
  Replace[vars, {x_Symbol, _} :> x, 1]]

Examples:

solveMissing[area == length*width, {area, {length, 2}, {width, 4}}]
solveMissing[area == length*width, {{area, 8}, length, {width, 4}}]
(*
  {{area -> 8, length -> 2, width -> 4}}
  {{area -> 8, length -> 2, width -> 4}}
*)

solveMissing[area == length*width, {area, length, {width, 4}}]
(*
  Solve::svars: Equations may not give solutions for all "solve" variables. >>
  {{area -> 4 length, width -> 4}}
*)

solveMissing[volume == Pi * radius^2 * height, {{volume, 12 Pi}, radius, {height, 3}}]
(*
  {{volume -> 12 π, radius -> -2, height -> 3},
   {volume -> 12 π, radius -> 2, height -> 3}}
*)
share|improve this answer

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