%%
%                  Self-study-1.m file  
%               Author: The First Math team
%comments and remarks can be send to: janotto@math.aau.dk
% Task 1
%% Chapter 1 Exercise 1
disp('Chapter 1 Exercise 1')

%1.1
A=[2.1  3.2  6.1 -2.3;
   1.3 -2.5 -1.7  1.5;
  -1.2  1.5  4.3  2.4;
   4.1  2.0  5.1  4.2;
   6.1 -1.4  3.0  -1.3]
    
% (a)
disp('1.1a)')
x1=[1.5; -2.2; 2.7; 4]
Ax1 =A*x1

% (b)
disp('1.1b)')
x2=[2; 2.1; 0.0; -1.1]
Ax2= 'replace this text by the command for vector matrix multiplication'

% (c)
disp('1.1c)')
x3='type here the column vector x'
Ax3='replace this text by the command for vector matrix multiplication'

%% Chapter 1 Exercise 2
disp('Chapter 1 Exercise 2')

A=[1.3 2.1 -3.3  4.1;
   6.1 2.4 -1.3 -3.1;
  -2.2 5.1  3.2  2.1;
   2.2 6.1  7.2 -5.1]

B=[2.1 -1.1  1.2  4.2;
  -4.6  8.1  9.2 -3.3;
   2.5  5.2 -3.3  4.2;
  -0.7  2.8 -6.3  4.7]

v=[3.2;-4.6;1.8;7.1]

%(a)
disp('1.2a)')
Answer_a=3*A-2*B
%% 

%(b)
disp('1.2b)')
Answer_b=A-4*transpose(B)
%% 

%(c)
disp('(1.2c)')
P=(1/2)*(A+transpose(A))
%% 

%(d)
disp('(1.2d)')
Q='replace this text by the command to define Q'
%% 

%(e)
disp('1.2e)')
P_transpose=transpose(P)

Q_transpose='replace this text by the command to obtain the transpose of Q'

P_plus_Q='replace this text by the command add Q to P'
%% 

%(f)
disp('1.2f)')
Av='replace this text by the command to multiply v with A'
%% 

%(g)
disp('1.2g)')
BAv=B*(A*v)
%% 

%(h)
disp('1.2h)')
ABv= 'replace this text by the command to multiply v by B, and then the outcome by A'
%% 

%(i)
disp('1.2i)')
x=[3.5;-1.2; 4.1; 2]
Ax=A*x
w_is_x=x
%%

%(j)
disp('1.2j)')
disp('...')

%% Chapter 1 Exercise 4
disp('Chapter 1 Exercise 4')

%(a)
A=[1.1  2.0  4.2  2.7  1.2  0.1;
   3.1 -1.5  4.7  8.3 -3.1  2.3;
   7.1 -8.5  5.7 19.5 -11.7 6.7;
   2.2  4.0  8.4  6.5   2.1 -3.4]
%%
disp('1st cycle')
A(1,:)= (1./1.1)*A(1,:)   %make the first element in the first row equal to 1
A(2,:)=A(2,:) - 3.1*A(1,:)%make the first element in the 2nd row equal to 0
A(3,:)=A(3,:) - 7.1*A(1,:)%make the first element in the 3rd row equal to 0
A(4,:)=A(4,:) - 2.2*A(1,:)%make the first element in the 4th row equal to 0
%%
disp('2nd cycle')
A(2,:)= (1/A(2,2))*A(2,:)  %make the second element in the 2nd row equal to 1
A(3,:)= (1/A(3,2))*A(3,:)  %make the second element in the 3rd row equal to 1
A(3,:)=  A(3,:) - A(2,:)   %make the second element in the 3rd row equal to 0
%%
disp('3rd cycle')
temp=A(4,:)                %created a copy of row 4
A(4,:)= A(3,:)             %switch rows: row 4 is replaced by row 3 ..
A(3,:)= temp               %and row 3 by row 4  
%%
disp('4th cycle')
A(3,:)= (1/A(3,4))*A(3,:) %make the third element in the 3rd row equal to 1
%%
disp('5th cycle :-)')
A(1,:)= -A(1,2)*A(2,:)+A(1,:)
%%
disp('6th cycle :-)')
A(1,:)= -A(1,4)*A(3,:)+A(1,:)
A(2,:)= -A(2,4)*A(3,:)+A(2,:)
disp('... and finished as the matrix is in reduced echelon form....')
disp('... so,what does the rref command say ....?')
%%
rref_A=rref(A)
disp('the same ...! of course .. :-)')

%% Chapter 1 Exercise 5
disp('Chapter 1 Exercise 5')

A=[1.1  2.0  4.2  2.7   1.2  0.1;
   3.1 -1.5  4.7  8.3  -3.1  2.3;
   7.1 -8.5  5.7 19.5 -11.7  6.7;
   2.2  4.0  8.4  6.5   2.1 -3.4]
