Skip to main content

How to make a Dynamic Calculator to solve a quadratic equation?


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




Answer



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


Comments