9 views (last 30 days)
Show older comments
Lahcen on 23 Aug 2023
Moved: John D'Errico on 28 Aug 2023
Accepted Answer: Nathan Hardenberg
I want to calculate an approximate value of an improper integral:
>> fun= 4./(x.*(1-x).*(2-x).*(pi.^2+(log(x./(1-x)).^2))
>> vpa(int(fun,0,1))
then matlab displays the result:
ans =
vpaintegral(4/(x*(log(-x/(x - 1))^2 + 2778046668940015/281474976710656)*(x - 1)*(x - 2)), x, 0, 1).
how can I get the approximate value of this improper integral? thanks in advance.
6 Comments Show 4 older commentsHide 4 older comments
Show 4 older commentsHide 4 older comments
Nathan Hardenberg on 23 Aug 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2011812-matlab-not-displaying-an-approximated-value-of-an-improper-integral#comment_2858517
Open in MATLAB Online
Hey Lahcen, I can not reproduce your results. Your fun variable is not correct. It seems there is a bracket missing at the end. But if I add it I do not get the answer you got. Have you done something beforehand? Can you show how you defined x and p? And make sure to "clear 'all'" at the start of the script
clear 'all'
syms x p
fun= 4./(x.*(1-x).*(2-x).*(p.^2+(log(x./(1-x)).^2))) % added bracket at the end
fun=
vpa(int(fun,0,1))
ans=
Dyuman Joshi on 23 Aug 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2011812-matlab-not-displaying-an-approximated-value-of-an-improper-integral#comment_2858547
Edited: Dyuman Joshi on 23 Aug 2023
Open in MATLAB Online
"But if I add it I do not get the answer you got"
If you check the term given by OP as the result and the final output of your code, they are the same (with a value substituted for p)
@Lahcen
You can try solving numerically, but as there are singularities at x==0 and x==1, the result will not be accurate -
%p^2 value taken from above
p2=2778046668940015/281474976710656;
fun= @(x) 4./(x.*(1-x).*(2-x).*(p2+(log(x./(1-x)).^2)));
integral(fun,0,1)
Warning: Minimum step size reached near x = 1. There may be a singularity, or the tolerances may be too tight for this problem.
ans = 2.7246
Nathan Hardenberg on 23 Aug 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2011812-matlab-not-displaying-an-approximated-value-of-an-improper-integral#comment_2858552
Open in MATLAB Online
@Dyuman Joshi good catch. I saw that but did not realise that p is probably just this numerical value then
p = 2778046668940015/281474976710656
p = 9.8696
Lahcen on 23 Aug 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2011812-matlab-not-displaying-an-approximated-value-of-an-improper-integral#comment_2858712
I'm sorry, it's pi not p.
Dyuman Joshi on 23 Aug 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2011812-matlab-not-displaying-an-approximated-value-of-an-improper-integral#comment_2858732
Open in MATLAB Online
I had an inkling because of the value but wasn't sure due to the format of the value -
sqrt(2778046668940015/281474976710656)
ans = 3.1416
Lahcen on 24 Aug 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2011812-matlab-not-displaying-an-approximated-value-of-an-improper-integral#comment_2862767
Moved: John D'Errico on 28 Aug 2023
Thank you al, Mr Nathan Hardenberg, Mr Dyuman Joshi and Mr Steven Lord, for your helpful responses in addressing my improper integral calculation issue on the Matlab forum. Your assistance was greatly appreciated!. Indeed, I worked by the idea of Mr Nathan Hardenberg.
I'd also like to note that the integral in question is convergent, whereas the numerical methods provided by Matlab seem to be getting stuck without yielding the desired result. It appears that there is a need for the development of more reliable numerical techniques.
Many thanks.
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Nathan Hardenberg on 23 Aug 2023
Edited: Nathan Hardenberg on 23 Aug 2023
Open in MATLAB Online
Here are some ideas, but please still regard my comment.
- You have two variables x and p. You can not approximate numerically then.
- "To approximate integrals directly, use vpaintegral instead of vpa. The vpaintegral function is faster and provides control over integration tolerances." [source]
- Specify that you want to use x as your variable (int(fun, x, [a b]))
- You can not numerically approximate if your denomenator gets 0 at some point (see example below)
-- EDIT --
Your problem is nr. 4. Your denomenator gets 0 to fix this you can simply choose values close to 0 and 1. See example below:
syms x
p = pi; % edited to be p = pi
fun = 4./(x.*(1-x).*(2-x).*(p.^2+(log(x./(1-x)).^2)))
fun=
vpa(int(fun, 0.001, 0.999)) % choosing values slightly above 0 and below 1
ans=
2.0701395395280001703925025323768
But maybe this is not good enough, since the solution can get quite a bit different when getting closer to the limits:
vpa(int(fun, 1e-110, 1 - 1e-13)) % choosing tighter values
ans=
2.7443512001078913067737891865659
-- EDIT END --
% Example of nr. 4 from above
syms x
f = 1/x;
Fvpaint = vpaintegral(f,x,[0 1])
Error using sym/vpaintegral
Failed precision goal. Try using 'MaxFunctionCalls'.
8 Comments Show 6 older commentsHide 6 older comments
Show 6 older commentsHide 6 older comments
Steven Lord on 23 Aug 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2011812-matlab-not-displaying-an-approximated-value-of-an-improper-integral#comment_2858887
Open in MATLAB Online
Let's look at how your function behaves over the limits of integration. I had to add one extra closing parenthesis, which I did at the end. I'm not sure if that's the right place for it.
syms x
fun= 4./(x.*(1-x).*(2-x).*(pi.^2+(log(x./(1-x)).^2)))
fun=
fplot(fun, [0 1])
That certainly looks like a singularity at x = 1.
try
vpa(subs(fun, x, 1))
catch ME
fprintf('subs threw an error:\n%s', ME.message)
end
subs threw an error:Division by zero.
The value at x = 0 also looks like it's potentially very, very large (if not infinite.)
try
vpa(subs(fun, x, 0))
catch ME
fprintf('subs threw an error:\n%s', ME.message)
end
subs threw an error:Division by zero.
What would I get if I tried to numerically integrate the function?
f = matlabFunction(fun)
f = function_handle with value:
@(x)4.0./(x.*(log(-x./(x-1.0)).^2+9.869604401089358).*(x-1.0).*(x-2.0))
integral(f, 0, 1)
Warning: Minimum step size reached near x = 1. There may be a singularity, or the tolerances may be too tight for this problem.
ans = 2.7246
What do you think the value of this integral should be and why?
Lahcen on 24 Aug 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2011812-matlab-not-displaying-an-approximated-value-of-an-improper-integral#comment_2859262
It is convergent, according to Bertrand's criterion.
Dyuman Joshi on 24 Aug 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2011812-matlab-not-displaying-an-approximated-value-of-an-improper-integral#comment_2859462
@Lahcen Can you show the calculations you used to determine that the integral in question is convergent via Bertrand's criterion?
Lahcen on 24 Aug 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2011812-matlab-not-displaying-an-approximated-value-of-an-improper-integral#comment_2860557
Edited: Lahcen on 25 Aug 2023
@Dyuman Joshi: Dear Dyuman, I apologize for the delay in response.
Recall that:
By Bertrand, let a<0, converge if and only if or ()
Now, in the vicinity of 0, we have
, which converges according to Bertrand's criterion.
For 1, it suffices to set x=1-t to find
, which converges according to Bertrand's criterion.
whence the integral is convergent.
Lahcen on 25 Aug 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2011812-matlab-not-displaying-an-approximated-value-of-an-improper-integral#comment_2860587
Edited: Lahcen on 25 Aug 2023
@Nathan Hardenberg
Thank you very much for the idea of the approximation, I based on your idea. Indeed, I noticed that the approximation via matlab in the vicinity of 0 does not present a problem while in the vicinity of 1 Matlab only approached 1 approximately , so I split the integral into two parts as follows:
then I made a change of variable for the second integral t=1-x, we get
Now we will apply your idea of approximation in the vicinity of 0 for the two integrals via matlab, we find:
Nathan Hardenberg on 25 Aug 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2011812-matlab-not-displaying-an-approximated-value-of-an-improper-integral#comment_2861122
@Lahcen Glad I I could help. Nice idea to use the better numerical precision near 0 to get a better approximation
Now I also got interested in the convergance 😀 I did not find the formula by Bertrand you provided. I only found the following. Always converging when
But this is obiously somthing else, regarding the area after to infinity. Would be greatly appreciated if you would provide a resource, if you can. But no worries if you can't
Lahcen on 25 Aug 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2011812-matlab-not-displaying-an-approximated-value-of-an-improper-integral#comment_2861357
Edited: Lahcen on 26 Aug 2023
Hello Dear @Nathan Hardenberg
Attached you will find a reference about A, concerning your question, (but they are references in French).
But using what you found like, one can find what my published. Indeed, Let a<1 and let put
by setting we find,
then
Now, this integral is converge if and only if
which is equivalent .
I hope you like the proof.
Nathan Hardenberg on 27 Aug 2023
Direct link to this comment
https://support.mathworks.com/matlabcentral/answers/2011812-matlab-not-displaying-an-approximated-value-of-an-improper-integral#comment_2862757
@Lahcen Thanks a lot :D
Sign in to comment.
More Answers (0)
Sign in to answer this question.
See Also
Categories
MATLABLanguage FundamentalsLoops and Conditional Statements
Find more on Loops and Conditional Statements in Help Center and File Exchange
Tags
- improper
- integral
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office