Write a MATLAB function that returns the square of a number.

23 views (last 30 days)
funciton square = sqr (x)
square = x * x;
end
Is it correct ?

Answers (1)

Adam Danz
Adam Danz on 14 Mar 2021
Edited: Adam Danz on 14 Mar 2021
> Is it correct ?
funciton square = sqr (x)
square = x * x;
end
Feedback
  1. "function" is spelled incorrectly which I'm sure is a typo.
  2. What is x? Is it a scalar (single value) or a vector/matrix/array? If it's not a scalar, then no, this is not correct.
  3. While x*x is effectively squaring a scalar, why not square the value using an exponent?
  4. This is minor, but stylistically, there's no need for the space between sqr and (x).
  5. Instead of asking if it's correct, test it!! Provide a wide variety of inputs including large numbers, small numbers, negative numbers, 0, etc.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!