3
$\begingroup$

I'm trying to create a custom interface, where I input equivalent electrical resistances which were previously measured, so I can solve a system of 8 non linear equations using FindRoot to get the actual resistances values I want (for two wheatstone bridges). My aim is to have an interface where I can easily input these measurements and obtain the values I want inside boxes, after the click of a button. I started by using GUIkit, but then learned that it was deprecated and went on using CreateDialog, and got this until now:

CreateDialog[
  Column[{Row[{TextCell["Equivalent Resistance 1: "], 
      InputField[Dynamic[req1], Number, FieldSize -> {5, 1}]}],
    Row[{TextCell["Equivalent Resistance 2: "], 
      InputField[Dynamic[req2], Number, FieldSize -> {5, 1}]}],
    Row[{TextCell["Equivalent Resistance 3: "], 
      InputField[Dynamic[req3], Number, FieldSize -> {5, 1}]}],
    Row[{TextCell["Equivalent Resistance 4: "], 
      InputField[Dynamic[req4], Number, FieldSize -> {5, 1}]}],
    Row[{TextCell["Equivalent Resistance A: "], 
      InputField[Dynamic[reqa], Number, FieldSize -> {5, 1}]}],
    Row[{TextCell["Equivalent Resistance B: "], 
      InputField[Dynamic[reqb], Number, FieldSize -> {5, 1}]}],
    Row[{TextCell["Equivalent Resistance C: "], 
      InputField[Dynamic[reqc], Number, FieldSize -> {5, 1}]}],
    Row[{TextCell["Equivalent Resistance D: "], 
      InputField[Dynamic[reqd], Number, FieldSize -> {5, 1}]}],
    Button[
     "Compute", {Clear[req1, req2, req3, req4, reqa, reqb, reqc, 
       reqd]}]
    }],
  WindowTitle -> "Wheatstone Bridges"
  ];

It doesn't do anything useful yet, but my question is: Are there any other tools I would be better off with, to do this rather than using DialogBoxes? I feel that using this method is giving me more problems than when I was using GUIkit, which felt quite intuitive. Do you recommend other softwares, like MatLab, to do these kinds of custom interfaces? Thank you for your insight.

UPDATE:

@m_Goldberg, Thank you very much for the time spent helping me! After reading the documentation I came up with the following, functional, program:

