## Help with a program...

General discussion about Mathematica features and functionality...
Forum Rules
By using the Wolfram Faculty Program Forum, you agree not to post any abusive, obscene, vulgar, slanderous, hateful, threatening, or sexually oriented material. Wolfram Faculty Program Forum administrators have the right to remove, edit, move or close any topic at any time should we see fit.

Personal Information: Posts in this forum may be viewed by non-members; however, the forum prohibits non-members from viewing your profile. Although your email address is hidden from both non-members and members, your account is initially configured to allow members to contact you via email through the forum. If you wish to hide your profile, or prohibit others from contacting you directly, you may change these settings by updating your profile through the User Control Panel.

Attachments: Attachments are not currently enabled on this forum. To share a file with others on this site, simply upload your file to the online storage service of your choice and include a link to the file within your post. If your school does not offer an online file storage and sharing service, the following sites provide free basic online file storage and sharing: Mozy, FilesAnywhere, Adrive, and KeepandShare.

### Help with a program...

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

Rob_Holman

Posts: 14
Joined: Wed Feb 29, 2012 7:03 pm
Organization: The Walker School
Department: Mathematics and Science

### Re: Help with a program...

Hi Rob,

Yes, allowing open input fields like this does complicate things. However, you might be able to do this in a tricky way by using mesh functions (I found an example in the Documentation Center that I applied). Take a look at it here (it's the top example, called InputFieldIntersectionExample.nb):
http://members.wolfram.com/mmorrison/Fa ... ogram.html

Thanks,
Michael

Michael_Morrison

Posts: 42
Joined: Fri Sep 11, 2009 9:50 pm
Organization: Wolfram Research, Inc.

### Re: Help with a program...

Michael:

Thanks for the suggestion. I'll play around with it a little bit.

At first glance, the display is perfect, but I'd love to retain the tooltip functionality where hovering over the point will provide the coordinates. I'll see if I can figure out how to make that happen.

Rob

Rob_Holman

Posts: 14
Joined: Wed Feb 29, 2012 7:03 pm
Organization: The Walker School
Department: Mathematics and Science

### Re: Help with a program...

Try Manipulate...

Manipulate[
points := Solve[f == g, Reals] // N;
solutions := {x, f} /. points;
Plot[{f, g}, {x, -10, 10}, PlotRange -> {-10, 20},
Epilog -> {ColorData[1][1], PointSize[Large],
Tooltip[Point[#], #] & /@ solutions}],
{{f, Sin[x]}, InputField}, {{g, x^2}, InputField},
TrackedSymbols -> True]

Note that Solve does not always find all the points of intersection (e.g. f = Sin[x], g = x/2 +x^2/8). I shortened some of the code. In particular, 1) you used k before it was initialized, which can lead to buggy behavior. 2) The variable points is initialized to approximate reals with "//N"; all the things computed from points will already be approximate (e.g. solutions) and further "//N" are unnecessary. 3) There seemed to be no need to hang onto the functions func1 and func2; f and g can be used as above. 4) I used Map (/@) and ReplaceAll (/.) in place of your use of Table. Solve returns a List of Rules, and these functions take advantage of it. But there is nothing wrong with using Table. 5) Ditto for Epilog instead of ListPlotPoint; a matter of taste, really.

Cheers!

Michael_Rogers

Posts: 10
Joined: Mon May 24, 2010 3:28 pm
Organization: Emory University
Department: Oxford College

### Re: Help with a program...

Michael:

This is really great... exactly what I had in mind. Thank you!

Rob

Rob_Holman

Posts: 14
Joined: Wed Feb 29, 2012 7:03 pm
Organization: The Walker School
Department: Mathematics and Science