close

flag=0;
flag1=0;
flag2=0;
flag3=0;

while(flag==0)
    var = input('Enter a non-negative value for the first weight parameter A: ');
    if(var<0)
        disp('         ')
        disp('ERROR: A must be non-negative.')
        disp('         ')
    else
        flag=1;
        A=var;
    end
end
flag=0;
disp('        ')
while(flag==0)
    var = input('Enter a positive value for the second weight parameter B: ');
    if(var<=0)
        disp('           ')
        disp('ERROR: B must be positive.')
        disp('            ')
    else
        flag=1;
        B=var;
    end    
end
disp('           ')
x0 = input('Enter the initial condition of the state (x_0): ');
disp('           ')
disp('One moment please...')
y1=labone(A,B,x0);

disp('               ')

while(flag1==0)
    disp('Would you like to plot')
    disp('   1. state on optimal path (x^*)')
    disp('   2. adjoint (lambda)')
    disp('   3. optimal control (u^*)')
    disp('   4. all three')
    var1=input('Type 1, 2, 3, or 4: ');

    if((var1==1) | (var1==2) | (var1==3) | (var1==4))
        flag1=1;
        disp('         ')
        while(flag2==0)
            disp('Would you like to vary any parameters?')
            disp('1. Yes')
            disp('2. No')
            var2=input('Type 1 or 2: ');
            
            if(var2==1)
                disp('         ')
                flag2=1;
                while(flag3==0)
                    disp('Which parameter would you like to vary?')
                    disp('1. A')
                    disp('2. B')
                    disp('3. x_0')
                    var3=input('Type 1, 2, or 3: ');
                    if(var3==1)
                        disp('        ')
                        A2=input('Enter a second A value: ');
                        disp('            ')
                        disp('One moment please...')
                        y2=labone(A2,B,x0);
                        flag3=1;
                    elseif(var3==2)
                        disp('        ')
                        B2=input('Enter a second B value: ');
                        disp('            ')
                        disp('One moment please...')
                        y2=labone(A,B2,x0);
                        flag3=1;
                    elseif(var3==3)
                        disp('        ')
                        x02=input('Enter a second x_0 value: ');
                        disp('            ')
                        disp('One moment please...')
                        y2=labone(A,B,x02);
                        flag3=1; 
                    else
                        disp('         ')
                        disp('Pardon?')
                        disp('           ')
                    end
                end
            
            elseif(var2==2)
                disp('            ')
                flag2=1;
                if(var1==1)
                    plot(y1(1,:),y1(2,:))
                    xlabel('Time')
                    ylabel('State')
                elseif(var1==2)
                    plot(y1(1,:),y1(3,:))
                    if(A==0)
                        axis([0 1 -1 1])
                    end
                    xlabel('Time')
                    ylabel('Adjoint')
                elseif(var1==3)
                    plot(y1(1,:),y1(4,:))
                    if(A==0)
                        axis([0 1 -1 1])
                    end
                    xlabel('Time')
                    ylabel('Control')
                elseif(var1==4)
                    subplot(3,1,1);plot(y1(1,:),y1(2,:))
                    subplot(3,1,1);xlabel('Time')
                    subplot(3,1,1);ylabel('State')
                    subplot(3,1,2);plot(y1(1,:),y1(3,:))
                    if(A==0)
                        axis([0 1 -1 1])
                    end
                    subplot(3,1,2);xlabel('Time')
                    subplot(3,1,2);ylabel('Adjoint')
                    subplot(3,1,3);plot(y1(1,:),y1(4,:))
                    if(A==0)
                        axis([0 1 -1 1])
                    end
                    subplot(3,1,3);xlabel('Time')
                    subplot(3,1,3);ylabel('Control')
                end
                
            else
                disp('           ')
                disp('Pardon?')
                disp('            ')
            end
        end
        
    else 
        disp('     ')
        disp('Pardon?')
        disp('          ')
    end
end

if(var2==1)
    if(var1==1)
        plot(y1(1,:),y1(2,:),'b-',y2(1,:),y2(2,:),'g-')
        xlabel('Time')
        ylabel('State')
        legend('First value','Second value',0)
    elseif(var1==2)
        plot(y1(1,:),y1(3,:),'b-',y2(1,:),y2(3,:),'g-')
        if(A==0)
            axis([0 1 -1 1])
        end
        xlabel('Time')
        ylabel('Adjoint')
        legend('First value','Second value',0)
    elseif(var1==3)
        plot(y1(1,:),y1(4,:),'b-',y2(1,:),y2(4,:),'g-')
        if(A==0)
            axis([0 1 -1 1])
        end
        xlabel('Time')
        ylabel('Control')
        legend('First value','Second value',0)
    elseif(var1==4)
        subplot(3,1,1);plot(y1(1,:),y1(2,:),'b-',y2(1,:),y2(2,:),'g-')
        subplot(3,1,1);legend('First value','Second value',0)
        subplot(3,1,1);ylabel('State')
        subplot(3,1,1);xlabel('Time')
        subplot(3,1,2);plot(y1(1,:),y1(3,:),'b-',y2(1,:),y2(3,:),'g-')
        if(A==0)
            axis([0 1 -1 1])
        end
        subplot(3,1,2);xlabel('Time')
        subplot(3,1,2);ylabel('Adjoint')
        subplot(3,1,2);legend('First value','Second value',0)
        subplot(3,1,3);plot(y1(1,:),y1(4,:),'b-',y2(1,:),y2(4,:),'g-')
        if(A==0)
            axis([0 1 -1 1])
        end
        subplot(3,1,3);xlabel('Time')
        subplot(3,1,3);ylabel('Control')
        subplot(3,1,3);legend('First value','Second value',0)
    end
end
