I'm working on my project for class and it's supposed to be a graph that can take functions from the user and display it along with finding the max and min of the graph. But I just don't know how to incorporate the Max and Min part in the code for the graph. Does anyone have any suggestions?

So far I have:

Panel[DynamicModule[{f = x^2 + 3}, 
  Column[{InputField[Dynamic[f]], Dynamic[Plot[f, {x, -10, 10}]]}]]]

for the interactive graph, but i don't know what to do next.

  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – Michael E2
    Dec 17 '14 at 2:13
  • $\begingroup$ possible duplicate of How to find all the local minima/maxima in a range $\endgroup$
    – Michael E2
    Dec 17 '14 at 2:15
  • $\begingroup$ Welcome to Mathematica.SE! It isn't really appropriate for us to do your homework for you, but here are some hints that might help. You can use calculus to find turning points, and show the turning point using Epilog, like this: Dynamic[Plot[f, {x, -10, 10}, Epilog -> {Red, PointSize[Large], Point[{With[{minx = First[x /. Solve[D[f, x] == 0]]}, {minx, f /. x -> minx}]}]}]]. $\endgroup$
    – Verbeia
    Dec 17 '14 at 3:01
  • $\begingroup$ I know, i just want to be put in the right direction, not just being spoon fed the information. $\endgroup$ Dec 17 '14 at 3:49
  • 1
    $\begingroup$ Don't know whether this helps (or you already realize it): the second argument to DynamicModule could be a compound expression with components separated by semicolons. So you could end the compound expression with your Column expression and begin it with as many expressions as you need to find the min and max. $\endgroup$
    – murray
    Dec 17 '14 at 4:15

This demo allows the user to set x- minimum and maximum. It uses Initialization and local variables x1 and x2 to store the values so when the notebook is reopened in another session the last saved inputs are still present.

Panel[DynamicModule[{f = x^2 + 3, x1 = -10, x2 = 10},
    Row[{"function ", InputField[Dynamic[f]]}],
    Row[{"x min =  ", InputField[Dynamic[xmin]]}],
    Row[{"x max =  ", InputField[Dynamic[xmax]]}],
    Dynamic[Plot[f, {x, x1 = xmin, x2 = xmax}]]}],
  Initialization :> (xmin = x1; xmax = x2)]]

enter image description here

  • $\begingroup$ It seems that you understood the question much better than I did.... And a minor remark on your implementation: I think you can leave out x1 and x2: DynamicModule[{f, xmin, xmax}, ..., Plot[f, {x, xmin, xmax}], ...,Initialization :> {f=x^2+3, xmin=-10, xmax=10}] $\endgroup$ Dec 17 '14 at 12:37
  • $\begingroup$ Hi Fred, if you write it the way you suggest the dynamic module does not remember the user-modified input values once the notebook is saved, closed and reopened. $\endgroup$ Dec 17 '14 at 13:12
  • $\begingroup$ Hi @Chris, yes, you are right, I overlooked that. Leave out the initialization option (that prevents the rememberance) and set the local variables as e.g. f=x^2, xmin=-1, xmax=1. Then evaluate the dynamic module and play with it. Now f, xmin and xmax are saved in the first argument of the dynamic module. $\endgroup$ Dec 17 '14 at 20:09
  • $\begingroup$ Hi Fred, yes, that's interesting. It works that way. I first looked at Initialization in this code: Making a Sample Distribution - which doesn't work without Initialization as far as I could work out. $\endgroup$ Dec 18 '14 at 10:45

Are you looking for something like this:

    Dynamic[With[{z=Minimize[{f, -10<=x<=10}, x]}, Row[{"Mimimum " ,z[[1]]," for ",z[[2]]}]]],
    Dynamic[With[{z=Maximize[{f, -10<=x<=10}, x]}, Row[{"Maximum " ,z[[1]]," for ",z[[2]]}]]]
  • $\begingroup$ I am looking for something like that, but could you just explain what was done? As you can tell i'm very much a beginner when it comes to mathematica and the language. $\endgroup$ Dec 18 '14 at 6:23
  • $\begingroup$ @Elisee. In the panel you see a column with four items, that you see displayed in the result. The first one is an input field with argument Dynamic[f]. Here the role of Dynamic is that whatever you enter in the input field, is assigned to f. The other three column elements also have a Dynamic, but their role is to display the changes in f when you used the input field. For the Plot that is evident. In the last two elements the fundamental part is Minimize/Maximize. Evaluate them separately to see how their output looks and you will understand the way I displayed that result in the Row. $\endgroup$ Dec 18 '14 at 20:01

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.