0
$\begingroup$

I'm looking for a Mathematica solution to generate (a lot of) student questions for interpretation of a straight line graph, i.e. an image of a straight line where the student should find the expression y=kx+m given the image.

Is it possible to have Mathematica to loop over say n=100 iterations and for each cycle:

  1. Generate two random numbers, k and m, (let's make it integer on [-10,10] for simplicity)
  2. Define a function y=kx+m
  3. Plot y (with fixed, uniform, dimensions so all graphs have the same 'look')
  4. Save image to disk
  5. Append the answer (y=kx+m, e.g. 'y=-3x+5') to a list

and at the end save the 'answer list' to disc?

TIA

Follow up (#2). After some testing

Clear[y, x, k, l]
For[
 i = 1, i < 3, i++,
 k = RandomInteger[{-5, 5}];
 l = RandomInteger[{-5, 5}];
 y[x_] := k x + l;
 Print [y[x]];
 H = Plot[y[x], {x, -3, 3}];
 ImageFileName =
  "/Users/...<some-path>.../Downloads/test"  <> ToString[i] <> ".pdf"; 
 Export[ImageFileName, H, "PDF"];
 ]

Is the syntax good?

$\endgroup$
  • $\begingroup$ Thanks, I tried to start easy and I do get y(x) printed on screen but the Plot function does not seem to display. (I would like to add my code there, but I don't know how to. Found out how to add code to original post, sorry.) $\endgroup$ – mf67 Jul 26 '18 at 10:15
  • $\begingroup$ Take a look at Table and Map instead of trying a For loop. Table will return a list of results that you can save into a variable for later export, printing etc. $\endgroup$ – MarcoB Jul 26 '18 at 13:02
0
$\begingroup$

This should give you a fairly good template:

Define a function that takes an ID and returns a suitably randomized graph of a line that includes some clearly labelled points.

lineProblemGeneratorPointsGiven[problemIDNum_] := Block[
    {m, k, visibleDomain, shownXVals, shownPoints},
    m = RandomChoice[Join[Range[-5, -1], Range[1, 5]]];
    k = RandomInteger[{-5, 5}];
    lineEq[x_] := m x + k;
    visibleDomain = Clip[Sort[{(-20 - k)/m, (20 - k)/m}], {-20, 20}];
    shownXVals = RandomSample[Range @@ IntegerPart[visibleDomain], 2];
    shownPoints = ({#, lineEq[#]} & /@ shownXVals);
    graphicForStudent = Plot[
        lineEq[x], 
        {x, -20, 20}, 
        PlotRange -> {{-20, 20}, {-20, 20}},
        Frame -> True, 
        FrameStyle -> Bold, 
        GridLines -> Transpose[shownPoints], 
        Epilog -> {
            PointSize[0.015], Red, Point[shownPoints], 
            Black, Text[Style[ToString[problemIDNum], 30], {-19.5, 17}, {-1, 0}]
        }, 
        FrameTicks -> {{Transpose[shownPoints][[2]], None}, {Transpose[shownPoints][[1]], None}}
     ];
    {Rasterize[graphicForStudent, RasterSize -> 500], problemIDNum, m, k}
]

Now use it to generate a batch of problems:

my5Problems = lineProblemGeneratorPointsGiven /@ Range[5];

Now you want to export this so you have a folder of images and a spreadsheet of answers, so go ahead and make some directory lineProblemStorage. Include inside that one some directory imageStorage.

 linePicsWithID = #[[1 ;; 2]] & /@ my5Problems;
 Export[FileNameJoin[{lineProblemStorage, imageStorage, ToString[#[[2]]] <> ".jpg"}], #[[1]]] & /@ linePicsWithID

Now imageStorage contains the images to be given to the students. You need your answer sheet, though.

myAnswers = Join[{{"ID", "m", "k"}}, #[[2 ;; 4]] & /@ my5Problems];
Export[FileNameJoin[{lineProblemStorage, "answers.xls"}], myAnswers]

I seem to remember tests being averse to giving students labelled points when reading from graphs in order to prepare them for the real world where graphs are only used to communicate numerical minutiae of the function in question rather than to eyeball a solution for sanity or present themes and trends./s

If you want to be cruel and have your students strain their eyes and second guess themselves finding points that fall on the grid, you can use this generating function instead:

lineProblemGeneratorGridLines[problemIDNum_] := Block[
    {m, k, visibleDomain, shownXVals, shownPoints},
    m = RandomChoice[Join[Range[-5, -1], Range[1, 5]]];
    k = RandomInteger[{-5, 5}];
    lineEq[x_] := m x + k;
    graphicForStudent = Plot[
        lineEq[x], 
        {x, -20, 20}, 
        PlotRange -> {{-20, 20}, {-20, 20}},
        Frame -> True, 
        FrameStyle -> Bold, 
        GridLines -> {Range[-19, 19], Range[-19, 19]} , 
        Epilog -> {Black, Text[Style[ToString[problemIDNum], 30], {-19.5, 17}, {-1, 0}]}
     ];
     {Rasterize[graphicForStudent, RasterSize -> 500], problemIDNum, m, k}
  ]
$\endgroup$

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.