%(a)
disp('Exercise 5a)')
b=[-1.; 2.3; 8.9; 1.6]
R_c= rref([A,b])
disp('Answer for a) consistent')
%%
%(b)
disp(' ')
disp('Exercise 5b)')
b='replace this text by defing the vector b',
R_c= 'replace this text by the proper rref command'
disp('your answer ...')
%%
%(c)
disp(' ')
disp('Exercise 5c)')
b='replace this text by defing the vector b',
R_c= 'replace this text by the proper rref command'
disp('your answer ...')
%%
%(d)
disp(' ')
disp('Exercise 5d)')
b='replace this text by defing the vector b',
R_c= 'replace this text by the proper rref command'
disp('your answer ...')

%% Chapter 1 Exercise 7
disp('Chapter 1 Exercise 7')
% 7a)
disp('Exercise 7a)')
s1= [1;  2; -1; 3;  2; 1];
s2= [1;  0;  1; 1;  0; 1];
s3= [2;  1; -1; 2;  0; 1];
s4= [3; -1;  1; 2; -1; 1];
s5= [0;  1;  1; 2;  2; 1];

disp('Place the vectors in the set as as column vectors in a matrix:')
A=[s1, s2, s3, s4, s5]

disp('Reduced row echelon form is:')

rref_A= 'give rref command here'

disp('Not full rank so linear dependent and from the reduced echelon form and the column correspondence property')
disp('a5= .. + .. - .. We can check this by computing a5- .. - .. + .. as this should give the zero vector ')
verify='A(:,5)-A(:,?) - A(:,?) + A(:,?)'
disp('is indeed zero ...')
%%
% 7b)
disp(' ')
disp('Exercise 7b)')
s1= [ 2; 1; -1;  1;  3;  1];
s2= [ 1; 2;  1;  1;  0; -1];
s3= 'define column vector s3'
s4= 'define column vector s4'
s5= 'define column vector s5'

disp('Place the vectors in the set as as column vectors in a matrix:')
A='[s1, s2, s3, s4, s5]' 

disp('Reduced row echelon form is:')
rref_A='rref(A)'
disp('answer ... ')

%% Chapter 1 Exercise 8
disp(' ')
disp('Chapter 1 Exercise 8')
disp('Exercise 8a')
%8a)
a1= [1;  2; -1; 3;  2; 1];
a2= [1;  0;  1; 1;  0; 1];
a3= [2;  1; -1; 2;  0; 1];
a4= [3; -1;  1; 2; -1; 1];
a5= [0;  1;  1; 2;  2; 1];

A=[a1, a2, a3, a4, a5]

disp('vector b for exercise a equals:')
b_a= [14; 2; -1; 12;-1; 5] %vector b for exercise a

disp('vector b for exercise b equals:')
b_b= [ 4; 3; -2;  7; 3; 2] %vector b for exercise b

disp('vector b for exercise c equals:')
b_c= [10; 6; -5; 13; 3; 5] %vector b for exercise c

disp('vector b for exercise d equals:')
b_d= [ 1; 6; -5;  4; 3; 1] %vector b for exercise d

%(a)
disp(' ')
disp('Exercise 8a, [R c] is:')
R_c=rref([A,b_a])
disp('reduced echelon form of augmented matrix (that is [R c]) is inconsistent so: no' )
%%
%(b)
disp(' ')
disp('Exercise 8b, [R c] is:')
R_c='rref(...)'
disp('From [R c] above and column correspondence property:')
disp(' answer ...')
%%
%(c)
disp(' ')
disp('Exercise 8c, [R c] is:')
R_c='rref(...)'
disp('From [R c] above and column correspondence property:')
disp('answer ...')
%%
%(d)
disp(' ')
disp('Exercise 8d, [R c] is:')
R_c=rref([A,bd])
disp('answer ... ')

% Exercise 94, 95, 96 of page 90+91

%% Task 2
disp('Task 2')
A = [1 -1 -3 1 -1 ; -2 2 6 0 -6 ; 3 -2 -8 3 -5]
b = [-2 ; -6 ; -7]

w = linsolve(A, b)

Aw_minus_b=A*w - b

nullA=null(A)

u=nullA(:,1); 
v=nullA(:,2);
s=10 ; t=-67 %random values

x = w +s*u+t*v

Ax_minus_b = A*x - b

s=sqrt(101.89) ; t=exp(5.6789) %random values
x = w +s*u+t*v
Ax_minus_b = A*x - b

%% Exercise 94 page 55
disp(' ')
disp('Exercise 94')

disp('Hint:you can use the outline as in the given in the practice problem above, just copy here and update the matrix A and vector b')

%% Exercise 95 page 55
disp(' ')
disp('Exercise 95')

disp('Hint as above')

%% Exercise 96 page 55
disp(' ')
disp('Exercise 96]')

disp('Hint:as above. Note that here b is not in the column space of A(!)')



%% Task 3: Exercise 3 and 6 page 90,91
disp(' ')
disp('Exercise 3 and 6')

disp(' ....  ')



%% Task 4: Exercise 94,95 and 96 by method book.
disp('Exercise 95, 95 and 96')


disp(' ....  ')


%%

