Search Program on this blog

Friday 6 November 2015

Write a function called moving_average that takes a scalar called x as an input argument and returns a scalar. The value it returns depends not only on the input but also on previous inputs to this same function when the function is called repeatedly. That returned value is a “moving average” of those inputs. The function uses a “buffer” to hold previous inputs, and the buffer can hold a maximum of 25 inputs. Specifically, the function must save the most recent 25 inputs in a vector (the buffer) that is a persistent variable inside the function. Each time the function is called, it copies the input argument into an element of the buffer. If there are already 25 inputs stored in the buffer, it discards the oldest element and saves the current one in the buffer. After it has stored the input in the buffer, it returns the mean of all the elements in the buffer. Thus, for each of the first 24 calls to the function, the function uses only the inputs it has received so far to determine the average (e.g., the first call simply returns x, the second call averages x and the input from the first call, etc.), and after that, it returns the average of the most recent 25 inputs.

function mean = moving_average(x)
persistent first;
persistent buffer;
persistent counter;
if isempty(buffer) && isempty(counter)
    buffer=x;
    counter=1;
    first=x;
elseif counter<25
    buffer=buffer+x;
    counter=counter+1;
    first=[first x];
elseif counter==25
    buffer=buffer-first(1)+x;
    first = [first(2:counter) x];
end
mean = buffer/counter;

Write a function called movies that takes the starting times of two movies, hr1, hr2, min1, min2, and their durations, durmin1, durmin2, as its input arguments and decides whether we can binge and watch both. The criteria are that they must not overlap and that we are not going to wait more than 30 minutes between the end of one and the beginning of the next. It returns true if the criteria are both met and returns false otherwise. You may assume that movie start times are always after 1 pm and before midnight. You may also assume that the first one starts earlier. The order of the input arguments is: hr1, min1, durmin1, hr2, min2, durmin2.

function watch = movies(hr1,min1,durmin1,hr2,min2,~)
diff = (hr2*60+min2)-(hr1*60+min1+durmin1);
if hr1<=hr2
    if diff>=0
        if diff<=30
            watch=1';
        elseif diff>30
            watch=0;
        end
    elseif diff<0
        watch=0;
    end
else
    watch =0;
end

Write a function called older that takes as its input arguments six positive scalar integers: y1, m1, d1, y2, m2, d2, in that order, representing the birthdates of two persons. The variables that start with y stand for the year, m for the month and d for the day. The variables that end in 1 correspond to the first person, while those that end in 2 correspond to the second person. The function returns 1 if the first person is older, 0 if they have the same age, and -1 if the first person is younger. You do not need to check whether the inputs have appropriate values. For example, you may assume that both m1 and m2 are positive integers that are less than 13 and that the day numbers fit with their months.

function out = older(y1,m1,d1,y2,m2,d2)
if m2>0 && m2<13
    if m2==1 || m2==3 || m2==5 || m2==7 || m2==8 || m2==10 || m2==12
        if d2>0 && d2<32
            flag=1;
        else
            flag=0;
        end
    elseif m2==4 || m2==6 || m2==9 || m2==11
        if d2>0 && d2<31
            flag=1;
        else
            flag=0;
        end
    elseif m2==2
        if mod(y2,4)==0 && (mod(y2,400)==0 || mod(y2,100)~=0) && d2<=29 && d2>0
            flag=1;
        elseif d2>0 && d2<=28
            flag=1;
        else
            flag=0;
        end
    end
else
    return;
end
if m1>0 && m1<13
    if m1==1 || m1==3 || m1==5 || m1==7 || m1==8 || m1==10 || m1==12
        if d1>0 && d1<32
            f=1;
        else
            f=0;
        end
    elseif m1==4 || m1==6 || m1==9 || m1==11
        if d1>0 && d1<31
            f=1;
        else
            f=0;
        end
    elseif m1==2
        if mod(y1,4)==0 && (mod(y1,400)==0 || mod(y1,100)~=0) && d1<=29 && d1>0
            f=1;
        elseif d2>0 && d2<=28
            f=1;
        else
            f=0;
        end
    end
else
    return;
end
if y1<y2
    out=1;
elseif y1==y2
    if m1<m2
        out=1;
    elseif m1==m2
        if d1<d2
            out=1;
        elseif d1==d2
            out=0;
        else
            out=-1;
        end
    else
        out=-1;
    end
else
    out=-1;
end

Write a function called classify that takes one input argument x. That argument will have no more than two dimensions. If x is an empty matrix, the function returns -1. If x is a scalar, it returns 0. If x is a vector, it returns 1. Finally, if x is none of these, it returns 2. Do not use the built-in functions isempty, isscalar, or isvector.

function out = classify(x)
[n m] = size(x);
if nargin==1
    if n==0 || m==0
        out=-1;
    elseif n==1 && m==1
        out=0;
    elseif n==1 && m>1
        out=1;
    elseif m==1 && n>1
        out=1;
    %elseif n>1 && m>1
        %out=1;
    else
        out=2;
    end
else
    out=2;
end

Write a function called sort3 that takes three scalar arguments. It uses if-statements, possibly nested, to return the three values of these arguments in a single row-vector in increasing order (or more precisely, non-decreasing order), i.e., element one of the output vector equals the smallest input argument and element three of the output vector equals the largest input argument. NOTE: Your function may not use any built-in functions, e.g., sort.

function row = sort3(a,b,c)
if a>=b && a>=c
    if b>=c
        row = [c b a];
    elseif b<=c
        row = [b c a];
    end
elseif b>=a && b>=c
    if a>=c
        row = [c a b];
    elseif a<=c
        row = [a c b];
    end
elseif c>=a && c>=b
    if a>=b
        row = [b a c];
    elseif a<=b
        row = [a b c];
    end
end

Write a function called letter_grade that takes a positive integer called score as its input argument and returns a letter grade according to the following scale: A: 91 and above; B: 81-90; C: 71-80; D: 61-70; F: below 61. Remember that to assign a letter to a variable, you need to put it in single quotes, as in: grade = 'A'.

function grade = letter_grade(score)
if score>=91
    grade = 'A';
elseif score>=81 && score<=90
    grade = 'B';
elseif score>=71 && score<=80
    grade = 'C';
elseif score>=61 && score<=70
    grade = 'D';
elseif score<61
    grade = 'F';
end

Write a function called generationXYZ that takes as its only input argument one positive integer specifying the year of birth of a person and returns as its only output argument the name of the generation that the person is part of ('X', 'Y', or 'Z ') according to the table below. For births before 1966, return 'O' for Old and for births after 2012, return 'K' for Kid. Remember that to assign a letter to a variable, you need to put it in single quotes

function gen = generationXYZ(year)
if year<1966
    gen = 'O';
elseif year>=1966 && year<=1980
    gen = 'X';
elseif year>=1981 && year<=1999
    gen = 'Y';
elseif year>=2000 && year<=2012
    gen = 'Z';
elseif year> 2012
    gen = 'K';
end