% MATLAB Introduction
% 1.050
% September 6-7, 2007
% -----------------------------------------------------------------

clear
close all
clc

% vector operations

a = [ 2 3 4 ]
b = [ 1 -2 3 ]

sizea = size(a)
sizeb = size(b)

aplusb = a + b

% dot and cross products

adotb = dot(a,b) % dot product
transb = transpose(b) % transpose of a matrix
atimestransb = a * transb % multiplication (dot product)

theta = acosd( adotb / (norm(a)*norm(b)) ) % angle between vector a and b

acrossb = cross(a,b) % cross product

% ------------------------------------------------------------------

% Matrix operations

A = [ 1 1 -1 ; 0 0 1 ; 0 -2 -2 ]
B = [ 2 1 0 ; -2 6 7 ; 3 3 1 ]

AmultB = A * B % multiplication

AarraymultB = A .* B % array multiplication

transposeA = A' % another way to perform a transpose operation

detA = det(A) % determinant

Ainv = A^(-1) % inverse
Ainv2 = inv(A)

% matrix operations

C = [-3 1 0]'

X = A\C   % solving system of equations, alternatively, you can use the next statement
X2 = inv(A)*C %

% rank operations

D = [1 -1 0
    2 1 3
    0 1 1]

E = [1 1 1 -3 3 -1 ; 1 0 0 1 0 1 ; -2 0 -2 0 0 -2]

rankD = rank(D)

rankE = rank(E)

% ------------------------------------------------------------------

% plotting routine
% example: problem of Alberto saving a friend from drowning

x = (0:1:100); %create an array of values for x

%REMEMBER: since x is an array, then for operations like * , / , ^ 
%          that are applicable to all elements in x use .* , ./ , .^

e1 = sqrt ( x.^2 + 100^2); 
e2 = sqrt ( (100-x).^2 + 50^2 ); 
v1 = 10000;
v2 = 3000;

t = e1/v1 + e2/v2;

figure(1)
plot(x,t)
xlabel('x [m]')
ylabel('T [h]')
    

