Message Boards Message Boards

0
|
4396 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Evaluate a function when a button is clicked in Manipulate?

I am using Mathematica 11.3. I have been wrestling with the following issue for a while and suspect I have a conceptual difficulty as I can find nothing relevant on web searches so would appreciate any assistance appreciated.

In this example with the function test defined. I am trying to get the sum of n1 and n2 when the button is pressed. I have tried multiple manipulates, dynamic,evaluate, @, @@ but at best all that prints is

test[2,3]

where 2 and 3 would be the current slider values

What follows is sample code

test[a, b] := a + b;

Manipulate[
 myPlot = Plot[Sin[n1 x] + Sin[n2 x], {x, 0, 2 Pi}, PlotRange -> 2],
 {n1, 1, 20}, {n2, 1, 20},

 Row[{Spacer[100], Button["Add n1 and n2",
    Print[Evaluate[test[n1, n2]]],  Method -> "Queued", 
    ImageSize -> 100]}]
 ]
POSTED BY: William Taylor
2 Replies

I apologise for the initial post, but it surprised me that the evaluation worked! Ok so here is a fuller code. I do get inconsistent results with this but it seems simple enough that a button should be able to execute a user defined function

relevant functions are buildCoords test2 and test 3

a typical output when each button is pressed is test 1 : buildCoords[60,60,60,60,120,0,0] test 2: test2[60,60,60,60,120,0,0] test 3: 120

buttons test 1 and test 2 display current parameter values but does not evaluate the function test 3 which is a simple calculation works

Is this a bug, or is there some initialisation issue or complexity limit

the code

fx [i, j, d, v, h, t, w, x0] := i (1 - d Sin[v]) Cos[v] Sin[h] Sin[t] - j (1 - dSin[v]) Cos[v] Cos[h] Cos[t] + wLog[2 - Sin[v]](-Sin[t] + Cos[t]) + x0;

fy [i, j, d, v, h, t, w, y0] := i (1 - d Sin[v]) Cos[v] Sin[h] Cos[t] + j (1 - dSin[v]) Cos[v] Cos[h] Sin[t] + wLog[2 - Sin[v]](Cos[t] + Sin[t]) + y0;

fz[k, v, z0_] := k (1 - Sin[v])/2 + z0;

fR[x, y, z_] := Sqrt[x^2 + y^2 + z^2];

fL[x, y] := ArcTan[y/x];

fB[x, y, z_] := ArcSin[z/Sqrt[x^2 + y^2 + z^2]];

cart2gal[FB_] := Module[ {fbD4}, fbD1 = Dataset[TableForm[<a href="mailto:InputForm@FB]]">InputForm@FB]]; fbD2 = fbD1[All, <| "x" -> 1, "y" -> 2, "z" -> 3 |>]; fbD3 = fbD2[ All, # ~Join~ <| "R" -> Sqrt[#x^2 + #y^2 + #z^2], "L" -> ArcTan[#y/#x], "B" -> ArcSin[#z/Sqrt[#x^2 + #y^2 + #z^2]] |> &]; fbD4 = fbD3; ]

test2[np, nq, i, j, k, d, t, w] := Module[{arr}, arr = {{"x", "y", "z", "R", "L", "B", "Ldeg", "Bdeg", "h", "v"}}; arr = Join[arr, {{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}}]; ]

test3[a, b] := a + b;

buildCoords[np, nq, i, j, k, d, t, w] := Module[{arr}, pi = 3.14159265358979; hstep = pi/(nq - 1); vstep = 2*hstep; xdist = 85; arr = {{"x", "y", "z", "R", "L", "B", "Ldeg", "Bdeg", "h", "v"}}; v = 0.01; h = -2; y = fy[i, j, d, v, h, t, w, 0]; z = fz[k, v, 0]; R = fR[x, y, z]; L = fL[x, y]; B = fB[x, y, z]; dL = r2d[L]; dB = r2d[B]; arr = Join[arr, {{x, y, z, R, L, B, dL, dB, h, v}}]; ]

cart[FB_] := TableForm[(InputForm@FB)[[1, 1, 1]]];

r2d[r_] := N[r*180/Pi];

Manipulate[ FB = ParametricPlot3D[{ fx [i, j, d, v, h, t, w, 0], fy [i, j, d, v, h, t, w, 0], fz[k, v, 0]}, {v, 0, 2 [Pi]}, {h, -[Pi]/2 , [Pi]/2}, ViewPoint -> Dynamic@vp, Mesh -> Automatic, MeshStyle -> mcolor, PlotStyle -> scolor, AspectRatio -> 1], Row[{Text[ "L, the y axis (horizontal) and B, the Z axis (vertical)\n are \ in approximate degrees as observed from the Sun.\nThe default \ parameter set best matches the bubble shape without the 'wind' \ effect"]}], {{i, 60, "L ~y axis (i)"}, 5, 160, Appearance -> "Labeled"}, {{j, 60, "To GC ~x axis (j)"}, 5, 160, Appearance -> "Labeled"}, {{k, 120, "B ~z axis (k)" }, 5, 160, Appearance -> "Labeled"}, {{d, 0.2, "Thinness (d)"}, 0, 1, Appearance -> "Labeled"}, {{t, 0, "xy Tilt (t)"}, -Pi/2, Pi/2, Appearance -> "Labeled"}, {{w, 0, "'Wind' factor (w)"}, -20, 20, Appearance -> "Labeled"}, {{vp, {0, -85, 0}, "View Point"}, {{5, 8, 4} -> "xyz", {0, -85, 0} -> "yz (from Sun)", {0, 0, Infinity} -> "yx", {Infinity, 0, 0} -> "xz"}, PopupMenu}, {{mcolor, RGBColor[0., 1., 0.07`], "Mesh"}, ColorSlider}, {{scolor, RGBColor[255, 72, 255], "Surface"}, ColorSlider}, Delimiter,

Row[{Spacer[100], Button["Test 1", Print[Evaluate[buildCoords[60, 60, i, j, k, t, w]]], Method -> "Queued", ImageSize -> 100]}], Row[{Spacer[100], Button["Test 2", Print[Evaluate[test2[60, 60, i, j, k, t, w]]], Method -> "Queued", ImageSize -> 100]}], Row[{Spacer[100], Button["Test 3", Print[Evaluate[test3[i, j]]], Method -> "Queued", ImageSize -> 100]}], Row[{Spacer[100], Button["Reset", FrontEndExecute[FrontEndToken["EvaluateNotebook"]], ImageSize -> 100]}] ]

POSTED BY: William Taylor

Sorry to bother I see the error here (forgot the underscores in the function definition) I will have to reformulate my request as this does not work for more complex functions in the context I am using them

Still any suggestions on evaluating two functions using current parameter values in a manipulate would be helpful

test[a_, b_] := a + b;

Manipulate[ myPlot = Plot[Sin[n1 x] + Sin[n2 x], {x, 0, 2 Pi}, PlotRange -> 2], {n1, 1, 20}, {n2, 1, 20},

Row[{Spacer[100], Button["Add n1 and n2", Print[Evaluate[test[n1, n2]]], Method -> "Queued", ImageSize -> 100]}] ]
POSTED BY: William Taylor
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract