close

flag1=0;

x0 = input('Enter the initial condition of the state (x_0): ');
disp('           ')
x1 = input('Enter the final time condition of the state (x_1): ');
disp('           ')

t=linspace(0,1,250);
C=4*((x1-x0*exp(1))/(exp(-1)-exp(1)));
A=(256*(x1-x0*exp(1))^3)/(27*(exp(-1/3)-exp(1))^3);
a1=C*exp(-t);
a2=A*exp(-t);
c1=-0.5*C*exp(-t);
c2=-exp(-t/3)*(0.25*A)^(1/3);
s1=0.25*C*exp(-t) + (x0-0.25*C)*exp(t);
s2=((x1-x0*exp(1))/(exp(-1/3)-exp(1)))*(exp(-t/3)-exp(t))+x0*exp(t);
 
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. state and control')
    disp('   5. state and adjoint')
    disp('   6. control and adjoint')
    disp('   7. all three')
    var1=input('Type 1, 2, 3, 4, 5, 6, or 7: ');

    if(var1==1)
        flag1=1;
        plot(t,s1,'b',t,s2,'g')
        xlabel('Time')
        ylabel('States')
        legend('u^2','u^4',0)
    elseif(var1==2)
        flag1=1;
        plot(t,a1,'b',t,a2,'g')
        xlabel('Time')
        ylabel('Adjoints')
        legend('u^2','u^4',0)
     elseif(var1==3)
        flag1=1;
        plot(t,c1,'b',t,c2,'g')
        xlabel('Time')
        ylabel('Controls')
        legend('u^2','u^4',0)
     elseif(var1==4)
         flag1=1;
         subplot(2,1,1);plot(t,s1,'b',t,s2,'g')
         subplot(2,1,1);xlabel('Time')
         subplot(2,1,1);ylabel('States')
         subplot(2,1,1);legend('u^2','u^4',0)
         subplot(2,1,2);plot(t,c1,'b',t,c2,'g')
         subplot(2,1,2);xlabel('Time')
         subplot(2,1,2);ylabel('Controls')
         subplot(2,1,2);legend('u^2','u^4',0)
     elseif(var1==5)
         flag1=1;
         subplot(2,1,1);plot(t,s1,'b',t,s2,'g')
         subplot(2,1,1);xlabel('Time')
         subplot(2,1,1);ylabel('States')
         subplot(2,1,1);legend('u^2','u^4',0)
         subplot(2,1,2);plot(t,a1,'b',t,a2,'g')
         subplot(2,1,2);xlabel('Time')
         subplot(2,1,2);ylabel('Adjoints')
         subplot(2,1,2);legend('u^2','u^4',0)
     elseif(var1==6)
         flag1=1;
         subplot(2,1,1);plot(t,c1,'b',t,c2,'g')
         subplot(2,1,1);xlabel('Time')
         subplot(2,1,1);ylabel('Controls')
         subplot(2,1,1);legend('u^2','u^4',0)
         subplot(2,1,2);plot(t,a1,'b',t,a2,'g')
         subplot(2,1,2);xlabel('Time')
         subplot(2,1,2);ylabel('Adjoints')
         subplot(2,1,2);legend('u^2','u^4',0)
     elseif(var1==7)
         flag1=1;
         subplot(3,1,1);plot(t,s1,'b',t,s2,'g')
         subplot(3,1,1);xlabel('Time')
         subplot(3,1,1);ylabel('States')
         subplot(3,1,1);legend('u^2','u^4',0)
         subplot(3,1,2);plot(t,c1,'b',t,c2,'g')
         subplot(3,1,2);xlabel('Time')
         subplot(3,1,2);ylabel('Controls')
         subplot(3,1,2);legend('u^2','u^4',0)
         subplot(3,1,3);plot(t,a1,'b',t,a2,'g')
         subplot(3,1,3);xlabel('Time')
         subplot(3,1,3);ylabel('Adjoints')
         subplot(3,1,3);legend('u^2','u^4',0)
     else
         disp('         ')
         disp('Pardon?')
         disp('           ')
     end
end