CreateDialog[
  DynamicModule[{inputs, outputs, req1, req2, req3, req4, reqa, reqb, 
    reqc, reqd, r1, r2, r3, r4, ra, rb, rc, rd, sol, sol1, sol2, sol3,
     sol4, sola, solb, solc, sold, eq1, eq2, eq3, eq4, eqa, eqb, eqc, 
    eqd, init1 = 1, init2 = 1, init3 = 1, init4 = 1, inita = 1, 
    initb = 1, initc = 1, initd = 1, unit},
   unit = Row["ola"];
   inputs = 
    MapThread[
     Row[{"Equivalent MS", #1, ": ", 
        InputField[Dynamic@#2, Number, 
         FieldSize -> {5, 1}]}] &, {{"1", "2", "3", "4", "A", "B", 
       "C", "D"}, {req1, req2, req3, req4, reqa, reqb, reqc, reqd}}];

   outputs =
    MapThread[
     Row[{"        MS", #1, ": ",
        Dynamic@
         Pane[Dynamic@#2, 100, BaseStyle -> {Background -> White}, 
          FrameMargins -> 4]}] &, {{"1", "2", "3", "4", "A", "B", "C",
        "D"}, {sol1, sol2, sol3, sol4, sola, solb, solc, sold}}
     ];

   req1 = 
    req2 = req3 = 
      req4 = reqa = 
        reqb = reqc = 
          reqd = sol1 = 
            sol2 = sol3 = sol4 = sola = solb = solc = sold = 0;

   Column[{Row[{Column[Join[inputs, {}]

        ],
       Column[outputs]

       }],
     Row[{Button[
        "Reset", {req1 = 
          req2 = req3 = 
            req4 = reqa = 
              reqb = reqc = 
                reqd = sol1 = 
                  sol2 = sol3 = sol4 = sola = solb = solc = sold = 0, 
         init1 = 1, init2 = 1, init3 = 1, init4 = 1, inita = 1, 
         initb = 1, initc = 1, initd = 1}],

       Button["Compute",
        If[
         req1 == 0 || req2 == 0 || req3 == 0 || req4 == 0 || 
          reqa == 0 || reqb == 0 || reqc == 0 || 
          reqd == 0, {sol1 = 
           sol2 = sol3 = sol4 = sola = solb = solc = sold = 0}, {


          eq1 = (r1*(((r3 + 
                    r4)*(((ra + rb)*(rc + rd))/(ra + rb + rc + 
                    rd)))/(r3 + 
                    r4 + (((ra + rb)*(rc + rd))/(ra + rb + rc + 
                    rd))) + 
                 r2))/(r1 + (((r3 + 
                    r4)*(((ra + rb)*(rc + rd))/(ra + rb + rc + 
                    rd)))/(r3 + 
                   r4 + ((ra + rb)*(rc + rd))/(ra + rb + rc + rd))) + 
               r2) == req1;

          eq2 = (r2*(((r3 + 
                    r4)*(((ra + rb)*(rc + rd))/(ra + rb + rc + 
                    rd)))/(r3 + 
                    r4 + (((ra + rb)*(rc + rd))/(ra + rb + rc + 
                    rd))) + 
                 r1))/(r2 + (((r3 + 
                    r4)*(((ra + rb)*(rc + rd))/(ra + rb + rc + 
                    rd)))/(r3 + 
                   r4 + ((ra + rb)*(rc + rd))/(ra + rb + rc + rd))) + 
               r1) == req2;

          eq3 = (r3*(((r1 + 
                    r2)*(((ra + rb)*(rc + rd))/(ra + rb + rc + 
                    rd)))/(r1 + 
                    r2 + (((ra + rb)*(rc + rd))/(ra + rb + rc + 
                    rd))) + 
                 r4))/(r3 + (((r1 + 
                    r2)*(((ra + rb)*(rc + rd))/(ra + rb + rc + 
                    rd)))/(r1 + 
                   r2 + ((ra + rb)*(rc + rd))/(ra + rb + rc + rd))) + 
               r4) == req3;

          eq4 = (r4*(((r1 + 
                    r2)*(((ra + rb)*(rc + rd))/(ra + rb + rc + 
                    rd)))/(r1 + 
                    r2 + (((ra + rb)*(rc + rd))/(ra + rb + rc + 
                    rd))) + 
                 r3))/(r4 + (((r1 + 
                    r2)*(((ra + rb)*(rc + rd))/(ra + rb + rc + 
                    rd)))/(r1 + 
                   r2 + ((ra + rb)*(rc + rd))/(ra + rb + rc + rd))) + 
               r3) == req4;

          eqa = (ra*(((rc + 
                    rd)*(((r1 + r2)*(r3 + r4))/(r1 + r2 + r3 + 
                    r4)))/(rc + 
                    rd + (((r1 + r2)*(r3 + r4))/(r1 + r2 + r3 + 
                    r4))) + 
                 rb))/(ra + (((rc + 


                    rd)*(((r1 + r2)*(r3 + r4))/(ra + rb + rc + 
                    rd)))/(rc + 
                   rd + ((r1 + r2)*(r3 + r4))/(r1 + r2 + r3 + r4))) + 
               rb) == reqa;

          eqb = (rb*(((rc + 
                    rd)*(((r1 + r2)*(r3 + r4))/(r1 + r2 + r3 + 
                    r4)))/(rc + 
                    rd + (((r1 + r2)*(r3 + r4))/(r1 + r2 + r3 + 
                    r4))) + 
                 ra))/(rb + (((rc + 
                    rd)*(((r1 + r2)*(r3 + r4))/(ra + rb + rc + 
                    rd)))/(rc + 
                   rd + ((r1 + r2)*(r3 + r4))/(r1 + r2 + r3 + r4))) + 
               ra) == reqb;

          eqc = (rc*(((ra + 
                    rb)*(((r1 + r2)*(r3 + r4))/(r1 + r2 + r3 + 
                    r4)))/(ra + 
                    rb + (((r1 + r2)*(r3 + r4))/(r1 + r2 + r3 + 
                    r4))) + 
                 rd))/(rc + (((ra + 
                    rb)*(((r1 + r2)*(r3 + r4))/(ra + rb + rc + 
                    rd)))/(ra + 
                   rb + ((r1 + r2)*(r3 + r4))/(r1 + r2 + r3 + r4))) + 
               rd) == reqc;

          eqd = (rd*(((ra + 
                    rb)*(((r1 + r2)*(r3 + r4))/(r1 + r2 + r3 + 
                    r4)))/(ra + 
                    rb + (((r1 + r2)*(r3 + r4))/(r1 + r2 + r3 + 
                    r4))) + 
                 rc))/(rd + (((ra + 
                    rb)*(((r1 + r2)*(r3 + r4))/(ra + rb + rc + 
                    rd)))/(ra + 
                   rb + ((r1 + r2)*(r3 + r4))/(r1 + r2 + r3 + r4))) + 
               rc) == reqd;


          sol = FindRoot[{eq1, eq2, eq3, eq4, eqa, eqb, eqc, 
             eqd}, {{r1, init1}, {r2, init2}, {r3, init3}, {r4, 
              init4}, {ra, inita}, {rb, initb}, {rc, initc}, {rd, 
              initd}}],
          sol1 = sol[[1, 2]], sol2 = sol[[2, 2]], sol3 = sol[[3, 2]], 
          sol4 = sol[[4, 2]], sola = sol[[5, 2]], solb = sol[[6, 2]], 
          solc = sol[[7, 2]], sold = sol[[8, 2]]



          }]],


       Button["Init", 
        CreateDialog[{TextCell[
            "Choose starting points (default = 1):"],
           Column[{

             Row[{"Init1: ", 
               InputField[Dynamic[init1], Number, FieldSize -> {5, 1}],
               "Init2: ", 
               InputField[Dynamic[init2], Number, FieldSize -> {5, 1}],
               "Init3: ", 
               InputField[Dynamic[init3], Number, FieldSize -> {5, 1}],
               "Init4: ", 
               InputField[Dynamic[init4], Number, 
                FieldSize -> {5, 1}]}],

             Row[{"InitA: ", 
               InputField[Dynamic[inita], Number, FieldSize -> {5, 1}],
               "InitB: ", 
               InputField[Dynamic[initb], Number, FieldSize -> {5, 1}],
               "InitC: ", 
               InputField[Dynamic[initc], Number, FieldSize -> {5, 1}],
               "InitD: ", 
               InputField[Dynamic[initd], Number, 
                FieldSize -> {5, 1}]}],
             Row[{DefaultButton[]}]}

            ]}];],
       CancelButton[],
       DefaultButton[DialogReturn[]]}]

     }]
   ],
  WindowTitle -> "Wheatstone Bridges (k\[CapitalOmega])"
  ];

