0
$\begingroup$

On the 7th line, I have an InputField that I want to dynamically change as I change the input. It solves an equation that is later used to determine the change in y intercept in the graph. However, the value (h) doesn't change as I change the other values. What am I doing wrong here?

I included a picture here. When I change the values in yellow, they should change the value in blue. enter image description here

Manipulate[
   DynamicModule[{p = 0, c = 1, d = 1, f = 0, \[Theta] = 0, i = 1, j = 1, k = 2}, Deploy[Style[
     Panel[Grid[Transpose[{{"P", "C", "D", "F", "\[Theta]", "h", "height", "left", "right"},
         {InputField[Dynamic[p]], InputField[Dynamic[c]], 
          InputField[Dynamic[d]], InputField[Dynamic[f]], 
          InputField[Dynamic[\[Theta]]], 
          InputField[Dynamic[h = x /.Solve[2 == x*Cos[\[Theta]] - (p*x^2)/(6*c*d) (3 f + x) Sin[\[Theta]], x, Reals][[1]]],
          InputField[Dynamic[i]], 
          InputField[Dynamic[j]], InputField[Dynamic[k]]]}}],
       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[\[Theta]] - (p*x^2)/(6*c*d) (3 f + x) Sin[\[Theta]], 
    x*Sin[\[Theta]] + (p*x^2)/(6*c*d) (3 f + x) Cos[\[Theta]] + h*Sin[\[Theta]] + (p*h^2)/(6*c*d) (3 f + h) Cos[\[Theta]] + i}, {x, 0, h}, Axes -> True]}]]]]

Thanks!

edit: small error in pasting

edit2: some changes, pasted below

    Manipulate[h = x /. Quiet@Solve[2 == x*Cos[\[Theta]] - (p*x^2)/(6*c*d) (3 f + x) Sin[\[Theta]], x, Reals][[1]];
 Text[h]
  Show[{Graphics[{Opacity[0.5], Red, Rectangle[{1, 0}, {2, 1}]},
     PlotRange -> {{-1, 2}, {-3, 3}},
     Axes -> True, AxesOrigin -> {0, 0}],
    ParametricPlot[{x*Cos[\[Theta]] - (p*x^2)/(6*c*d) (3 f + x) Sin[\[Theta]],x*Sin[\[Theta]] + (p*x^2)/(6*c*d) (3 f + x) Cos[\[Theta]] + h*Sin[\[Theta]] + (p*h^2)/(6*c*d) (3 f + h) Cos[\[Theta]] + i}, 
 {x, 0, h}, Axes -> True]}],
 {p, 0.2, 2, ControlType -> InputField}, {c, 0.2, 2, ControlType -> InputField}, 
 {d, 0.2, 2, ControlType -> InputField}, {f, 0.2, 2, ControlType -> InputField}, 
 {\[Theta], 0, \[Pi], ControlType -> InputField}, {i, 0.2, 2, ControlType -> InputField}, 
 {j, 0.2, 2, ControlType -> InputField}, {k, 0.2, 2, ControlType -> InputField}, 
 {l, 0.2, 2, ControlType -> InputField}]
$\endgroup$
3
  • 1
    $\begingroup$ Why aren't you calling the variables to be changed in the second argument of Manipulate[]? $\endgroup$
    – Feyre
    Jul 8, 2016 at 12:51
  • $\begingroup$ Would that be the correct way to do it? I'm new at this, so I'm just learning as I'm going. $\endgroup$
    – rxc370
    Jul 8, 2016 at 13:01
  • $\begingroup$ I just looked up Manipulate. I'm a bit confused about what you mean. Can you explain it a bit more? $\endgroup$
    – rxc370
    Jul 8, 2016 at 13:02

2 Answers 2

1
$\begingroup$

There's no need for all this dynamic stuff, you can use Solve[] in conjunction with Manipulate[], and let Manipulate[] handle the dynamic stuff.

Manipulate[
 h = x /. Quiet@
    Solve[2 == 
       x*Cos[θ] - (p*x^2)/(6*c*d) (3 f + x) Sin[θ], x, 
      Reals][[1]]; 
 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[θ] + 
      h*Sin[θ] + (p*h^2)/(6*c*d) (3 f + h) Cos[θ] + 
      i}, {x, 0, h}, Axes -> True]}], {ρ, 0, 1, 0.1}, {c, 0.2, 2,
   0.2}, {d, 0.2, 2, 0.2}, {f, 0, 2, 0.2}, {p, 0, 2, 0.2}, {θ, 
  0, π, 0.1 π}, {i, -2, 2, 0.5},ControlType -> InputField]
$\endgroup$
4
  • $\begingroup$ Thanks, but this still isn't working the way it should be. The end of the curve isn't intersecting with the corner of the box. Also, what is the [Rho] for? And is there anyway to get InputFields back? Thanks again! $\endgroup$
    – rxc370
    Jul 8, 2016 at 13:25
  • 1
    $\begingroup$ @rxc370 I cannot account for the validity of your expressions, this code changes the value of h dynamically as you asked. As for the inputfield, just add ControlType -> InputField to the Manipulate[]. I mistook p for rho. $\endgroup$
    – Feyre
    Jul 8, 2016 at 13:28
  • $\begingroup$ Sorry, last question. If I wanted to output the value of a variable as text somewhere, how would I do it? Before, I just made it a dynamic InputField. $\endgroup$
    – rxc370
    Jul 8, 2016 at 13:49
  • 1
    $\begingroup$ @rxc370 Text[] is a graphics directive $\endgroup$
    – Feyre
    Jul 8, 2016 at 13:57
0
$\begingroup$

Got everything working the way I want it to. Here is the code below if anyone ever wants to model a cantilever bending on a box.

Manipulate[h = x /. Quiet@Solve[k == x*Cos[\[Theta]] - (p*x^2)/(6*c*d) (3 f + x) Sin[\[Theta]], x, Reals][[1]];
 a = x /.Quiet@Solve[l == x*Cos[\[Theta]] - (p*x^2)/(6*c*d) (3 f + x) Sin[\[Theta]], x, Reals][[1]];
 b = a*Sin[\[Theta]] + (p*a^2)/(6*c*d) (3 f + a) Cos[\[Theta]] - h*Sin[\[Theta]] - (p*h^2)/(6*c*d) (3 f + h) Cos[\[Theta]];
 Text[ h, b]
  Show[{Graphics[{Opacity[0.5], Red, Rectangle[{j, 0}, {k, i}]},
     PlotRange -> {{-1, 2}, {-3, 3}},
     Axes -> True, AxesOrigin -> {0, 0}],
    ParametricPlot[{x*Cos[\[Theta]] - (p*x^2)/(6*c*d) (3 f + x) Sin[\[Theta]],
      x*Sin[\[Theta]] + (p*x^2)/(6*c*d) (3 f + x) Cos[\[Theta]] - h*Sin[\[Theta]] - (p*h^2)/(6*c*d) (3 f + h) Cos[\[Theta]] + i}, {x, 0, h}, Axes -> True]}],
 {p, 0, 1, ControlType -> InputField}, {{c, 1}, 0, 10, ControlType -> InputField},
 {{d, 1}, 0, 10, ControlType -> InputField}, {f, 0, 10, ControlType -> InputField},
 {{\[Theta], 0}, -2 \[Pi], 2 \[Pi], ControlType -> InputField}, {{i, 1}, 0, 2, ControlType -> InputField}, 
 {{j, 1}, 0, 2, ControlType -> InputField}, {{k, 2}, 0, 2, ControlType -> InputField}, 
 {{l, 0}, , 0.2, 2, ControlType -> InputField}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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