I have a really big vector (nearly 30,000 elements) called x and am trying to numerically find the root of a function where one of the parameters successively takes each value stored in the vector. At the moment, I'm using a For loop (I know a lot of people here cringe at the very mention of For loops for some reason; sorry):
For[j=1,j<=Length[x],j++,
f = SOME REALLY COMPLICATED FUNCTION OF t AND x[[j]];
sol[[j]]=t/.FindRoot[f,{t,t0}];
Clear[t];
];
Print[sol];
I'm wondering whether there's a more efficient way of doing this (I'm talking code-wise, not things like some simple approximation to f which gives an acceptably small error). Currently, my code takes just over 5 minutes to run (I've got a few more operations before and after the numerical bit, but they don't take long, so perhaps the numerical bit takes 4-5 minutes). I've tried setting it up as a vector operation (see code below), but after about 10 minutes of evaluation I quit the kernel because it was clear to me that that wasn't going to be more efficient than the For loop. Here's my vector-operation code:
f = SOME REALLY COMPLICATED FUNCTION OF t AND x;
sol=t/.FindRoot[f,{t,t0}]
Print[sol];
Thanks in advance.
sol=ParallelTable[t/.FindRoot[SOME REALLY COMPLICATED FUNCTION OF t AND x[[j]],{t,t0}],{j,Length[x]}]. (c) ConsiderCompile. – yohbs May 23 '17 at 15:47FindRootthat might improve its performance. That is almost certainly where the bottleneck lies. – Daniel Lichtblau May 23 '17 at 16:03xvector are not far off from each other, use the lasttas the start point for the nextFindRootiteration. – MikeY May 23 '17 at 16:25t0. Is there anything else I can do to optimise it? – Rain May 23 '17 at 17:15t0(which is about 1.42), so I'm not sure setting each value oftas the starting point for the nextFindRootwould make a big difference. – Rain May 23 '17 at 17:20Compile(link) does, as its name suggests, compilation. It takes a series of mathematica commands and compiles them to machine language which is more efficient in terms of running time. Two things: If this will do anything, it will probably mainly accelerate theCOMPLICATED FUNCTIONand notFindRoot, so if the bottleneck is inFindRootI wouldn't bother. Second, not all mathematica's commands are compilable so this might not work for you. – yohbs May 23 '17 at 18:29f, you could recast the problem as an ODE, and then the output ofNDSolvewill give you an interpolating function that can be used on your vector. – Carl Woll May 23 '17 at 19:33FindRoot, so no point usingCompile, then, but it's good to know there's that option. – Rain May 24 '17 at 17:19