%% Getting Started with MATLAB, Part 2
%
% MATLAB is a high-performance technical computing language. The name MATLAB stands for Matrix Laboratory. This suggests that at its core MATLAB is a matrix centered programming language.
% MATLAB allows matrix manipulations, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages, including C, C++, C#, Java, Fortran and Python.

clc;clear;

%
%% Linear Algebra
% Let's load the magic square text file we created from Part 1. Note: This file needs to be in your course directory.

load magicSquare.txt;
A= magicSquare

% We will also create another matrix of dimension 4x4. We will use the "rand" command which creates random uniform variates Unif(0,1).

B= rand(4,4)

% We can perform elementwise multiplication by using the '.*' notation.

A.*B

% The ' . ' notation can be applied to several different operations. For example we can take the elementwise power operation by ' .^  ' or we can take the elementwise division operation as ' ./ '

A.^2
A./B

% We can invert a matrix by using the ' inv() ' command

inv(B)

% We can perform matrix multiplication by simply using the ' * ' operator. But remember that the two matrices need to conform (conformable). If the matrices are not conformable then you will receive an error.

A*B

% Kronecker Tensor Product
% The Kronecker product, kron(X,Y), of two matrices is the larger matrix formed from all possible products of the elements of X with those of Y. If X is m-by-n and Y is p-by-q, then kron(X,Y) is mp-by-nq. The elements are arranged such that each element of X is multiplied by the entire matrix Y:

a=[1 2;3 4];
b=[4 3;2 1];
kron(b,a)

% We can calculate the eigenvalues and eigenvectors of a matrix using the eig() function.

eig(A)

%
%% Creating for, else, and while loops

clc;clear;

% We can create a' for' loop using the syntax

for i = 1:10
    fprintf('iteration number: %d\n', i);
end

% We can use conditional statements to control iteration loops. Try to figure out what this code block will do before you run it...

count=1;

for i = 1:10
    
   x=0.01;
   
   whileCount=1;
   
   while x < 0.75
       
       x= rand(1);
       
       results(whileCount,i) = x;
       
       count=count+1;
       
       whileCount=whileCount+1;
       
    end
end

count

% We can also use 'else' and 'elseif' statements to act on multiple conditional statements

if count < 40
    fprintf('count is less than 40 and the number is: %d\n ', count);
elseif count > 40
    fprintf('count is greater than 40 and the number is: %d\n', count);
end

%
%% Creating functions in MATLAB

clc;clear;

% creating inline functions in MATLAB is simple and useful

f_x = @(x) x.^2 + x
f_x(3)

% Anonymous functions (inline functions) require that you explicitly specify the input arguments as you would for a standard function, separating multiple inputs with commas. For example, this function accepts two inputs, x and y:

f_xy = @(x,y) (x^2 + y^2 + x*y);

x = 1;
y = 10;
z = f_xy(x,y)

% However, you do not explicitly define output arguments when you create an anonymous function. If the expression in the function returns multiple outputs, then you can request them when you call the function. Enclose multiple output variables in square brackets.
% For example, the ndgrid function can return as many outputs as the number of input vectors. This anonymous function that calls ndgrid can also return multiple outputs:

c = 10;
mygrid = @(x,y) ndgrid((-x:x/c:x),(-y:y/c:y));
[x,y] = mygrid(pi,2*pi);

% You can use the output from mygrid to create a mesh or surface plot:

z = sin(x) + cos(y);
mesh(x,y,z)

% We can also build functions in ' .m ' files and reference those functions in MATLAB just like we would call any built-in function in MATLAB.
% There is a file on the lab web page called 'objective_Fn_1.m'. Download this file and place it in your course directory on your computer. Now simply call the function by typing ' objective_Fn_1(0.5) '.



% We can plot the output from the function,

x=linspace(-1,1, 201);
plot(x, objective_Fn_1(x));

%
%% References
%
% Works, M. (2005). Getting Started with MATLAB, Version 7. MA: Math Works.