Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a function is even in Matlab using the following strategy. (Don\'t use

ID: 3902161 • Letter: 1

Question

1. Write a function is even in Matlab using the following strategy. (Don't use an alternative method without first checking that it's okay.) • Input: an integer x. Output: true if x is even, false if x is odd. • Divide x by 2 and call the result y. Then y will look something like 7 or 7.500, depending on whether x is even or odd. • Get the decimal part of y, and call the result z. So in the examples above, you would get z 0 or z .500, respectively. Find a way to get this decimal part that uses the floor command. • If the distance from z to 0 is less than .01, return ???. If the distance from z to .5 is less than .01 return ???. Remember that we measure distance using absolute value. • If neither of these conditions is true, raise an error with an appropriate message. Remember the syntax is error(msg).

Explanation / Answer

Save the following code in a file named isEven.m

function res = isEven(x)

y = x / 2;
z = y - floor(y); %get decimal part of y
if abs(z) < 0.01
res = true
elseif abs(z-0.5) < 0.01
res = false
else
error('Something is wrong')
end

end


==========

Now in command window you can call the function as shown below
Please do rate the answer if it helped. Thank you
> isEven(5)
result = 0
> isEven(2)
result = 1
> isEven(4)
result = 1
> isEven(13)
result = 0
> isEven(0)
result = 1
> isEven(1)
result = 0