Introduction to MATLAB Introductory Problems 1. Create a vector that contains the numbers 1, 2, 3, 4, 5: vector = [ 1 2 3 4 5 ]; vector = [ 1:5 ]; 2. Clear the Workspace and Command windows: clear clc 3. Create a matrix with the same contents as the following table: 4 5 6 10 50 3 15 56 20 Matrix = [ 4 5 6; 10 50 3; 15 56 20 ]; Alternatively: 1 2 3 4 V1 = [4 5 6]; V2 = [10 50 3]; V3 = [15 56 20]; Matrix1 = [V1; V2; V3]; 5 6 % The semicolon between V1, V2 and V3 specify that we want to insert V2 ... and V3 into new rows of the matrix. Moreover, Matrix = [V1, V2, V3] ... would insert all variables into the same row. 4. Transpose the matrix from (3) using an inverted comma (’): 4 5 6 10 15 50 56 3 20 Matrix2 = [ 4 5 6; 10 50 3; 15 56 20 ]'; Or TransposedMatrix = Matrix'; Note: You cannot use spaces in variable names, instead remove the spaces or use an underscore. 1 5. Vertically concatenate matrices 1 and 2: Matrix3 = cat(1, Matrix1, Matrix2); 6. Horizontally concatenate matrices 1 and 2: Matrix4 = cat(2, Matrix1, Matrix2); 7. Create a cell array that contains matrix 1 in slice 1 and matrix 2 in slice 2: 1 2 3 CellArray = zeros(3, 3, 2); CellArray(: ,: ,1) = Matrix1; CellArray(: ,: ,2) = Matrix2; 4 5 % zeros creates an empty matrix, as given: 6 7 zeros(Number Columns, Number Rows, Number Slices) 8 9 %We use a colon in the second and third lines to select all elements of ... each column and row. 8. Determine the third power of each element within vector (a): a = [4 6 7].ˆ3 9. Multiply each element of vector (a) by the respective element in vector (b): 1 2 3 a = [4 6 7]; b = [3 4 5]; c = a.*b; 10. Multiply each element of matrix 1 by the respective element in matrix 2 1 2 Matrix5 = Matrix1. * Matrix2; % Removing the dot (.) will perform matrix multiplication, which ... calculates a very different result. Check for yourself. In these ... problems, we essentially use matrices as arrays to store data. 11. Create an empty 3 x 3 x 2 array and fill the centre of slices 1 and 2 with 6 and 7 respectively. 1 2 3 Array = zeros(3,3,2); Array(2,2,1) = 6; Array(2,2,2) = 7; 2 12. Insert numbers 5:13 into a 3 by 3 matrix using a for loop: 1 TempMatrix = zeros(3,3); 2 3 for i = 1:9; 4 5 6 a = 4 + i; TempMatrix(i) = a; 7 8 end 13. Insert numbers 5:13 into a 3 by 3 matrix using a while loop: 1 2 3 TempMatrix= zeros(3,3); i = 1; a =5; 4 5 6 7 8 while i < 10 TempMatrix(i) = a + i; i = i + 1; end 14. Mulitply each row of matrix 1 by the following factors using a nested loop. 4 5 6 10 50 3 15 56 20 Row 1 Multiplication Factor = 7 Row 2 Multiplication Factor = 3 Row 3 Multiplication Factor = 4 1 2 3 4 5 Matrix1 = [ 4 5 6; 10 50 3; 15 56 20]; NewMatrix = zeros(3,3); RowNumber = 1; ColumnNumber = 1; RowFactor = 7; 6 7 8 9 10 11 for i = 1: 3 for j = 1:3 NewMatrix(RowNumber, ColumnNumber) = ... Matrix1(RowNumber, ColumnNumber) * RowFactor; end 12 13 14 if RowNumber == 3 RowFactor = 3; 15 16 elseif RowNumber == 3 && ColumnNumber == 2; 3 RowFactor = 4; 17 end 18 19 end Alternative Method: 1 2 3 4 Matrix1 = [ 4 5 6; 10 50 3; 15 56 20]; NewMatrix = zeros(3,3); RowNumber = 1; ColumnNumber = 1; 5 6 7 8 9 while Column Number < 4 && Row Number < 4 NewMatrix(Row Number, Column Number) = ... Matrix1(Row Number, Column Number) * Row Factor; Row Number = Row Number + 1; 10 if Row Number == 4; Column Number = Column Number + 1; Row Number = 1; Row Factor = 3; 11 12 13 14 15 elseif Row Number == 1 && Column Number == 3; Row Factor = 4; end 16 17 18 19 20 end Problem Section 2 Problem 1 Determine the first 100 numbers in the fibonacci sequence and plot. The first two numbers in the sequence are: F1 = 0 F2 = 1 Where Fn = Fn−1 + Fn−2 1 2 f0 = 0; f1 = 1; 3 4 5 6 fibonacci Sequence = zeros(100,1); fibonacci Sequence(1) = 0; fibonacci Sequence(2) = 1; 7 8 for i = 1:98 9 10 fibonacci Sequence(i+2,1) = fibonacci Sequence(i+1,1) + ... fibonacci Sequence(i,1); 4 11 12 end 13 14 15 16 17 plot(1:100,fibonacci Sequence); xlabel('x coordinate'); ylabel('Fibonacci Sequence'); title('Problem 1'); Problem 2 Create a matrix, which contains the following in each column using for/ while loop (where x = 1:100). Plot the functions on the same graph and try the subplot function. Note: You must use the command: hold on to plot several functions on the same graph. Use help subplot in the command window Column Column Column Column 1 1: 2: 3: 4: y y y y =x = x2 = x3 = x4 Matrix = zeros(20,4); 2 3 4 5 for i = 1:20 Matrix(i,1) = i; end 6 7 8 9 for i = 1:20 Matrix(i,2) = iˆ2; end 10 11 12 13 for i = 1:20 Matrix(i,3) = iˆ3; end 14 15 16 17 for i = 1:20 Matrix(i,4) = iˆ4; end 18 19 20 21 22 23 24 25 26 27 figure(1); plot(Matrix(:,1), Matrix(:,2), hold on plot(Matrix(:,1), Matrix(:,3), plot(Matrix(:,1), Matrix(:,4), xlabel('x') ylabel('y') title('Problem 2') legend(' y = xˆ2', 'y=xˆ3', 'y '−−r.'); '−−b.'); '−−k.'); = xˆ4') 28 29 figure(2); 30 31 for i = 1:3; 5 subplot(3,1,i) plot(Matrix(:,1), Matrix(:,i+1), '−−k.'); xlabel('x') ylabel('y') 32 33 34 35 36 end Problem 3 Create a matrix, which contains the following in each column using a nested loop (where x = 1: 100) Column Column Column Column 1 1: 2: 3: 4: y y y y =x = x2 = x3 = x4 Matrix = zeros(20,4); 2 3 4 for j = 1:4 for i = 1:20 5 h = i; if j == 2 h = iˆ2; elseif j == 3 h = iˆ3; elseif j == 4 h = iˆ4; end Matrix(i,j) = h; 6 7 8 9 10 11 12 13 14 15 end 16 17 18 end Problem 4 We have collected absorbance data for Cytochrome C. Import sample and control absorbance files into MATLAB. Write a function to perform a baseline correction, plot the baseline corrected absorbance, and calculate the concentration using the Beer Lambert Law A=e∗l∗c Pathlength = 1cm Molar extinction Coefficient 280nm = 11585 M-1 cm-1 Wavelength Range: 400 to 250 nm, 0.2 nm steps. Note: Baseline Absorbance data has not been stored the same as Cytochrome C Absorbance data, you must either rearrange both datasets into columns or rows. 6 1 function [Output] = ProteinConc(Wavelength, Sample Data, Control Data) 2 % In the Home Tab, select Import Data; import Sample.txt and ... Control.txt files. 3 4 % Wavelength: Baseline Corrected Data(:,1) = Wavelength; 5 6 7 % Absorbance: Baseline Corrected Data(:,2) = Sample Data − Control Data; 8 9 10 assignin('base','Baseline Corrected Absorbance', ... Baseline Corrected Data) % assignin saves variables in the workspace 11 12 13 % Plot: plot(Baseline Corrected Data(:,1), Baseline Corrected Data(:,2)) xlabel('wavelength') ylabel('Absorbance') title('Protein Concentration') 14 15 16 17 18 19 % Determine Concentration: Number Data Points below 280nm = (280−250)/0.2; 20 21 22 Abs 280nm = Baseline Corrected Data(Number Data Points below 280nm, 2); 23 24 Concentration = (Abs 280nm / (1 * 11585)) * 1000000; 25 26 Output = sprintf('Absorbance at 280 nm =%d; Cytochrome C ... Concentration = %d micromolar', Abs 280nm, Concentration); 27 28 29 end 30 31 % Type the following in the Command Window: 32 33 ProteinConc(Wavelengthnm,CytochromeCAbs, ControlAbs) 7