Convert a decimal approximation to exact value symbolically

3 次查看(过去 30 天)
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?

采纳的回答

Torsten
Torsten 2025-6-26
移动:Torsten 2025-6-26
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 个评论
Torsten
Torsten 2025-7-10
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 2025-7-10
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

请先登录,再进行评论。

更多回答(1 个)

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

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by