Mean of squares of first nn positive integers.
Mostra commenti meno recenti
Write a function called mean_squares that returns mm, which is the mean of the squares of the first nn positive integers, where nn is a positive integer and is the only input argument. For example, if nn is 5, your function needs to compute the average of the numbers 1, 4, 9, 16, and 25. You may use any built-in functions including, for example, sum.
function mm = mean_squares (nn)
sqr = (1:nn).^2; %ERROR
total = sum(sqr);
mm = total/nn;
end
I wrote the above code. In line 2, I got the following error: "For colon operator with char operands, first and last operands must be char."
However,the code if executed in the command window, runs fine.
3 Commenti
Roger Stafford
il 24 Lug 2015
You could just say
mean_squares = @(nn) (nn+1).*(2*nn+1)/6;
Duddela Sai Prashanth
il 23 Dic 2016
function O = odd_rms(n)
n = n*2-1;
O = [1:2:n];
O = O.^2;
O = sum(O)/n;
O = sqrt(O);
Ali Tajik
il 23 Gen 2017
it did not work for 2 when you check it by grader
Risposta accettata
Più risposte (2)
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!