Not exactly the exact answer using Symbolic Math Toolbox

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

Accepted Answer

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

More Answers (1)

John D'Errico
John D'Errico on 8 May 2025
Edited: John D'Errico on 8 May 2025
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 Comments
Walter Roberson
Walter Roberson on 8 May 2025
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 = 

Sign in to comment.

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!