Not exactly the exact answer using Symbolic Math Toolbox

1 回表示 (過去 30 日間)
Kristian Lomholdt
Kristian Lomholdt 2025 年 5 月 8 日
コメント済み: Kristian Lomholdt 2025 年 5 月 9 日
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 件のコメント
Kristian Lomholdt
Kristian Lomholdt 2025 年 5 月 9 日
Thanks - I wasn't aware of the difference :-)

サインインしてコメントする。

その他の回答 (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 = 
Kristian Lomholdt
Kristian Lomholdt 2025 年 5 月 9 日
Thanks for clarifiyng :-)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLinear Algebra についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by