Speed of vpaintegral vs. int+vpa

7 Ansichten (letzte 30 Tage)
Michael Loibl
Michael Loibl am 19 Aug. 2024
Kommentiert: Walter Roberson am 20 Aug. 2024
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)
ans = 
0.011145833333333333333333333333333
toc
Elapsed time is 1.432269 seconds.
tic
vpaintegral(vpaintegral(mon,y,0,f),x,0,0.5,'RelTol', 1e-32, 'AbsTol', 0)
ans = 
0.011145833333333333333333333333333
toc
Elapsed time is 16.967404 seconds.
I am curious whether I miss something here.
  4 Kommentare
Michael Loibl
Michael Loibl am 20 Aug. 2024
Hi,
Thank you for your explanation. Actually I was not sure whether "int" is integrating analytically here because I get the following solution to my integral:
F =
int((x*min([1/2, 5*x - 1], [], 2, 'omitnan', false)^2)/2, x, 1/5, 2/5) + 9/1600
Does it mean that it partly computes it analytically (resolving one of my "int") and partly is computing it then numerically using the "vpa" command?
Walter Roberson
Walter Roberson am 20 Aug. 2024
Yes, one of your int() is being resolved analytically, and then vpa() computes the rest numerically.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Rahul
Rahul am 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:
Hope this helps! Thanks.

Produkte


Version

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by