It does what I initially explained in the introduction of this question. I'll read your new update to further improve the code. Once again, thank you very , very much!

$\endgroup$
  • $\begingroup$ What problems is this method giving you? If you don't want the popup then you can remove CreateDialog and have the UI in the notebook. $\endgroup$ – Edmund Aug 10 '17 at 2:27
  • 1
    $\begingroup$ You can use DynamicModule or Manipulate but a dialog was suggested as it was doing what your GUIKit example was doing. This is the way to go in Mathematica, you can read related tutorials (go to Manipulate documentation and scroll down to see tutorials) to understand it better. About other software, this is off topic here/option based and unclear untill you make clear what does not work as you like. $\endgroup$ – Kuba Aug 10 '17 at 6:43
  • $\begingroup$ I'm sorry for being unclear! What I meant was that I was having a much easier time learning and using GUIkit than using dialogboxes which left me wondering why the first was deprecated. Thank you so much for keeping up with my questions! $\endgroup$ – RicardoP Aug 10 '17 at 9:24
6
$\begingroup$

I can understand you might be more comfortable with GUIKit if you have done GUI development with other GUI frameworks. But in Version 6, the Mathematica front-end became a programable GUI with the introduction of the wrapper function Dynamic. This made GUI development possible for users with no previous experience with Java and no desire to learn it. It also allowed the full power of Mathematica to be applied to GUI objects.

If you have experience with conventional GUI development, you face not only a fairly steep learning curve, but also an unlearning curve -- your previous experience getting in your way of becoming comfortable with Wolfram Language's way of doing things.

An example of what I mean by applying the "full power" can be seen in this rewrite of your code. I have added quite a bit of functionality, but still reduced the size of the code.

rtn = 42; Dynamic @ rtn

