disp(' ')
disp('   This shows how one vector can be graphed ')
disp('   as a LINEAR COMBINATION of two other vectors ')
disp(' ')

disp(' Before you begin, consider this cosmetic, yet essential "warning": ')
disp('                          *******                           ')
disp('           To make your figure look "nice" (see below), ')
disp(' the lengths of your vectors must not differ by more than about a factor of 2.')
disp(' If one of the vectors is much longer (or shorter) than the others,')
disp(' then details in the figure will be poorly discernable.')
disp('                          *******                           ')
disp('   ')
disp(' Press any key to confirm that you have read the above "warning", and continue')
pause


v1 = input('enter your 1st vector (e.g., [1 2] - yes, you need square brackets): ')
v1 = v1.';       % transpose row into column

v2 = input('enter your 2nd vector, linearly INDEPENDENT fromthe 1st (e.g., [3 4]): ')
v2 = v2.';        % transpose row into column

stubborness_counter = 0;
while cond([v1 v2]) > 10^6
    stubborness_counter = stubborness_counter + 1;
    if stubborness_counter == 2
        disp(' Well, what have you you expected? ')
        disp(' ')
    elseif stubborness_counter > 2
        disp(' Huh? Still not convinced?')
        disp(' ')
    end
    disp(' Your first two vectors are LINEARLY DEPENDENT.' )
    disp(' It ain`t gonna work unless you fix this! ')
    disp(' So, try again: ')
    v2 = input('enter your 2nd vector, linearly INDEPENDENT from the 1st (e.g., [3 4]): ')
    v2 = v2.';        % transpose row into column
end

v3 = input('enter your 3rd vector; it can be arbitrary (e.g., [-5 6]): ')
v3 = v3.';        % transpose row into column

disp(' You have entered: ')
[v1 v2 v3]
disp(' (Here 1st COLUMN is your 1st vector, etc.) ')
disp(' ')
disp(' Press any key to continue')
pause

V = [v1 v2];

disp(' ')
disp(' ')
disp(' These are c1 and c2, such that  c1*v1 + c2*v2 = v3 :')
c = (V\v3).'
disp(' Press any key to continue')
pause

disp('You may need to wait a few seconds until a figure appears')
pause(0.1)

figure(17)
clf
p1 = plot([0 v1(1)], [0 v1(2)], 'r','linewidth',5);
hold on
p2 = plot([0 v2(1)], [0 v2(2)], 'b','linewidth',5);

p3 = plot([0 v3(1)], [0 v3(2)], 'k','linewidth',5);

axis('equal')
legend('v1','v2','v3', 'location', 'best')
disp(' Press any key to continue')
pause


coeffmin = -0.3;
coeffmax = 1.3;

plot([coeffmin*c(1)*v1(1)  coeffmax*c(1)*v1(1)], ...
     [coeffmin*c(1)*v1(2)  coeffmax*c(1)*v1(2)], 'r--')
plot([coeffmin*c(2)*v2(1)  coeffmax*c(2)*v2(1)], ...
     [coeffmin*c(2)*v2(2)  coeffmax*c(2)*v2(2)], 'b--')

plot(c(2)*v2(1) + [coeffmin*c(1)*v1(1)  coeffmax*c(1)*v1(1)], ...
     c(2)*v2(2) + [coeffmin*c(1)*v1(2)  coeffmax*c(1)*v1(2)], 'r--')
plot(c(1)*v1(1) + [coeffmin*c(2)*v2(1)  coeffmax*c(2)*v2(1)], ...
     c(1)*v1(2) + [coeffmin*c(2)*v2(2)  coeffmax*c(2)*v2(2)], 'b--')
 
disp(' ')
disp(' ')
disp(' Dashed lines are those which you need to draw, creating a parallelogram.')
disp(' Press any key to continue')
pause

c1p1 = plot([0  c(1)*v1(1)], [0  c(1)*v1(2)], 'r','linewidth',2);
c2p2 = plot([0  c(2)*v2(1)], [0  c(2)*v2(2)], 'b','linewidth',2);

legend([p1,p2,p3,c1p1,c2p2], {' v1',' v2',' v3',' c1*v1',' c2*v2'})

disp(' ')
disp(' ')
disp(' Finally, the thinner solid lines are vectors  c1*v1  and  c2*v2,')
disp(' thus illustrating that   c1*v1 + c2*v2 = v3 .')
disp('(Of course, if your ci*vi (i=1,2) is shorter than vi itself, ')
disp(' you will be unable to see the thinner line behind the thicker one.') 

hold off




