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

Answers (1)

> 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 Mathematics and Optimization in Help Center and File Exchange

Products

Asked:

on 14 Mar 2021

Commented:

on 14 Mar 2021

Community Treasure Hunt

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

Start Hunting!