%% This is the MATLAB code that will be used for quick introduction to MATLAB % Code is divided into section so that each can be run separately. % Recommended way of going through the examples is by selecting and % clicking F9 (evaluate selection option). %% MATLAB Variables % If you wish to print the line of code results, just ommit the semi colon % at the end of the code line. 45 pi eps P inf NaN i %% Elementary math functions % Choose a value for x arbitrairly x = 23; % Assign value to properly named variable % squareRoot = sqrt(x); exponential = exp(x); naturalLogarithm = log(x); logarithTen = log10(x); sinus = sin(x); %(in radians) % Choose a value for y arbitrairly y = 1.23; lowerValueRound = fix(y); higherValueRound = ceil(y); integerRound = round(y); %% Creating MATLAB matrices % We can create a matrix in various ways, take for example matrix: % 2 5 3 % 6 4 1 %a direct way of creating matrix - by imputing values A = [2,5,3;6,4,1] %crating matrix using range-values, the value between colons is a step %size. B = [1:1.5:6; 2 3 4 5] %a purely iterative way of creating a matrix, imputing each cell of a %2-D matrix in a nested for loop for i = 1:4 for j = 1:3 C(i,j) = i*j; end end C %creating a matrix/vector by concatenating D = []; D = [D, 5]; D = [D, 6, 7] %creating a matrix using MATLAB function E = zeros(4,5) %creating a multi-dimesional matrices is not that easy. How would you do %it? %% Matrix operations %generating two sample matrices A = [4,1,1;2,4,2;3,3,4]; B = [9,3,3;4,9,4;4,4,9]; %Addiction: C = A + B; %Subtraction: D = A - B; %Multiplication E1 = A * B %(Matrix multiplicaiton) E2 = A .* B %(Element wise multiplication, A and B are same size) %Max, Min, Sum max = max(A); min = min(A); sum = sum(sum(A)) %Division: %Left Division and Right Division F = A ./ B %(Element wise division) F1 = A/B F2 = A*inv(B) %(A * inverse of B) %are F1 and F2 different? difference = sum(sum(F1-F2)); if difference == 0 disp('A/B is the same as A*inv(B)'); else disp('A/B is the same as A*inv(B)'); end F3 = A .\ B %(Element wise division) F4 = A\B F5= inv(A)*B %(inverse of A * B) %are F4 and F5 different? difference = sum(sum(F4-F5)); if difference == 0 disp('A\B is the same as inv(A)*B'); else disp('A\B is the same as inv(A)*B'); end %Transpose: A A' %% Workspace maintenance %Sometimes workspace get too crowded! It is smart to remove unnecessary variables clearvars -except A B %Or you could just clear entire worspace clear %If command window is crowded with text, simply clean it with clc %% Generating basic matrices m = 3; n = 2; %Matrix with ZEROS: A = zeros(m, n) %Matrix with ONES: B = ones(m, n) %IDENTITY Matrix: I = eye(m, n) %RANDOM Matrix: I = rand(m, n) %MAGIC SQUARE Matrix: M = magic(n) %matrix with each entry ~ U(0,1) Rand(m,n) %standard normal distribution Randn(m,n) %% Array indexing, obtaining information % Get the dimensions of a matrix size(M) % Get a length of a vector V = [1:10:100] length(V) % Get a second cell M(2) % Get a second row M(2,:) %% Save/load data %save the workspace save sample_workspace.mat; %save a single variable save('single_variable.mat','M'); clear M %load single variable load('single_variable.mat'); %% MATLAB 2-D plotting, Example 1 % reference: http://www.mathworks.com/matlabcentral/fileexchange/35214-matlab-plot-gallery-line-plot-2d--1-/content/html/Line_Plot_2D_1.html % Define values for x, y1, and y2 x = 0: .1 : 2*pi; y1 = cos(x); y2 = sin(x); % Plot y1 vs. x (blue, solid) and y2 vs. x (red, dashed) figure plot(x, y1, 'b', x, y2, 'r-.', 'LineWidth', 2) % Turn on the grid grid on % Set the axis limits axis([0 2*pi -1.5 1.5]) % Add title and axis labels title('Trigonometric Functions') xlabel('angle') ylabel('sin(x) and cos(x)') %% MATLAB 2-D plotting, example 2 % reference: http://www.mathworks.com/matlabcentral/fileexchange/35259-matlab-plot-gallery-function-plot--1-/content/html/Function_Plot_1.html % Create the plot using the lemniscate function f(x,y) = (x^2 + y^2)^2 - x^2 + y^2 figure ezplot('(x^2 + y^2)^2 - x^2 + y^2', [-1.1, 1.1], [-1.1, 1.1]) % Adjust the colormap to plot the function in blue colormap([0 0 1]) % Add a multi-line title title({'Lemniscate Function', '(x^2 + y^2)^2 - x^2 + y^2'}) %% MATLAB 2-D plotting, example 3 % reference: http://www.mathworks.com/matlabcentral/fileexchange/35273-matlab-plot-gallery-horizontal-bar-plot/content/html/Horizontal_Bar_Plot.html % Create the data for the temperatures and months temperatures = [40.5 48.3 56.2 65.3 73.9 69.9 71.1 59.5 48.7 35.3 31.7 30.0]; months = {'Dec', 'Nov', 'Oct', 'Sep', 'Aug', 'Jul', 'Jun', 'May', 'Apr', 'Mar', 'Feb', 'Jan'}; % Plot the temperatures on a horizontal bar chart figure barh(temperatures) % Set the axis limits axis([0 80 0 13]) % Add a title title('Boston Monthly Average Temperature - 2001') % Change the Y axis tick labels to use the months set(gca, 'YTick', 1:12) set(gca, 'YTickLabel', months) %% MATLAB 2-D plotting, example 4 % reference: http://www.mathworks.com/matlabcentral/fileexchange/35268-matlab-plot-gallery-semilogy-plot/content/html/Semilogy_Plot.html % Create some data eb = 0:5; SER = [0.1447 0.1112 0.0722 0.0438 0.0243 0.0122]; BER = [0.0753 0.0574 0.0370 0.0222 0.0122 0.0061]; % Create a y-axis semilog plot using the semilogy function % Plot SER data in blue and BER data in red figure semilogy(eb, SER, 'bo-') hold on semilogy(eb, BER, 'r^-') % Turn on the grid grid on % Add title and axis labels title('Performance of Baseband QPSK') xlabel('EbNo (dB)') ylabel('SER and BER') %% MATLAB 3-D plotting, example 1 x = [0:10]; y = [0:10]; z = x'*y; figure; mesh(x,y,z); colorbar; figure; surf(x,y,z); colorbar; %% MATLAB 3-D plotting, example 2 % reference: http://www.mathworks.com/matlabcentral/fileexchange/35310-matlab-plot-gallery-function-mesh-plot/content/html/Function_Mesh_Plot.html % Create the mesh plot using the function f(x,y) = y^2 - x^2 figure ezmesh('y^2 - x^2', [-3 3], [-3 3]) %% MATLAB 3-D plotting, example 3 % reference: http://www.mathworks.com/matlabcentral/fileexchange/35240-matlab-plot-gallery-change-lighting-to-phong/content/html/Lighting_Phong.html % Create a grid of x and y points points = linspace(-2, 0, 20); [X, Y] = meshgrid(points, -points); % Define the function Z = f(X,Y) Z = 2./exp((X-.5).^2+Y.^2)-2./exp((X+.5).^2+Y.^2); % "phong" lighting is good for curved, interpolated surfaces. "gouraud" % is also good for curved surfaces surf(X, Y, Z) view(30, 30) %for reference what is asimuth and projection use this [X,map] = imread('http://upload.wikimedia.org/wikipedia/commons/thumb/f/f7/Azimuth-Altitude_schematic.svg/436px-Azimuth-Altitude_schematic.svg.png'); imtool(X,map) shading interp light lighting phong title('Lighting phong')