Obtaining accurate derivatives using Symbolic Math Toolbox
10 views (last 30 days)
Show older comments
Alexander Schulze-Hulbe
on 30 Mar 2021
Commented: Alexander Schulze-Hulbe
on 8 Apr 2021
Hi all
I'm coding a model that requires extensive partial differentiation (first and second-order derivatives). I decided to try using Symbolic Math Toolbox for the partial derivatives. I used the following methodology:
1) Use the diff() function to determine derivatives
2) Use subs() to determine the value of the derivative at a certain point
3) Use double() to convert symbolic values to numerical ones that can be used in subsequent calculations
In order to verify that round-off error or internal cancellation don't reduce the accuracy of my derivatives obtained with steps 1 -3, I checked my results against numerical derivatives (formulae for numerical first and second partial derivatives w.r.t. x and y provided at https://en.wikipedia.org/wiki/Finite_difference under the heading "Multivariate finite differences". h=k=0.00001 used).
I found that the % difference between some of the "analytical" derivatives obtained using Symbolic Math Toolbox (steps 1-3) and the checks (numerical derivatives) is at times not satisfactory (up to 0.0076%, would be preferable if this difference were at least 2 orders of magnitude smaller).
How do I increase the agreement between the numerical and the "analytical" derivatives?
I have tried using digits(300) to increase the precision used in calculations. I've also used vpa(...,300) to prevent truncation errors during differentiation.
Is it a fool's errand to try to get the numerical and the "analytical" derivatives to match up to a greater extent?
Thanks in advance.
Alex
0 Comments
Accepted Answer
Andreas Apostolatos
on 30 Mar 2021
Hello,
The evaluations of symbolic expressions that represent analytic derivatives in MATLAB are only governed by round-off errors that might occur during the evaluations. The 'vpa' functionality can provide you with higher accuracy in terms of the round-off errors of the corresponding evaluations. Therefore, only the round-off errors play a role when evaluating a symbolic expression in MATLAB.
On the contrary, when computing a derivative numerically, e.g., using the finite difference method, then the solution accuracy depends primarily on the discretization parameters, in your case 'h' and 'k', and decreasing those should result in principle into a better approximation of the corresponding derivative. Of course, the round-off errors come into the play in this case as well, and which of the two (discretization vs round-off error) is more significant depends solely on the level of both errors.
Having said that, I would expect that you can get a better agreement than 0.0076% between the analytical and the discretized expression of the underlying derivative, if you would decrease the step sizes 'h' and 'k', if the value of your derivatives is not that high such that round-off errors would be significant at the level of 0.0076%. However, there will certainly be a point when decreasing the step sizes, where the round-off error of the computations is more significant than the discretization error, but I would expect this to occur in a much lower error level than 0.0076%, provided that the actual value of the derivative is not very high.
I hope that this helps with the understanding.
Kind Regards,
Andreas
11 Comments
Andreas Apostolatos
on 8 Apr 2021
Hello Alex,
Indeed it is better to have this question posted in a separate thread in order to have each issue addressed in the appropriate channel for the sake of clarity.
Since you are going to repost this question, please provide a link to the new post herein for the sake of completeness.
Thanks.
Kind Regards,
Andreas
More Answers (0)
See Also
Categories
Find more on Assumptions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!