In MatLab Write a function leapyear(year). The function has one parameter year.
ID: 669004 • Letter: I
Question
In MatLab
Write a function leapyear(year). The function has one parameter year. If the year is leap year, the function returns true. Otherwise returns false. A year is a leap year if the year meets either of the two conditions.
The year is divisible by 4 but not divisible by 100.
The year is divisible by 400.
(Hint: use mod function, logic and(&&), and logic or(||). Conditions (1) and (2) are logic or relationship. Condition (1) has two conditions. They are logic and relationship. )
Note: not is ~ in MATLAB. For example, “year is not divisible by 100” is represented as “
mod(year, 100)~=0)
Do not assign 1 or 0 to the return variable. Do not assign ‘true’ or ‘false’ to the return variable.
Explanation / Answer
working matlab code
function x=isleap(Year)
switch nargin
case 0
c=clock; Year=c(1); clear c
case 1
if ~isvector(Year) || ~all(isnumeric(Year)) || ~all(isfinite(Year)) || isempty(Year)
error('Warning: Year values must be numeric and finite')
end
if ~isequal(Year,round(Year))
error('Warning: Year values must be integer')
end
L=Year-1583;
if L(L<0)
error('Warning: Every value of Year must be >1582')
end
otherwise
error('stats:Isleap:TooMuchInputs','Year must be a scalar or a vector.');
end
x = ~mod(Year, 4) & (mod(Year, 100) | ~mod(Year, 400));
return
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.