Not exactly the exact answer using Symbolic Math Toolbox

1 次查看(过去 30 天)
I'm finding the RMS value of sine across one period, and I expect the result to be A/sqrt(2).
The result I get is very close, but not exactly the same.
Can anyone please help?
syms t
f = sin(t);
frms = sqrt(1/(2*pi)*int(f^2,t,[0 2*pi])) % rms value from definition
frms = 
frms_simplified = simplify(frms) % here I expected 1/sqrt(2)
frms_simplified = 
var = vpa(frms_simplified) % decimal value
var = 
0.70710678118654754625835857353067
var2 = vpa(1/sqrt(2)) % for comparison
var2 = 
0.70710678118654752440084436210485
Thanks in advance :-)

采纳的回答

Torsten
Torsten 2025-5-8
syms t
f = sin(t);
frms = sqrt(1/(2*sym(pi))*int(f^2,t,[0 2*sym(pi)])) % rms value from definition
frms = 

更多回答(1 个)

John D'Errico
John D'Errico 2025-5-8
编辑:John D'Errico 2025-5-8
Your problem stems from a common mistake.
Whaen you write this:
syms t
f = sin(t);
frms = sqrt(1/(2*pi)*int(f^2,t,[0 2*pi])) % rms value from definition
frms = 
And there, MATLAB has chosen a way to express the result using large integers.
In that third line, do you think MATLAB "knows" that something like sqrt(1/(2*pi)) really is EXACTLY the number you wrote? It is not. In fact, pi in MATLAB is the number
format long g
pi
ans =
3.14159265358979
which is darn close to the true value of pi, but it is a DOUBLE PRECISION NUMBER. It is not exactly the number pi, but ony a 52 binary bit approximation to pi. They are entirely different things.
So let me change what you wrote by telling MATLAB to use pie as a symbolic constant instead.
pie = sym('pi');
frms = sqrt(1/(2*pie)*int(f^2,t,[0 2*pie])) % rms value from definition
frms = 
Which is indeed sqrt(1/2), even though simplify did not notice that sin(4*pie) would be zero. Stupid computers. Of course, when I say that, it is most of the time my own foolishness I am referring to, since the computer just does what I tell it to do. ;-) I could probably convince MATLAB to fix that of course, but if I did, then MATLAB would just ignore everything I type. Oh well.
  4 个评论
Walter Roberson
Walter Roberson 2025-5-8
syms t
f = sin(t);
pie = sym(pi);
frms = sqrt(1/(2*pie)*int(f^2,t,[0 2*pie])) % rms value from definition
frms = 

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Linear Algebra 的更多信息

产品


版本

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by