Speed of vpaintegral vs. int+vpa
7 views (last 30 days)
Show older comments
Dear all,
I am currently testing the possibilities of integrating symbolic functions in Matlab. In the documentation of "int" (https://de.mathworks.com/help/symbolic/sym.int.html), I found the following statement which is not in agreement with my personal observations:
"To approximate integrals directly, use vpaintegral instead of vpa. The vpaintegral function is faster and provides control over integration tolerances."
I run the following example and observe that a combination of "int" and "vpa" needs 0.35 secs vs. 47.69 secs in case of "vpaintegral" both having the same precision of 32 valid digits.
syms x y
% define integration boundary
f1 = 0;
eqn = 0.980580675690920 * (0.4-x) - 0.196116135138184 * (1-y) == 0;
f2 = solve(eqn,y);
f3 = 1;
y_max = 0.5;
f = piecewise(x<0.2, min(f1,y_max), 0.2<=x<0.4, min(f2,y_max), ...
x>=0.4, min(f3,y_max)); %#ok<CHAIN>
% define integrand
mon = x*y;
% integration over x-y-domain limited by the boundary, y=0, x=0 and x=0.5
tic
F = int(int(mon,y,0,f),x,0,0.5);
vpa(F)
toc
tic
vpaintegral(vpaintegral(mon,y,0,f),x,0,0.5,'RelTol', 1e-32, 'AbsTol', 0)
toc
I am curious whether I miss something here.
4 Comments
Walter Roberson
on 20 Aug 2024
Yes, one of your int() is being resolved analytically, and then vpa() computes the rest numerically.
Answers (1)
Rahul
on 20 Aug 2024
I understood your query and was able to reproduce the same with the help of the code provided.
You are trying to evaluate the performance benefits of using "vpaintegral" function for the computation of integral as compared to using "int" and "vpa" functions separately. The two methods use different techniques to compute results.
The "int" + "vpa" functions use symbolic methods to evaluate the results and is efficient in cases where integral is computable in closed/analytical form which are less computationally intensive.
The "vpaintegral" function uses numerical methods to solve the integral and is much more efficient in cases which are computationally intensive.
In your case, you are using "piecewise" function which returns symbolic expression or functions. As correctly mentioned by @John D'Errico, "vpaintegral" is not the ideal function to be used in this case as it is designed for numerical cases and looks at the piecewise function as a black box.
Hence in this case using the combination of "int" + "vpa" is much more efficient. If you require to use "vpaintegral" for your case, you can try adjusting the tolerance values which can make the computation more efficient.
tic
vpaintegral(vpaintegral(mon, y, 0, f), x, 0, 0.5, 'RelTol', 1e-6, 'AbsTol', 1e-10)
toc
% You can adjust the 'RelTol' and 'AbsTol' values according to your usecase
You can refer to the following documentations to know more about these functions:
"vpaintegral": https://www.mathworks.com/help/symbolic/sym.vpaintegral.html
Hope this helps! Thanks.
0 Comments
See Also
Categories
Find more on Calculus 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!