Group Abstract Group Abstract

Message Boards Message Boards

Models where the variable is 'hid' in the function

GROUPS:
hello[hh_] := there*hh
Manipulate[Plot[hello[hh], {hh, 1, 5}], {there, 1, 3}]
Hi, how would I get the above code to work? If I was to define there=1 at the begining I could have a simple plot. However, I would like to be able to have a slider on the plot enabling me to change the value of 'there' and see the effects on the plot. Could anyone help please?
POSTED BY: Luke Foley
Answer
1 year ago
Perhaps the best approach is to redefine hello to include there (how's that for a gramattically interesting sentence??).
Also, if you want the Manipulate to retain the definition of hello if you close and then reopen the notebook in a new Mathematica session (or in the CDFPlayer) you should use the SaveDefinitions option.
hello[hh_, there_] := there*hh;

Manipulate[
Plot[hello[hh, there], {hh, 1, 5}], {there, 1, 3},
SaveDefinitions->True]
POSTED BY: David Reiss
Answer
1 year ago
 unitflux = 2.07*10^-15
 BI[a_] := (2*unitflux)/(Sqrt[3]*(a)^2)
 
 NU[m_, n_] := m*m + m*n + n*n
 BK[m_, n_, a_] := (2 - BI[a])*((-1)^
   NU[m, n])*(3^0.75/(2*\[Pi]))/(NU[m, n])^1.5
 K[m_, n_,
   a_] := ((2*\[Pi])/(a*(Sqrt[3]*a)/2)) {m*(Sqrt[3]*a)/2,
    n*a - m*a*0.5}
B[r_,a_] := BI[a] + (2 - BI[a])*
   Sum[Sum[BK[m, n, a]*Cos[K[m, n, a]*r], {m, 8}], {n, 8}]
Manipulate[Plot[B[{r, a}], {r, 0, 3*10^-9}], {a, 5*10^-10, 15*10^-10}]
Haha, thanks. Unfortunately my real code is a bit more complicated and for some reason that method doesn't work. It works when I define a =1*10^-9 at the beginning but I can't  seem to get it to work where 'a' can vary. Either like above or with ContourPlot[B[{x,y}],{x, 0, 6*10^-9}, {y, 0, 6*10^-9}]. Any ideas?
POSTED BY: Luke Foley
Answer
1 year ago
You need to use 
Plot[B[r, a], {r, 0, 3*10^-9}]
 rather than
Plot[B[{r, a}], {r, 0, 3*10^-9}]
 Since 
B[r_,a_]

is the pattern that you defined, not
B[{r_,a_}]

Also, just some advice.  Choose your function and parameter names as things that (a) are longer than simple things like K or B and (b) double check that any function names that you define do not conflict with ones that are reserved Mathematica names.  You will notice from the syntax coloring that K is reserved by Mathematica.
POSTED BY: David Reiss
Answer
1 year ago
By the way, your code above will work much faster if you evaluate the sums before execution.  Each time Sum is executed for each call of your function B[r_,a_] Mathematica attempts to do various symbolic analyses of the sum to see if it is expressable in a simple form.  You are doing only numerical evaluations so there is no need to have Mathematica do more gymnastics than needed. 

One way to make this happen is to do something like
(B[r_, a_] := BI[a] + (2 - BI[a])*#) &[
Sum[BK[m, n, a]*Cos[K[m, n, a]*r], {m, 8}, {n, 8}]]
POSTED BY: David Reiss
Answer
1 year ago
Brilliant! Thank you!! I've changed everything and it works and is a lot faster now!
Manipulate[
ContourPlot[
  Magfield[{x, y}, a], {x, 0, 3*10^-9}, {y, 0, 3*10^-9}], {a,
  5*10^-10, 15*10^-10}]
One quick question, does it make sense to change to a contour plot and replace 'r' with {x,y} as r is the radius? It seems to work and gives roughly what I expected. (In the code above - Magfield was called 'B' before)
POSTED BY: Luke Foley
Answer
1 year ago