2
$\begingroup$

Taking my first steps in the world of Mathematica Programming.

The aim of my program was to modify Wolfram's dynamic calculator
(which performs an addition subtraction and a product - in response to the user inputting two values used to perform these operations)
to now take three input and display on the three outputs:

[1] the quadratic equation to be solved
[2] the roots of this quadratic equation
[3] the discriminant of this equation

The three things (in red text) which you have to input are:

first input box is coefficient a
second input box is coefficient b
third input box is coefficient c

These coefficients represent a,b and c in the quadratic equation $ax^2 + bx +c$

So the Wolfram Demonstration & My attempt (the code below) look like this: http://www.controlmanchester.com/2013/08/01/quadratic-equation-images/

The code for the original program is available here: http://reference.wolfram.com/mathematica/example/ConstructADynamicCalculator.html

This is the code that I have written (modified really):

 DynamicModule[ 
      {a = 0, b = 0, c = 0}, 
      Deploy[ 
       Style[ 
        Panel[ 
         Grid[ 
          Transpose[
        {
        {
        Style["input a ", Red],
        Style["input b", Red],
        Style["input c", Red],
    "here is the equation",
    "here is the value of the discriminant",
    "here are the roots"},

     {InputField[Dynamic[a], Number],
     InputField[Dynamic[b], Number],
     InputField[Dynamic[c], Number],
     InputField[Dynamic[a*x^2 + b*x + c],Enabled -> False],
     InputField[Dynamic[b]^2 - (4*Dynamic[a]*Dynamic[c]) , Enabled -> False],
     InputField[Dynamic[a b], Enabled -> False]}
            }
                ]
    , Alignment -> Right]
    , ImageMargins -> 10]
       , 
    DefaultOptions -> {InputField -> {ContinuousAction -> True, 
    FieldSize -> {{5, 30}, {1, Infinity}}}}]
    ]
    ]

I have discovered, via trial and error, that within an input field I cannot simply try something like
Solve[x^2 + a x + 1 == 0, x] or Roots[ ].

I think this is where the heart of my problem lies.

At the moment the variables I put in do propagate down and show the coefficients of the quadratic, but I cannot get any further with it.

Any help, or guidance to things I should read are most welcome.

Many Thanks, David

p.s. I have seen from the answer by Mike Honeychurch that I have not described the problem as clearly as possible. I have edited the post :) I hope this makes my problem easier to understand

$\endgroup$
  • 4
    $\begingroup$ "First steps in the world of Mathematica Programming" and "DynamicModule..." — yikes! :) $\endgroup$ – rm -rf Aug 1 '13 at 19:48
  • $\begingroup$ I think Nasser is right - Manipulate is a better place to start than DynamicModule $\endgroup$ – cormullion Aug 2 '13 at 8:31
3
$\begingroup$

My first thought was this:

Manipulate[
 Grid[
  {
   {TraditionalForm[u]},
   {"Equation", Row[{u, " = 0"}]},
   {"Solutions", Solve[u == 0, x]},
   {"Roots", Roots[u == 0, x]},
   {"Discriminant", Discriminant[u, x]}
   }, Alignment -> Left], 
 {{u, x^2 + 2 x + 1}},
 BaseStyle -> FontSize -> 16]

equationsolver

$\endgroup$
  • $\begingroup$ +1 this is exactly what I was trying to write but in far less code. I have learned something about the complexities of dynamic vs manipulate. Thanks. $\endgroup$ – AugustCrawl Aug 2 '13 at 14:26
  • $\begingroup$ :) Cool, and thanks. Even experienced users mutter under their breath about the complexities of Dynamic and DynamicModule... $\endgroup$ – cormullion Aug 2 '13 at 14:34
2
$\begingroup$

Sorry but it is unclear to me what you want to be able to type in the input fields and what you want to see. If you use InputField[expr, Hold[Expression]] you can type Solve[...]. Here is an example (which is why I have made this an "answer" --though it is not really an answer 'cause I am unsure what you want -- rather than a comment).

DynamicModule[{z = Null},

 Column[{

   InputField[Dynamic[z], Hold[Expression]],
   Dynamic[ReleaseHold[z]]

   }]
 ]

enter image description here

or

DynamicModule[{z = Null},

 Column[{

   InputField[Dynamic[z], Hold[Expression]],
   Spacer[{0, 15}],
   Dynamic[
    If[Head[ReleaseHold[z]] === Equal, Solve[ReleaseHold[z], x], 
     Spacer[0]]]

   }]
 ]

enter image description here

$\endgroup$
  • $\begingroup$ Thank you for your Answer Mike. I have modified the question to make it more readable. I have also linked to a website I administrate where you can see images of both the output of the wolfram example and of the one I have written. @MikeHoneychurch $\endgroup$ – AugustCrawl Aug 1 '13 at 23:05

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.