I kindly ask you if you can give me some useful suggestions to speed up my program written in Wolfram language.
For this purpose, some premises are necessary.
The authors of the article in question use Matlab in their calculations.
I reproduced figures 2, 3, 4, 5, 6, 7,8 and 9 of their article with Mathematica 12.3, and in doing so I noticed some errors in the original text that led the authors to perform a Preprint Rev1. pdf June 8, 2021 (see final quote in the Acknowledgments).
I have found that in these graphs for the tracing of Clothoid the numerical calculation of the function d (Deltafi) = d12 is fundamental, the meaning of which is well described in relations (14) and (15) reported on pages 9 ÷ 10 of the aforementioned article. Figure 5 shows an example of the graphic representation of this function.
Now I correctly reproduce fig. 5 with N = 10,000 but with unacceptable processing time (8.76 h) while the authors take only a few seconds, always with N = 10,000 points. They sent me their Matlab code "Programa que pinta la function d (Delta phi)" see the attachment "Plot function d.rtf".
Can you give me some suggestions / optimization corrections of my Mathematica code in order to speed up this numerical calculation and break it down, if possible, to a few seconds of processing as they do with Matlab?
PS: I am attaching my notebook in fig. 5, with (N = 1,000, t = 178 s) and with (N = 10,000, t = 8.76h)
Sincerely and thanks,
thanks again for your clarity of explanations.
Hi Loris, I will try to answer your two questions.
1. To explain Clear@Evaluate[Context<>"*"] first check by executing the following line in its own cell in both notebooks, yours and mine:
Context <> "*"
When having both notebooks open I did not want variables with the same names to interfere with each other. So I changed the CellContext of my notebook from default "Global" to Notebook.
Therefore the need to change the argument to Clear
2. I just replaced your explicit numbers 10 000 with the symbol i which already had the same fixed value. The reason was to make it easier to try out different values for i. Such as 500, 1000, 2000 etc. This does not change the functionality of the code since the value of i remains constant during each trial.
Thank you very much for your solution, it was very enlightening for me.
It is true that the simplest solutions are the most effective.
The time is spent during plotting. The first argument to Plot, the function(s), is evaluated for each plot point. So making the functions simple and depending only on the plot variable speeds things up.
On my machine the attached takes around 17 s with N = 10 000