I'm looking for a way to *significantly* speed up a procedure that numerically solves a non-linear equation for me.
I'm working on simulating photon paths in water (under certain conditions) using the Monte Carlo method (for those who don't know, this method is based on random number generation and probabilities, combined with the physics of your choice. Basically in my case, the idea is that for every photon I simulate, the code will generate a random number which will result in a free photon path, then another random number that will decide whether my photon is absorbed or scattered, then another random number that will decide which direction my photon takes next (if it wasn't absorbed), and repeats this loop until any such time when the photon is absorbed. The issue appears when scattering occurs, because the random number results in a scattering angle just by solving a gruesomely-long and complicated non-linear equation, that needs a numerical solution).
I was solving my equation using fminsearch at first, but the speed was such that my numerical experiments would not have finished within my lifetime... I tried to fiddle with the parameters of fminsearch (no great improvement), then I discovered fminbnd, which seems to be a bit more efficient for my needs, but still, to simulate 10,000 photons the program takes some 14 hours to run, and all in all I would need to simulate at least about 18,000,000 photons to get anywhere...
...so I need some serious help. Does anyone have any ideas how I could speed this up?
Looking forward to hearing (well, reading) your ideas,
Alina
You might want to look at other ways to speed up your code. Perhaps you
should generate a large random matrix, then read out the numbers from
that matrix, instead of generating random numbers one at a time (if that
is what you are doing). There are other ways to speed up calculations
involving "vectorizing."
Good luck,
Alan Weiss
MATLAB mathematical toolbox documentation