1
$\begingroup$

How can I make a printable list with different quadratic functions and with points that belong or not to them so that a young person can check whether or not the point belongs to the parabola? my code is horrible (I can't get it not to take the number zero)

something like the image

sig := 2*Random[Integer, {0, 1}] - 1;
a := Random[Integer, {-9, 9}];
b := Random[Integer, {-9, 9}];
c := Random[Integer, {-9, 9}]
nul := Random[Integer, {0, 1}] - 1;

For[i = 1, i <= 30, i++, 
 Print[i, " f(x) = ", sig*a, "x^2", nul*sig*b, "x ", nul*sig*c]]

enter image description here

$\endgroup$
4
  • $\begingroup$ Sorry for the fact that this isn't a full answer, but you will likely find it far easier in mathematica to use Table or Array rather than For loops—For loops are unFortunate even for procedural programming, but especially so for building lists. You should also avoid Print, and instead simply apply formatting functions to your output—I suspect Grid in this case might be part of what you want. Also check out Row and the like, which can be nested inside Grid. $\endgroup$
    – thorimur
    Commented Sep 6, 2021 at 0:22
  • $\begingroup$ Also: do you want just one point per function? $\endgroup$
    – thorimur
    Commented Sep 6, 2021 at 0:32
  • $\begingroup$ Thank you for answering, if random quadratic functions and one valid or wrong point per random function, the idea is that the young person verifies the membership of the points to the different functions. $\endgroup$
    – zeros
    Commented Sep 6, 2021 at 1:00
  • $\begingroup$ You might be able to reuse some of the code from this random problem generator. $\endgroup$ Commented Sep 6, 2021 at 1:28

1 Answer 1

1
$\begingroup$

Here's an answer; there could be some cleanup done, and I need to explain why some of the auxiliary functions work. But here you go—I tried to explain how it works in inline comments along the way, so that you can simply copy-paste the whole code block and run it.

(* Given a list of coefficents {a, b, c}, make a polynomial
 function—this can be done with FromDigits. *)

makePolynomial[coeffs_List] := Evaluate[FromDigits[coeffs, #]] &

(* Example: makePolynomial[{1, 2, 3}] ==> 3 + 2 #1 + #1^2 & *)

(* Format polynomials as a function definition,
using fname and var for the lhs: *)

polynomialFormat[fname_, var_][f_] := 
 With[{poly = Reverse@Inactivate[f[var], Plus]}, 
  HoldForm[fname[var] = poly]]

(* Example: polynomialFormat[f, x][makePolynomial[{1, 2, 3}]] ==>
   f[x] = x^2 + 2 x + 3 *)
(* We expect to apply TraditionalForm to this to make it look nicer. *)

(* Format a 2D point as a tuple: *)

pointFormat[{x_, y_}] := Row[{"(", x, ", ", y, ")"}]

(* Again, we expect to apply TraditionalForm. *)

(* If the first argument is True, get a point from the
graph of the function; if false, get one definitely not from the graph. *)

ifGetPoint[xrange_ : {-9, 9}, yrange_ : {-25, 25}][bool_, f_] :=
 With[{x = RandomInteger[xrange]},
  If[bool,
   {x, f[x]},
   Block[{y = RandomInteger[yrange]},
    While[y == f[x], y = RandomInteger[yrange]]; {x, y}]
   ]
  ]

(* Ok, main function. We allow parameters for the range of 
coefficients, the percentage of answers which should be 
true, and the range in which to select a "false" point. 
All but the number of rows requested can be omitted and
will take on default values.*)

ParabolaPointTableBasic[numrows_Integer?Positive, 
  coefficientrange : (_Integer?Positive) : 9, 
  truepercentage : (_?(0 <= # <= 1 &)) : 0.5,
  xrange : {_Integer, _Integer} : {-9, 9}, 
  yrange : {_Integer, _Integer} : {-25, 25}] :=
 (* First, make sure x is "fresh" for the duration of 
    this evaluation, and that any external definition of x 
    or f, e.g. x = 5, will not affect us. *)
 Block[{f, x},
  (* "With" is a way of saying that symbols explicitly used 
     in the following expression should be repalced with 
     some value (whatever we set in the following list. *)
  With[
   (* First, let's get a list with about truepercentage*
   numrows values of True, and the rest False. 
   We'll do this by asking if each integer between 1 and numrows is \
below truepercentage*numrows, 
   then mixing that list up with RandomSample. *)
   {truelist = 
     RandomSample@Table[i <= truepercentage*numrows, {i, numrows}],
    (* Next, we'll get a list of polynomial functions. 
    We generate a random list of three coefficient ranges, 
    and if the leading coefficient is 0, we change it to 1,
    then apply makePolynomial. *)
    
    polys = 
     Table[
      makePolynomial@
       Replace[{0, b_, c_} :> {1, b, c}]@
        RandomInteger[{-coefficientrange, coefficientrange}, 
         3], {numrows}]},
   (* We need a new "With", since we're going to now
      use the variables defined in the previous one. *)
   With[
    (* Get a point in the parabola if the corresponding 
       element of truelist is True, and a point not in the 
       parabola if it's false. *)
    {points = 
      MapThread[ifGetPoint[xrange, yrange], {truelist, polys}]},
    (* store the "answer key" in global variable answerKey *)
    answerKey = Transpose[{polys, points, truelist}];
    (* Put it all together. We have two lists of equal length; 
    that's three rows. We need a third list of just blank space. 
    We need to flip it to turn those rows into columns, 
    so we use Transpose. 
    We also need to format the polynomials and points, 
    and call them f, 
    so we map those formatting functions over the lists. 
    Also, prepend the headers. 
    Wrap the whole thing in Grid and give it appropriate options. *)
    TraditionalForm@Grid[
      Prepend[
        Text /@ {"Quadratic Function", "Point", 
          "Put T if the point belongs \n to the graph of the \n \
function and F if not."}]@
       Transpose[{polynomialFormat[f, x] /@ polys, 
         pointFormat /@ points, Table["   ", {numrows}]}],
      Frame -> All,
      Alignment -> {{Left, Center, Center}, {Center, 
       {Automatic}}, {1, 1} -> {Center, Center}}]
    ]
   ]
  ]

(* Test/example call: *)

ParabolaPointTableBasic[9]

(* Out: *)

A table like the one in the original.

Validate the answers that have been stored in answerKey:

validAnswer[{f_, {x_, y_}, answer : (True | False)}] :=
  (f[x] == y) == answer

AllTrue[answerKey, validAnswer]

(* Out (should be): True *)

Improvements we could make:

  • Give the optional arguments as Options instead of default arguments

  • Remove grid from around header elements

  • ✓ Align $f(x)=$ to the left of each cell [added Alignment -> {{Left, Center, Center}, {Center,{Automatic}}, {1, 1} -> {Center, Center}} to Grid]

  • Generate values that are in the function range (considered over the xrange) instead of a predetermined range, so that they seem more realistic

  • ✓ Store the "answer key" as well as the computational content in a global variable somewhere for the instructor [declared variable answerKey]

  • Make storing the answer key optional and controlled by an option

  • ✓ Include way of validating the answer key [via validAnswer for a single triple of {function, point, answer}, and then AllTrue[answerKey, validAnswer]]

  • Make it interactive, so that someone could choose True or False in the actual mathematica notebook

  • Provide an "Export" button for exporting it as a given filetype

  • Provide manipulation options for individual elements/the ability to "refresh" individual rows

  • Include optional numbering of rows along the side


Hope this helps; it was fun! Let me know if there's anything you'd like me to explain or change.

$\endgroup$
2
  • $\begingroup$ @thorimur_wow You have read my mind, besides I learn a lot of new things with the comments of your code. If you have a little time you could implement all those things you tell me, it would be very helpful for me (I do not know very well how it works to accept the answer, I think it closes, but you have my ticket won by far, I do not speak English only use translator). $\endgroup$
    – zeros
    Commented Sep 7, 2021 at 1:30
  • $\begingroup$ I hadn't noticed, if you can fix the double signs in the parabolas in some of them. $\endgroup$
    – zeros
    Commented Sep 7, 2021 at 3:12

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.