COMP 116: Introduction to Scientific Programming Lecture 6: Scripts and publishing, Creating matrices
Recap Very Very useful commands ◦ doc e.g. >> doc rand ◦ help ◦ lookfor Creating arrays ◦ x=linspace(0,2*pi,100); Plotting ◦ >>plot(x, sin(x), ’:r’) ◦ >>plot(x, sin(x) ’-og’)
Scripts Create
Write the script Write the commands
Save to current folder
Run the script Script should be in the current folder To run test.m >>test
Comments in scripts
Exercise 1 Write a script that plots a square with dotted red edges/lines
Publishing Output your scripts as HTML or Microsoft Word Files
Cells in Scripts Structure your code using cells ng-code-rapidly-with-cells-matlab-video- tutorial.html
Creating a publishable script Same as regular script except for the % command, which acts like a comment, but is also used to separate commands into groups (cells, sections) for publication purposes. % Sine curves: The ups and downs of life; % Create Data x=linspace(0,2*pi,100); y=sin(x); % Now plot x vs. y: The Basic solid blue plot plot( x, y); % The dotted red line plot(x,y,’:r’); % Green line with green squares plot(x,y,’sg’);
Publish Example To publish the script in HTML >> publish( 'pub_circle', 'html' ) Or in MS Word >> publish( 'pub_circle', ‘doc' ) Note: You can also publish from the script Editor window using the File →Publish menu item, defaults to HMTL
Exercise 1I Write and publish a script that plots ◦ A triangle with solid blue lines/edges ◦ A square with dotted red lines/edges Use cells and comments in the script so that the published report can be read and understood by others.
Text Markup for Publishing Bold Text ◦ Use * * ◦*comment 1* Italics ◦ Use _ _ ◦_comment 2_ Monospacing ◦ Use | | ◦|comment 3| Hyperlinked Text ◦ Bulleted Text Items ◦* Item 1 ◦* Item 2 Numbered Items ◦# Item 1 ◦# Item 2 Note: You can also use the Editor window to do text markup using the Cell →Insert Text Markup → menu item
Matrices Each element of the matrix is a variable (memory location) To create this 3x4 matrix ◦ y=[ ; ; ] An array is a 1xn matrix
Matrix operations m=[3 6 4; 1 2 3] Matrix-Scalar operations >>m=m*2 >>n=m-3 Matrix-matrix operations (must be same size) >>p=m+n >>q=m.*n (use. for multiplication and division)
Creating matrices >>rand(m,n) mxn matrix of random numbers >>zeros(m,n) all zeros >>ones(m,n) all ones >>eye(m,n) identity matrix >>diag(v) diagonal matrix >>doc to get detailed documentation e.g. >>doc diag
Matrix indexing m=[ ; ; ] Individual elements ◦ m(2,3) ◦ m(2,1)=7 Rows ◦ m(2,:)=[ ] ◦ m(3,:) Columns ◦ m(:,2) ◦ m(:,3)=[1; 9; 7] Block ◦ m(2:3,2:end)
Assignment 1 Images as matrices