Convert a decimal approximation to exact value symbolically

조회 수: 4 (최근 30일)
rezheen
rezheen 2025년 6월 26일
댓글: Walter Roberson 2025년 7월 10일
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.

카테고리

Help CenterFile Exchange에서 Numeric Solvers에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by