MATLAB Answers

0

Help with making a function.

Asked by Macaulay Rocha on 12 Mar 2019
Latest activity Answered by Stephen Cobeldick on 12 Mar 2019
I am trying to create a function that takes a single number and determines if it is a square root or not. If it is a perfect square it takes the square root, and if not it divides by three and rounds down. How do i do that?
this is what I have:
function y = x^2;
if y=1;
z = sqrt(y);
end
else
y/3

  0 Comments

Sign in to comment.

2 Answers

Answer by Akira Agata
on 12 Mar 2019

Like this? (Please save the following code as squareRootCheck.m)
function y = squareRootCheck(x)
if mod(sqrt(x),1) == 0
y = sqrt(x);
else
y = floor(x/3);
end
end

  0 Comments

Sign in to comment.


Answer by Stephen Cobeldick on 12 Mar 2019

function y = myfun(x)
y = sqrt(x);
if y~=fix(y)
y = floor(x/3);
end
end

  0 Comments

Sign in to comment.