Convert a decimal approximation to exact value symbolically

Hi, I'm working with a definite integral and get a decimal approximation as my answer. I'd like to also get the exact solution:
syms x y
y1=sin(pi*x/2); y2=x; x1=-1; x2=1;
A=int((abs(y2-y1)),x1,x2);
Gives the answer as 0.2732 which is a correct decimal approximation. The exact solution is:
(4-pi)/pi
How do I get that?

 Accepted Answer

Here, you need the fact that y2-y1 is an odd function:
syms x y
y1=sin(pi*x/2); y2=x; x1=-1; x2=1;
A=2*int(y2-y1,x,x1,0)
A = 

6 Comments

It may be that I'm using MATLAB 2021a version and a fractional exact answer can not be outputted, because running your code on my end still gives the answer as a decimal approximation. I'm also not sure what even, odd functions have to do with spitting out the output.
If f is even or odd,
integral_{x=-1}^{x=1} abs(f(x)) dx = 2*integral_{x=-1}^{x=0} abs(f(x)) dx
Since in your case (y2-y1)(x) >=0 for -1 <= x <= 0, you get
integral_{x=-1}^{x=1} abs(y2(x)-y1(x)) dx = 2*integral_{x=-1}^{x=0} (y2(x)-y1(x)) dx
I don't know how MATLAB 2021a shows the result of this last integral. In R2024b, it's 4/pi - 1 .
I'm also not sure what even, odd functions have to do with spitting out the output.
If you don't make this simplification of splitting the integral, you won't get the symbolic output from above:
syms x y
y1=sin(pi*x/2); y2=x; x1=-1; x2=1;
A=int((abs(y2-y1)),x1,x2)
A = 
A=vpaintegral((abs(y2-y1)),x1,x2)
A = 
0.27324
Actually, your original answer works without using @Walter Roberson line of code, but I wanted to take the integral of the whole region instead of splitting it into half and doubling, if I have functions or areas without identical halves (odd?), I suppose there aren't exact solutions with the version I'm using.
If you have functions or areas that do not have identical halves, then it is unlikely that the function uses abs() [but possible...] int() tends to struggle a bit with abs()
Whether a function without abs() can be integrated or not... depends a lot on the function. It is easy to create functions that have no known exact integral.
if I have functions or areas without identical halves (odd?), I suppose there aren't exact solutions with the version I'm using.
Sometimes it works, sometimes not. The "abs" function is always problematic together with "int". And it doesn't have to do with your MATLAB version - it also doesn't work in R2024b as you can see above.
Huh, there appears to be a simplify bug.
syms x y
y1=sin(pi*x/2); y2=x; x1=-1; x2=1;
A=int((abs(y2-y1)),x1,x2)
A = 
simplify(A, 'steps', 50)
ans = 
vpa(A)
ans = 
0.27323954473516268615107010698011

Sign in to comment.

More Answers (1)

sympref('FloatingPointOutput',false);
Will display an unevaluated int() form for your original problem, and will display 4/pi - 1 for the version suggested by @Torsten
It seems that you have FloatPointOutput true in effect.

Products

Release

R2021a

Tags

Asked:

on 26 Jun 2025

Commented:

on 10 Jul 2025

Community Treasure Hunt

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

Start Hunting!