%% Matlab Command List - Fall 2013

% The list of commands is given as a .m file. Executing the file by typing
% commands in the windows will run all the commands. Instead try each
% command individually. Commands are lines not preceeded by %.

% Running a command - You can run a command in the command window directly.
% If you want to run more than one command at a time (a code), write all
% your commands in a .m file. You can open a new .m file by clicking on New
% and and an existing one by clicking on Open.

%% Help
% Display the help file for saved functions, here the eigenvalue
% function. A function is any .m file that starts (after the comments %)
% with the word function. If you write your own function, you can write
% your own help file. The Matlab Documentation under Help has an index that
% you can search by keyword or topic.
help eig 

%% Clear memory
clear all
clear x     % only clears x
who         % list all used variables
x = 2;
class(x)    % list the class of the variable x (string, char, double, ...)

%% Constants
% The semicolon ; suppresses the screen output
% To execute the command, press enter/return
c = 3;
d = 4
c+d
a = c.*d;   % Use .* to make sure that the multiplication is elementwise.
a
c.^4        % Use .^ to make sure that the ^ is elementwise if c is a vector.
pi          % The constant Pi

%% Functions
% There are two types of functions: symbolic, numerical, and Matlab
% A symbolic function is what we call a function in Calculus. You can take
% its derivative, its integral. Matlab has a small symbolic toolbox.
syms f(x)
f(x) = x.^2;
f(1)
% or
f=@(x) x.^2;
f(2)
% or
f=inline(x.^2,'x');
f(-1)
diff(f(x))     % Differentiate f

% A matlab function is any myfun.m file that starts with [list of
% arguments]=myfun(list of inputs). The file is saved under myfun.m. or
% inside a bigger .m file.For example, 
% y=myfun(x)
% y=x.^2;

% Compute values of a function.
% Define a vector of equally spaced values x starting at x0 and ending at
% xf. The vector x has n+1 entries and the spacing is (xf-x0)/n
x = 0:1/10:1;
%or
x = linspace(0,1,11) % 9 points in the interval, 2 endpoints = 11
% Operations on x are similar with operations on constants. Don't forget
% the period to makes sure the operations are pointwise
x.^2
y=x.^4-4.*x;
y

% Predefined functions and syntax: sin(x), cos(x), exp(x), log(x)

%% Plots
% Matlab plots data stored as vectors or tables. The color is changed with
% the option 'r' (red), 'b' (blue), 'y' (yellow), 'k' (black), 'g' (green),
% 'm' (magenta), ... The line style is changed with 'o' open circles, 'o-'
% connected open circles, '--' dashed, ':'small dashed, ... The line width
% is changed with the option 'LineWidth', 1.5
x = 0:1/10:4;
y = x.^2+2*x;
plot(x,y)
plot(x,y,'r')
plot(x,y,'o')
plot(x,y,'mo-','LineWidth',1.5)
close            % Close current plot window

% Plot multiple plots
hold on
x = linspace(0,2*pi,32);
y1 = cos(x);
y2 = sin(x);
plot(x,y1,'r')
plot(x,y2)
hold off
% or
plot(x,[y1;y2])

% To add labels, title, legend
xlabel('x')
ylabel('y')
title('This is a plot')
legend('cos','sin')

% Function of two variables
% Define a two dimensional grid with meshgrid, plot is with surf
figure          % Open a new figure window
[x,y] = meshgrid(-2:.2:2, -2:.2:2);
z = x .* exp(-x.^2 - y.^2);
surf(x,y,z)

%% Algebra
% Find roots - Given initial guess, fsolve returns a numerical
% approximation of one root. Can be used with myfun instead of @
fsolve(@(x) x.^2-3.*x+2,5)
% or symbolic
syms x
solve(x^2-3*x+2==0)

%% Linear Algebra
% Solve the linear system Ax=b for a 3x3 matrix A and a vector b
A = [1 2 3;4 5 6; 7 8 9]        % The ; separates rows
b = [0;-3;-6]                   
x = A\b
% Matlab warning: the matrix is close to singular and results are
% inaccurate. Check by doing the forward multiplication
A*x

% Matrix algebra
det(A)      % Determinant
inv(A)      % AVOID whenever possible (slow), use \
A*b         % Matrix-vector multiplication
A'          % Transpose
B = [9 8 7; 6 5 4; 3 2 1];
A+B         % Matrix addition
A*B         % Matrix multiplication
2*A+3*B
R = rref(A) % Row reduced form (Gauss-Jordan)
rref([A b]) % Row reduced form of A augmented by b
A(1,:)      % The first row of A
A(:,3)      % The third column of A

% Special matrices
zeros(3,3)  % A 3x3 matrix of zeros
zeros(3,1)  % A vector of zeros
ones(3,3)   % A 3x3 matrix of ones
ones(3,1)   % A vector of ones
eye(3,3)    % The 3x3 identity matrix (1 on the diagonal)

%% For loop
for i=1:10
    u(i+1) = i;
end
for j=2:3
    C(j,:) = A(j,:)-A((j-1),:)
end

%% If statement
a = 2;
b = 6;
if mod(b,a)==0
    c='True';
else
    c='False';
end
