I have another question on passing a list which needs to be generated dynamically. (Sorry for posting two questions in one day...) I did some search and found a similar post here, but I think it was for a fixed-length list. I hope I didn't misunderstand it...
Here is my code:
FindRootsOfResult[i_, points_] := Module[{a, c},
Result = 2 E^(2 I \[Pi] w x) (2 + E^(I \[Pi] w x)) ((-100 + 0.55 I) + w)^3 /. x -> i;
a = FindRoot[Result == 0, {w, j}] // Hold;
c = ParallelTable[{Re[(a // ReleaseHold)[[1, 2]] - 100], Im[(a // ReleaseHold)[[1, 2]]]}, {j, 100 - 6, 100 + 6, 0.001}];
c = DeleteDuplicates[c, (Abs[#1[[2]] - #2[[2]]] < 10^-2 &) || Abs[#1[[1]] - #2[[1]]] < 10^-8 &];
points = Join[points, c];
]
What I'm trying to do is to assign a value for the variable x and to pass a list into the FindRootsOfResult
function, and then compute the corresponding roots of the function of w in a certain region. Finally I get rid of duplicate roots and save the roots in the list I passed in for further calculation. Since the number of roots in the given region changes for different x, the length of the list should be dynamically changed as well. I use Join
in the last line because I'd like to assign many different x, compute the roots, and save all of them in the same list.
A simple example would be
FindRootsOfResult[1/100, ans]
and I expect ans
contains all the roots for x=1/100. However Mathematica gives me many error messages like
$RecursionLimit::reclim: Recursion depth of 256 exceeded. >>
Join::heads: Heads Join and List at positions 1 and 2 are expected to be the same. >>
and the list ans
has nothing useful inside. Then I think, OK maybe Mathematica doesn't know that ans
should be a list object, so I make ans
be an empty list and do it again:
ans = {};
FindRootsOfResult[1/100, ans]
but I still get error messages
Set::shape: Lists {} and {{-1.82878*10^-6,-0.55},{-1.81874*10^-6,-0.55},{-1.80871*10^-6,-0.55},{-1.79868*10^-6,-0.55},{-1.78866*10^-6,-0.55},{-1.77864*10^-6,-0.55},{-1.76862*10^-6,-0.55},<<38>>,{-2.06832*10^-6,-0.55},{-2.05815*10^-6,-0.55},{-2.04798*10^-6,-0.55},{-2.03781*10^-6,-0.55},{-2.02765*10^-6,-0.55},<<344>>} are not the same shape. >>
Now I really don't know why I can't pass a list as I expected. If I execute the commands in FindRootsOfResult
line by line manually, it works perfectly. I guess I don't really understand how Mathematica passes list objects. If anyone could give me a brief explanation or any previous discussion, I would so appreciate it!