frms =
Not exactly the exact answer using Symbolic Math Toolbox
Show older comments
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_simplified = simplify(frms) % here I expected 1/sqrt(2)
var = vpa(frms_simplified) % decimal value
var2 = vpa(1/sqrt(2)) % for comparison
Thanks in advance :-)
Accepted Answer
More Answers (1)
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
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
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
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
Torsten
on 8 May 2025
Defining
pie = sym('pi')
pi is used as a symbolic variable with name "pi", not as the numerical constant pi.
John D'Errico
on 8 May 2025
yes
syms t
f = sin(t);
pie = sym(pi);
frms = sqrt(1/(2*pie)*int(f^2,t,[0 2*pie])) % rms value from definition
Kristian Lomholdt
on 9 May 2025
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!