1
$\begingroup$

I want to solve something like f(x)=0 and then have the real solution be set as the value of another variable. I can't figure out how to do it. I tried something like this as an example, but it didn't work.

a = Solve[x + 1 == 0, x, Reals]

added: Why won't my InputField dynamically update?

InputField[ Dynamic[G = x /. Solve[ 2 == x*Cos[\[Theta]] -(P*x^2)/(6*C*D) (3 F + x) Sin[\[Theta]], x, Reals]]]

second add: here's the rest of my code.

    Manipulate[
 DynamicModule[{P = 0, C = 1, D = 1, F = 0, θ = 0}, 
  Deploy[Style[
     Panel[Grid[Transpose[{{"P", "C", "D", "F", "θ", "G"},
     {InputField[Dynamic[P]], InputField[Dynamic[C]], 
      InputField[Dynamic[D]], InputField[Dynamic[F]], 
      InputField[Dynamic[θ]], 
      InputField[
       Dynamic[
        G = x /. 
          Solve[2 == 
            x*Cos[θ] - (P*x^2)/(
              6*C*D) (3 F + x) Sin[θ], x, Reals][[1]]]]}}],
   Alignment -> Right], ImageMargins -> 10, DefaultOptions ->
   {InputField -> {ContinuousAction -> True, 
      FieldSize -> {{5, 30}, {1, Infinity}}}}]]] Dynamic[Show[
 {Graphics[{Opacity[0.5], Red,
    Rectangle[{1, 0}, {2, 1}]},
   PlotRange -> {{-1, 2}, {-3, 3}}, Axes -> True,
   AxesOrigin -> {0, 0}],
  ParametricPlot[{x*Cos[θ] - (P*x^2)/(
      6*C*D) (3 F + x) Sin[θ], 
    x*Sin[θ] + (P*x^2)/(6*C*D) (3 F + x) Cos[θ] + 
     G*Sin[θ] + (P*G^2)/(6*C*D) (3 F + G) Cos[θ] - 
     2}, {x, 0, B}, Axes -> True]}]]]]
$\endgroup$
1
  • 6
    $\begingroup$ This appears to be a duplicate of (1819) or (6669) $\endgroup$
    – Mr.Wizard
    Commented Jul 8, 2016 at 1:53

1 Answer 1

2
$\begingroup$

You want to run

a = x /. Solve[x + 1 == 0, x, Reals][[1]]

The [[1]] selects the first solution, and the x /. applies the solution to the expression x.

$\endgroup$
4
  • $\begingroup$ Just what I needed. Thanks! $\endgroup$
    – rxc370
    Commented Jul 8, 2016 at 1:57
  • $\begingroup$ Quick question: I want to do dynamic solve inside of a InputField. What am I doing wrong now? *added to OP $\endgroup$
    – rxc370
    Commented Jul 8, 2016 at 2:17
  • $\begingroup$ I don't know which variables are symbolic and which are numerical, but the Solve is very slow. Also, don't use capital variables. I run: g = x /. Solve[ 2 == xCos[[Theta]] - (px^2)/(6*c*d) (3 f + x) Sin[[Theta]], x, Reals][[1]] InputField[Dynamic[g]] and this displays a result. If I set values for p, c, d, f, and theta, it gives numerics correctly. $\endgroup$ Commented Jul 8, 2016 at 2:24
  • $\begingroup$ I posted the rest of my code. I will definitely change the uppercase variables to lowercase. You can try running the code and you'll see that the last InputField (which is for G) doesn't update. Btw, thanks for your help. You're a lifesaver :) $\endgroup$
    – rxc370
    Commented Jul 8, 2016 at 2:30

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