Timeline for How to pass pre-generated values to an anonymous function to speed up a fit?
Current License: CC BY-SA 3.0
8 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Apr 14, 2018 at 15:20 | comment | added | Cris Luengo | @ALC: Sorry, I saw your comment but I forgot to answer. If your integral must be recomputed during the optimization procedure, then of course you can't pre-compute the integral. The above was written assuming the integral result is a constant. You must re-generate the function handle for every new fitting problem, so that the pre-computed integral can be updated with the new problem's constants. The part of the answer under "Code Review" does recompute the integral. Those snippets are written to illustrate other things. | |
Mar 20, 2018 at 13:03 | comment | added | ALC | Dear Chris, So far I have not been able to implement the nested function solution properly. As soon as I rely on the precomputed integral value the calculus can only be done for the same independant variable. but fittype seems to not like this. I have tried to trick the thing with an if loop and putting the original code but it seems more clever than this. I am still trying to understand what I am doing wrong. As for your second suggestion: I though doing so would recalculate every time the integral, What did I miss? | |
Mar 5, 2018 at 13:48 | comment | added | Cris Luengo |
@ALC: Yes, writing it as a nested function of the function that calls fit is one way of doing it. You can then calculate the integral for all possible limits in the main function, and use that result in the zeusEquation . --- The other way of doing it is writing a function zeusEquationGenerator that computes the integral and returns a function handle to zeusEquation as its output argument. When you call fit you use the new function to create the function handle you pass into it. --- Either way you'd have what you are asking for: pre-computed integral values.
|
|
Mar 5, 2018 at 8:44 | comment | added | ALC |
Dear Cris, Thank you for your answer, I tested your version after the fourth bullet: it definitively is faster: over 200 tries my former version takes ~6 hours, your solution takes~ 3 hours. This is a great improvement but is there a way to do even better? Regarding the first part of your answer: I feel you are trying to tell me something very important but I am afraid I have a hard time understanding it. Are you suggesting that instead of saving zeusEquation in a separate file I should write it as a nested function in the same script where I call fit ?
|
|
S Mar 5, 2018 at 8:21 | history | edited | Billal Begueradj | CC BY-SA 3.0 |
i = find(u(a6,a5,x(i))~=0): typo, it does not work this way because matlab says: i is used before being defined. Correction: i = find(u(a6,a5,x)~=0)
|
S Mar 5, 2018 at 8:21 | history | suggested | ALC | CC BY-SA 3.0 |
i = find(u(a6,a5,x(i))~=0): typo, it does not work this way because matlab says: i is used before being defined. Correction: i = find(u(a6,a5,x)~=0)
|
Mar 5, 2018 at 8:17 | review | Suggested edits | |||
S Mar 5, 2018 at 8:21 | |||||
Feb 27, 2018 at 17:47 | history | answered | Cris Luengo | CC BY-SA 3.0 |