Calculating the RMS (Root-Mean-Square) Average

Hi all. So I'm trying to calculate the rms average. The image below shows the exact question
. The answer I get after running my script is 2.5. However, when I calculate this by hand I get 6.20. Either way, one of them is wrong. Any help checking my code is greatly appreciated!
N=input('Input the number of data points:');
for ii=1:N
A=input('Enter the value of x: '); % A is the variable to read in user input
x_rms=sqrt(1/N.*(sum(x(ii).^2)));
end
fprintf('The root-mean-square value is:%d\n',x_rms)

1 件のコメント

CHETARIYA SUNIL
CHETARIYA SUNIL 2022 年 5 月 15 日
hello I'm tring but geting propted by this:
rms_voltage
Input the number of data points:
4
Enter the value of x:
1:0.5:2
Unrecognized function or variable 'x'.
Error in rms_voltage (line 6)
x_rms=sqrt(1/N.*(sum(x(ii).^2)));

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

 採用された回答

Star Strider
Star Strider 2016 年 7 月 28 日

0 投票

You’re almost there. Collect the numbers in the loop, then do the calculation after the loop:
N=inputdlg('Input the number of data points:'); N = str2num(N{:});
for ii=1:N
A=inputdlg('Enter the value of x: '); A = str2num(A{:});% A is the variable to read in user input
x(ii) = A;
end
x_rms=sqrt(1/N.*(sum(x.^2)));
fprintf('The root-mean-square value is: %f\n',x_rms)
I used the inputdlg function because I prefer it. You can substitute your input calls.

4 件のコメント

Matthew Lintern
Matthew Lintern 2016 年 7 月 28 日
Brilliant, thanks so much! Also, thanks for the introduction to the inputdlg function!
Star Strider
Star Strider 2016 年 7 月 28 日
My pleasure!
I prefer inputdlg because it doesn’t mess up the Command Window, even if requires an extra line or two of code.
idris
idris 2023 年 12 月 7 日
We venerate loyalty to the developers of MATLAB as the tool "inputdlg" is okay.
Star Strider
Star Strider 2023 年 12 月 7 日
@idris— What?

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

その他の回答 (0 件)

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by