CreateDialog[
  DynamicModule[{inputs, req1, req2, req3, req4, reqa, reqb, reqc, reqd, sum},
    inputs =
      MapThread[
        Row[
          {"Equivalent Resistance ", #1, ": ", 
           InputField[Dynamic @ #2, Number, FieldSize -> {5, 1}]}] &,
        {{"1", "2", "3", "4", "A", "B", "C", "D"}, 
         {req1, req2, req3, req4, reqa, reqb, reqc, reqd}}];
    sum = req1 = req2 = req3 = req4 = reqa = reqb = reqc = reqd = 0;
    Column[
      Join[
        inputs,
        {Row[
          {"Summation       ",
           Dynamic @ Pane[sum, 100,
             BaseStyle -> {Background -> White},
             FrameMargins -> 4]}],
         Row[
           {Button["Reset", 
             sum = req1 = req2 = req3 = req4 = reqa = reqb = reqc = reqd = 0],
            Button["Compute", 
              sum = Plus[req1, req2, req3, req4, reqa, reqb, reqc, reqd]],
            CancelButton[], 
            DefaultButton[DialogReturn[rtn = sum]]}]}]]],
  WindowTitle -> "Wheatstone Bridges"];

This gives a dialog window that looks like

dialog

It has full dialog functionality.

  • You can carry out a simple computation with Compute button.
  • You can reset all variables for new computation with the Reset button.
  • You can exit the dialog with no side effects with the Cancel button.
  • You can exit the dialog and update the global dynamic variable rtn with the OK button.

This could be all the GUI you need for your purposes -- you might be able to get away with just changing the actions of the Compute and Reset buttons and the label of the Summation pane. But even if that's not true, This code should move you a fair way forward.

Update

It was late at night when I wrote the above and I was tired. After a good sleep and with a refreshed mind, I had second thoughts. They weren't about the functionality of the code, but its style. I think you will find what I show below to better organized and more readable and, therefore, more easily extended and maintained.

CreateDialog[
  DynamicModule[
      {req1, req2, req3, req4, reqa, reqb, reqc, reqd, 
       reset, inputs, sum, doReset, doCompute},
    Dynamic @
      Column[
       Join[
        inputs, 
        {Row[
           {"Summation       ", 
            Pane[sum, 100, 
              BaseStyle -> {Background -> White}, 
              FrameMargins -> 4]}],
         Row[
           {Button["Reset", doReset[]], Button["Compute", doCompute[]],
            CancelButton[], DefaultButton[DialogReturn[rtn = sum]]}]}]],
    Initialization :> (
      inputs = 
        MapThread[
          Row[
            {"Equivalent Resistance ", #1, ": ", 
             InputField[Dynamic@#2, Number, 
               FieldSize -> {5, 1}]}] &, 
          {{"1", "2", "3", "4", "A", "B", "C", "D"}, 
           {req1, req2, req3, req4, reqa, reqb, reqc, reqd}}];
      doReset[] := 
        (req1 = req2 = req3 = req4 = reqa = reqb = reqc = reqd = sum = 0);
      doCompute[] := 
        (sum = Plus[req1, req2, req3, req4, reqa, reqb, reqc, reqd]);
      doReset[])],
  WindowTitle -> "Wheatstone Bridges"];

References

Wolfram Language Documentation Center articles

  • tutorial/IntroductionToDynamic
  • tutorial/IntroductionToManipulate
  • tutorial/AdvancedDynamicFunctionality
  • tutorial/AdvancedManipulateFunctionality
$\endgroup$
  • 1
    $\begingroup$ Thank you so much sir!! I'm going to sweep the Mathematica Documentation to understand your modifications on the code. It looks much "cleaner" than mine and this is an opportunity to learn more about these mathematica functions I wasn't aware about! $\endgroup$ – RicardoP Aug 10 '17 at 13:24
  • $\begingroup$ @RicardoP. I have added a better code design and some references I urge you to read. $\endgroup$ – m_goldberg Aug 10 '17 at 21:46
  • $\begingroup$ I've just posted an update, showing the code I made after reading your advices. Many symbols you used are very useful and I was completely unaware of them until now. I'll read your new update to further improve on my code. Thank you very, very much. $\endgroup$ – RicardoP Aug 10 '17 at 23:43
  • $\begingroup$ My +1 was more for the didactic parts than the code parts anyway, but it's unfortunate I can't upvote again for the update. $\endgroup$ – J. M. will be back soon Aug 11 '17 at 1:53
  • $\begingroup$ @J.M. It's the thought that counts :-) $\endgroup$ – m_goldberg Aug 11 '17 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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