Effacer les filtres
Effacer les filtres

help with this question

2 vues (au cours des 30 derniers jours)
Alexandra Huff
Alexandra Huff le 6 Août 2016
Commenté : prashant paudel le 23 Mai 2018
Hi, so I am trying to geta function that returns an output, which is the square root of the mean of the squares of the first nn positive odd integers, where nn is a positive integer and the only input argument. for example if nn=3 i want my function to return the sqaure root of the average of the numbers 1 9, and 25. my code so far is this:
function orms = odd_rms(nn)
orms = sqrt(mean((1:nn).^2));
however this is only returning positive integers. So I am wondering how to make this odd positive integers.
  1 commentaire
prashant paudel
prashant paudel le 23 Mai 2018
function rms=odd_rms(n) x=n*(2*n+1)*(2*n-1)/3; rms=sqrt(x/n);

Connectez-vous pour commenter.

Réponse acceptée

John D'Errico
John D'Errico le 6 Août 2016
Ok, so the first nn positive integers are easy to generate.
2*(1:nn) - 1
TRY IT! Put in various values for nn. See that this works, and think about why it works. I could also have done it differently.
1:2:(2*nn-1)
Both of these will work, generating odd integers as needed. You should see why the code that you wrote did not work properly. The rest of your code was correct, in terms of squaring elements, forming the mean, then the sqrt.
oddrms = @(nn) sqrt(mean((1:2:(2*nn-1)).^2));
Test out the function.
oddrms(1)
ans =
1
oddrms(2)
ans =
2.2361
oddrms(3)
ans =
3.4157
Yes, I could have written it in m-file form. So this should work too:
function orms = odd_rms(nn)
orms = sqrt(mean((1:2:(2*nn-1)).^2));
end
  2 commentaires
SULE SAHIN
SULE SAHIN le 30 Oct 2017
I dont understand (2*nn-1). why we use (2*nn-1) instead of nn because for example if nn = 3, we use 2*3_1=5 instead of 3. we go out border line. please may you explain this?
Isaac DeVaughn
Isaac DeVaughn le 14 Déc 2017
Modifié(e) : Isaac DeVaughn le 14 Déc 2017
I think i understand if you just bounded it by nn you only get some of the number you need up to three. That would leave out the 25 from the example.I think that leaves out part of the solution.

Connectez-vous pour commenter.

Plus de réponses (1)

Azzi Abdelmalek
Azzi Abdelmalek le 6 Août 2016
nn=3
mm=(0:nn-1)*2+1

Catégories

En savoir plus sur Performance and Memory dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by