Asked by Amitdyuti
on 24 Jul 2015

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.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Roger Stafford (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/231082-mean-of-squares-of-first-nn-positive-integers#comment_300050

## Duddela Sai Prashanth (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/231082-mean-of-squares-of-first-nn-positive-integers#comment_415820

## Ali Tajik (view profile)

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/231082-mean-of-squares-of-first-nn-positive-integers#comment_422550

Sign in to comment.