# How can I build a Manipulate expression to interactively solve an equation with many parameters?

I'm new to Mathematica and I'm a little lost as to how to do this.

I have a somewhat complicated equation with a bunch of different input variables and one output variable (pco2) and I want to create a manipulate output for the variable pco2.

equation =
(-0.4692*cprop+1.4919)*(pco2-tc)+(so+a/(po2-po))*(pco2-tp)+dw ==
k*vco2/(pco2*(1-vdanat-vdalvratio))-(psv-vth*r-peepi)*fr/(1/cl + 1/ccw);


I turned this into a function as such:

fpco2[
r_, dw_, a_, fr_, po_, tc_, tp_, cprop_, po2_, peepi_, so_, vth_, psv_, cl_,
ccw_, k_, vco2_, vdanat_, vdalvratio_] :=
Evaluate[equation];


I want be able to be able to create a Manipulate expression which can tell me the values of the variable pco2 for different inputs. So I tried:

Pane[
Manipulate[
Column[
{
Pane[
{"Test=",
fpco2[r, dw, a, fr, po, tc, tp, cprop, po2, peepi, so, vth, psv,
cl, ccw, k, vco2, vdanat, vdalvratio]}]}],
{{cl,0.075,"CL"},0,2,Appearance->"Labeled"},
{{ccw,0.125,"CCW"},0,2.5,Appearance->"Labeled"},
{{r,5,"R"},0,10,Appearance->"Labeled"},
{{peepi,0,"PEEPi"},0,20,Appearance->"Labeled"},
{{po2,100,"PO2"},60,140,Appearance->"Labeled"},
{{vth,0.2,"VTH"},0,1,Appearance->"Labeled"},
{{psv,0,"PSV"},0,15,Appearance->"Labeled"},
{{cprop,0.5,"CPROP"},0,3,Appearance->"Labeled"},
{{dw,7,"DW"},0,15,Appearance->"Labeled"},
{{a,16.7,"A"},0,20,Appearance->"Labeled"},
{{fr,20,"FR"},0,30,Appearance->"Labeled"},
{{po,30,"PO"},0,50,Appearance->"Labeled"},
{{tc,34,"TC"},0,50,Appearance->"Labeled"},
{{tp,34,"TP"},0,50,Appearance->"Labeled"},
{{so,0,"SO"},0,2,Appearance->"Labeled"},
{{k,0.75,"K"},0,5,Appearance->"Labeled"},
{{vco2,0.25,"VCO2"},0,3,Appearance->"Labeled"},
{{vdanat,0.12,"VDANAT"},0,1,Appearance->"Labeled"},
{{vdalvratio,0.3,"VDALV"},0,1,Appearance->"Labeled"},
ContentSize->Automatic]]


and this gets me:

which doesn't end up returning just the pco2 value but an expression for pco2.

When I try a sample input to my function:

fpco2[5,7,16.7,20,30,34,34,0.5,100,0,0,0.2,10,0.075,0.125,0.75,0.25,0.12,0.3]


I get

7 + 1.49587 (-34 + pco2) == -8.4375 + 0.323276/pco2


which only becomes the answer I want when I apply NSolve onto it:

NSolve[fpco2[5,7,16.7,20,30,34,34,0.5,100,0,0,0.2,10,0.075,0.125,0.75,0.25,0.12,0.3],pco2]

{{pco2 -> 23.6891}, {pco2 -> -0.00912287}}


Anyone know how I can get my desired output?

• Sorry - I copied a wrong bit of code as I was playing around with it. I mean to put in my function there not the variable 'solution'. Nov 20, 2017 at 3:51
• You've realized the problem yourself, no? You've created one equation with one unknown variable. Mathematica won't solve it until you ask it to, using Solve or NSolve. Just do Row@{"Test=", Solve[ fpco2[ ... ], pco2 ]. (Row is just for formatting.) Note that you can probably solve the symbolic equation before plugging in values, which will make Manipulate run even faster. Nov 20, 2017 at 4:20
• Ah thanks a bunch. I feel a little silly now. Nov 20, 2017 at 4:50
• @Regailia No worries -- happens to all of us! Nov 20, 2017 at 4:53

I think this might be what you want or, at least, it will get moving in the right direction.

I have modified your Manipulate expression in the following ways:

• Corrected some syntax errors.
• Removed unneeded code, including the definition of function fpco2.
• Wrapped the expression in Block to make sure certain free variables remain value free.
• Introduced Row to improve output formatting.
• Solved the equation within the expression.

Block[{eq, sol, pco2},
Manipulate[
eq =
(-0.4692*cprop + 1.4919)*(pco2 - tc) +
(so + a/(po2 - po))*(pco2 - tp) + dw ==
k*vco2/(pco2*(1 - vdanat - vdalvratio)) -
(psv - vth*r - peepi)*fr/(1/cl + 1/ccw);
sol = NSolve[eq, pco2];
Column[{Row[ {"Equation: ", eq}], Row[{"Solutions: ", sol}]}],
{{cl, 0.075, "CL"}, 0, 2, Appearance -> "Labeled"},
{{ccw, 0.125, "CCW"}, 0, 2.5, Appearance -> "Labeled"},
{{r, 5, "R"}, 0, 10, Appearance -> "Labeled"},
{{peepi, 0, "PEEPi"}, 0, 20, Appearance -> "Labeled"},
{{po2, 100, "PO2"}, 60, 140, Appearance -> "Labeled"},
{{vth, 0.2, "VTH"}, 0, 1, Appearance -> "Labeled"},
{{psv, 0, "PSV"}, 0, 15, Appearance -> "Labeled"},
{{cprop, 0.5, "CPROP"}, 0, 3, Appearance -> "Labeled"},
{{dw, 7, "DW"}, 0, 15, Appearance -> "Labeled"},
{{a, 16.7, "A"}, 0, 20, Appearance -> "Labeled"},
{{fr, 20, "FR"}, 0, 30, Appearance -> "Labeled"},
{{po, 30, "PO"}, 0, 50, Appearance -> "Labeled"},
{{tc, 34, "TC"}, 0, 50, Appearance -> "Labeled"},
{{tp, 34, "TP"}, 0, 50, Appearance -> "Labeled"},
{{so, 0, "SO"}, 0, 2, Appearance -> "Labeled"},
{{k, 0.75, "K"}, 0, 5, Appearance -> "Labeled"},
{{vco2, 0.25, "VCO2"}, 0, 3, Appearance -> "Labeled"},
{{vdanat, 0.12, "VDANAT"}, 0, 1, Appearance -> "Labeled"},
{{vdalvratio, 0.3, "VDALV"}, 0, 1, Appearance -> "Labeled"},
ControlPlacement -> Bottom]]