close all

PID = [0.5, 0, 0; 
       1, 0, 0; 
       2, 0, 0];
transNum = [1];
transDen = [1 0];

delay = 0;
N = 0;

tstop = 10; %time to stop simulation
options = simset('FixedStep',0.01,'Solver','ode4');  %options for simulation

colors = [[1,0,0];[0,1,0];[0,0,1];[0,1,1];[1,0,1];[1,.75,0.5];[.75,0.5,1];[0.5,.75,1];[.5,.5,.5]];

yStore = {};

[n,~] = size(PID);
[types,order] = size(transDen);
for jj=1:types
    PidLegend = {};
    for ii=1:n
        P = PID(ii,1);
        I = PID(ii,2);
        D = PID(ii,3);

        sim('pidLoop.mdl', tstop, options); %Runs the simulation

        %extract data from simout
        SP = out.signals.values(:,1);
        Y = out.signals.values(:,2);
        t = out.time;

        Pstr = num2str(P);
        Istr = num2str(I);
        Dstr = num2str(D);
        PidStr = '';
        if P
            PidStr = strcat(PidStr,' P=',Pstr);
        end
        if I
            PidStr = strcat(PidStr,' I=',Istr);
        end
        if D
            PidStr = strcat(PidStr,' D=',Dstr);
        end
        PidLegend{ii+1} = PidStr;

        fig = figure(jj);
        hold on
        if ii==1
            p = plot(t,SP);
            color = [0,0,0];
            set(p,'Color',color,'LineWidth',2)
            PidLegend{1} = ' Setpoint';
        end
        p = plot(t,Y);
        color = colors(ii,:);
        set(p,'Color',color,'LineWidth',2)
    end
    typeStr = strcat('Order:',num2str(order-1));
    
    xlabel('time')
    ylabel('amplitude')
    legend(PidLegend,'Location','NorthEastOutside')
    title(typeStr)
    
    set(fig,'Position',[0,100,800,400])
    
    ax = axis;
    if ax(4)<=1
        axis([ax(1),ax(2),ax(3),1.2])
    end
end

