Convert a decimal approximation to exact value symbolically

3 views (last 30 days)
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

Torsten
Torsten on 26 Jun 2025
Moved: Torsten on 26 Jun 2025
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
Torsten
Torsten on 10 Jul 2025
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.
Walter Roberson
Walter Roberson on 10 Jul 2025
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)

Walter Roberson
Walter Roberson on 8 Jul 2025
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.

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!