# Create a dynamically updating function

Posted 9 years ago
5587 Views
|
6 Replies
|
1 Total Likes
|
 I am attempted to create a function with will be dynamically update when I input the initial parameters in the InputField space, but it doesn't work, I misunderstand something here: InputField[Dynamic[n]] Dynamic@If[NumberQ[n], A = Table[c, {i, 3 n} ]; Grid[{Table[With[{i = i}, InputField[Dynamic[A[[i]]]]], {i, 3 n}]}]] Dynamic@If[NumberQ[n], f[t_] := Sum[A[[3 i - 2]] PDF[NormalDistribution[A[[3 i - 1]], A[[3 i]]], t], {i, n}] ]; Then I execute f[2] and get nothing. Any ideas?
6 Replies
Sort By:
Posted 9 years ago
 In[1]:= Attributes[Sum] Out[1]= {HoldAll, Protected, ReadProtected} 
Anonymous User
Anonymous User
Posted 9 years ago
 Thanks for explanation. No i see the problem. I am not sure Sum has attribute Hold: this code seems to work: Dynamic@If[NumberQ[n], A = Table[c, {i, 3 n} ]; Grid[{Table[With[{i = i}, InputField[Dynamic[A[[i]]]]], {i, 3 n}]}]] Dynamic@(f = Sum[A[[3 i - 2]] PDF[NormalDistribution[A[[3 i - 1]], A[[3 i]]], t], {i, n}]) Dynamic@Plot[f, {t, 0, 10}] I used c just a sumbol to declare a matrix. Is there a better way?
Posted 9 years ago
 What is c? Assuming you want n and c numeric I cooked up something simple: DynamicModule[{n = 2, c = 1}, Column[{ nin@Dynamic[n], Dynamic[If[NumberQ[n], A = Table[c, {i, 3 n}]; Grid[{Table[With[{i = i}, nin[Dynamic[A[[i]]]]], {i, 3 n}]}]], If[NumberQ[n], With[{n = n}, f[t_] := Sum[A[[3 i - 2]] PDF[ NormalDistribution[A[[3 i - 1]], A[[3 i]]], t], {i, n}]]]]}], Initialization :> (nin = InputField[#, Number, FieldSize -> 2] &)] 
Posted 9 years ago
 I am a little confuse: "With" does or does not override the "Hold" attribute? Because this code below doesn't work: Clear[f, fn, c, n, t]; InputField[Dynamic[n]] Dynamic@If[NumberQ[n], A = Table[c, {i, 3 n} ]; Grid[{Table[ With[{i = i}, InputField[Dynamic[A[[i]]], FieldSize -> Tiny]], {i, 3 n}]}]] Dynamic@If[NumberQ[n], With[{n = n}, fn[t_] := Sum[A[[3 i - 2]] PDF[NormalDistribution[A[[3 i - 1]], A[[3 i]]], t], {i, 3 n}]]]; Dynamic@Plot[fn[t], {t, -10, 10}] 
Posted 9 years ago
 Both SetDelayed and Sum have the attribute HoldAll, also the n in your Sum is localized in the Sum and is not the n that you are changing globally --so you need to get your n into the definition via a Pure function.I believe that the following works: Dynamic@If[NumberQ[n], Function[nn, f[t_] := Sum[A[[3 i - 2]] PDF[NormalDistribution[A[[3 i - 1]], A[[3 i]]], t], {i, nn}] ][n] ] 
Posted 9 years ago
 Why is it that when put semicolon after your code, it doesn't subsequently execute the Dynamic@Plot[f[t], {t, -10, 10}] command? What is special about the semicolon?Another problem seems to be that when I use the function: Dynamic@(If[NumberQ[n], f[t_]: = Sum[A[[3 i - 2]] PDF[NormalDistribution[A[[3 i - 1]], A[[3 i]]], t], {i, n}]]) the machine is getting close to freezing, while this version: Dynamic@(If[NumberQ[n], f = Sum[A[[3 i - 2]] PDF[NormalDistribution[A[[3 i - 1]], A[[3 i]]], t], {i, n}]]) doesn't do that. Thanks.
Community posts can be styled and formatted using the Markdown syntax.