function resp = FreqSystems (task, studentnumber, parameters)
% Function to return values dependent on your student number
% Different tasks are available, and optional parameters can be provided
% Richard Mitchell 2/10/13
if nargin < 3, parameters = 0; end
resp.cnum = 1; resp.cden = 1; resp.pnum = 1; resp.pden = 1;    % default
if length(studentnumber) == 8
    umm = getdata(studentnumber);
    switch task 
        case 0, resp = doclosedloop(parameters);
        case 1, 
            resp = case1(umm);
        case 2, 
            resp = case2(umm);
        case 3, 
            resp = case3(umm);
        case 4, 
            resp = case4(umm);
        case 5, 
            resp = case5(umm);
        case 6, 
            resp = case6(umm);
        case 7, 
            resp = case7(umm);
        case 8, 
            resp = case8(umm);
        case 11, 
            resp = case3(umm);     % first order sys ok
        case 12, 
            resp = case12(umm);   
        case 13, 
            resp = case13(umm);  
        case 14, 
            resp = case14(umm);  
        case 15, 
            resp = case15(umm);  
        case 16, 
            resp = case16(umm);  
        case 17, 
            resp = case17(umm);  
    end
else error ('student number should be 8 digits')
end

function resp = doclosedloop(resp)
resp.clnum = conv(resp.cnum, resp.pnum);
resp.clden = pyadd(resp.clnum, conv(resp.cden, resp.pden));

function pb = pyadd(p1, p2)
while length(p1) < length(p2), p1 = [0 p1]; end
pb = p1 + p2;

function resp = case1(um)
resp.cnum = um(3)+2;
resp.cden = 1;
resp.pnum = um(5)+3;
resp.pden = [um(2)+1 um(6)*3+1 0];

function resp = case2(um)
resp.cnum = 1;
resp.cden = 1;
resp.pnum = um(6)+3;
resp.pden = conv([um(2)+1, um(5)*3+2], [um(3)+1, um(7)+4, 0]);

function resp = case3(um)
k = um(3)+3;
t = um(5)*3+4;
resp = setsys(fnumstostr(k), k, fnumstostr('(1 + s/',t,')'), [1/t 1]);

function resp = case4(um)
k = um(3)+3;
t1 = um(5)*3+4;
t2 = t1 + um(6)+2;
resp = setsys(fnumstostr(k), k, fnumstostr('(1 + s/',t1,')(1 + s/', t2, ')'), conv([1/t1 1], [1/t2 1]));

function resp = case5(um)
k = um(3)+3;
t1 = um(5)*3+4;
t2 = t1 + um(6)+2;
t3 = t2+ um(7)+5;
resp = setsys(fnumstostr(k, '*(1 + s/', t2, ')'), k*[1/t2 1], ...
              fnumstostr('(1 + s/',t1,')(1 + s/', t3, ')'), conv([1/t1 1], [1/t3 1]));

function resp = case6(um)
resp = case5(um);
resp.pden = [resp.pden 0];
resp.denstr = ['s', resp.denstr];

function resp = case7(um)
k = um(3)+3;
t1 = um(5)*3+4;
t2 = t1 + um(6)+2;
t3 = t2+ um(7)+5;
resp = setsys(fnumstostr(k), k, ...
              fnumstostr('(1 + s/',t1,')(1 + s/', t2, ')(1 + s/', t3, ')'), ...
                           conv(conv([1/t1 1], [1/t2 1]), [1/t3 1]));

function resp = case8(um)
k = um(3)+3;
t1 = um(5)*3+4;
t2 = t1*3 + um(6)+2;
if um(4)>5, zeta = 0.3; else zeta = 0.5; end
resp = setsys(fnumstostr(k,'*',t2,'^2'), k*t2*t2, ...
              fnumstostr('(1 + s/',t1,')(s^2 + 2*', zeta,'*', t2, 's + ', t2, '^2)'), ...
                         conv([1/t1 1], [1 2*zeta*t2 t2*t2]));

function resp = case12(um)
k = um(3)+ 3;
t1 = um(5)*3+4;
t2 = t1*5 + um(6)+2;
resp = setsys(fnumstostr(k), k, fnumstostr('(1 + s/',t1,')(1 + s/', t2, ')'), conv([1/t1 1], [1/t2 1]));

function resp = case13(um)
k = um(3)+3;
t1 = um(5)*3+4;
t2 = t1*3 + um(6)+2;
t3 = t2*8+ um(7)+5;
resp = setsys(fnumstostr(k, '*(1 + s/', t2, ')'), k*[1/t2 1], ...
              fnumstostr('(1 + s/',t1,')(1 + s/', t3, ')'), conv([1/t1 1], [1/t3 1]));

function resp = case14(um)
resp = case13(um);
resp.pden = [resp.pden 0];
resp.denstr = ['s', resp.denstr];

function resp = case15(um)
k = um(3)+3;
t1 = um(5)*3+4;
t2 = t1*3 + um(6)+2;
resp = setsys(fnumstostr(k), k,  ...
              fnumstostr('s(1 + s/',t1,')(1 + s/', t2, ')'), conv([1/t1 1 0], [1/t2 1]));

function resp = case16(um)
k = um(2)+5;
t1 = um(6)*2+1;
t2 = t1 + um(3)+2;
t3 = t2+ um(5)+4;
resp = setsys(fnumstostr(k), k, ...
              fnumstostr('(1 + s/',t1,')(1 + s/', t2, ')(1 + s/', t3, ')'), ...
                           conv(conv([1/t1 1], [1/t2 1]), [1/t3 1]));

function resp = case17(um)
k = um(4)+3;
t1 = um(2)*2+1;
te = t1 + um(7)+3;
t2 = te + um(3)+2;
t3 = t2+ um(6)+7;
t4 = t3+ um(5)+8;
resp = setsys(fnumstostr(k, '*(1 + s/', te, ')'), k*[1/te 1], ...
              fnumstostr('(1 + s/',t1,')(1 + s/', t2, ')(1 + s/', t3, ')(1 + s/', t4, ')'), ...
                           conv(conv([1/t1 1], [1/t2 1]), conv([1/t3 1], [1/t4 1])));


function resp = setsys(ns, num, ds, den)
resp.pnum = num;
resp.pden = den;
resp.numstr = ns;
resp.denstr = ds;

function vals = getdata(stno)
vals = str2num(stno')';

function s = fnumstostr(varargin)
% this is passed a series of strings or numbers, and it returns in s
% a string containing each element in turn : string arguments are just
% copied, numbers are converted ..
% call by s = fnumstostr('Gain is', gmax, 'bandwidth is', wo, 'rad/s')
s = '';                                 % empty string
for ct = 1:length(varargin)             % for each argument
    if isstr(varargin{ct})              % if string, just add
        s = [s, varargin{ct}];
    elseif isinf (varargin{ct})         % in INF, add 'inf'
        s = [s, ' Inf '];
    elseif isnan (varargin{ct})         % in NAN, add 'nan'
        s = [s, '  NaN '];
    else                                % else add string equiv of number
        s = [s, num2str(varargin{ct}, max([4, ceil(log10(varargin{ct}))]))];
    end
end
