by Rob_Holman » Tue May 22, 2012 9:17 pm
Hi all:
I put together the following to find and show the intersection of two functions. What I'd LIKE to do is get the functions to be defined by input fields, so that I could make this a CDF or a widget that my students could use without having to edit the functions in the code itself. I've tried to do this a couple of different ways, and it hasn't worked. I've included my latest attempt as well.
First, the initial code:
______________________________
func1[x_] := (1/2) x^3 - 2 x;
func2[x_] := (-1/3) x^2 + 1;
points := Solve[func1[x] == func2[x], Reals];
graphplot :=
Plot[{func1[a], func2[a]}, {a, -10, 10}, PlotRange -> {-5, 20}];
solutions := Table[x /. points[[i]], {i, 1, k}];
k = Length[points];
Show[graphplot,
ListPlot[Tooltip[
Table[{solutions[[j]] // N, func1[solutions[[j]]] // N}, {j, 1,
k}]], PlotMarkers -> {Automatic, 8}]]
_________________________
Now, my most recent attempt at generating a document where the user can edit the functions without going into the code:
___________________________________________
Panel[DynamicModule[{f = Sin[x], g = x^2},
func1[x_] := Dynamic[f];
func2[x_] := Dynamic[g];
points := Solve[f == g, Reals] // N;
solutions := Table[x /. points[[i]], {i, 1, k}];
k = Length[points];
graphplot := Plot[{f, g}, {x, -10, 10}, PlotRange -> {-10, 20}];
Column[{InputField[Dynamic[f]], InputField[Dynamic[g]],
Dynamic[Show[graphplot,
ListPlot[
Tooltip[Table[{solutions[[j]] // N,
func1[solutions[[j]]] // N}, {j, 1, k}]],
PlotMarkers -> {Automatic, 14}]]]}]]]
____________________________________________
The problem I'm having is that I can't figure out how to dynamically define the two functions so that I can get them to accept input. It's easy to graph Sin[x] as a dynamically defined f, but I can't compute f[4], for example.
I fear that what I'm trying to do is more difficult than I realize, or that it requires expertise with the programming capabilities of Mathematica that I'm not quite ready to tackle yet. Anyone have a suggestion on how I could pull this off?
Thanks for the help.
Rob