Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 116: Introduction to Scientific Programming Lecture 6: Scripts and publishing, Creating matrices.

Similar presentations


Presentation on theme: "COMP 116: Introduction to Scientific Programming Lecture 6: Scripts and publishing, Creating matrices."— Presentation transcript:

1 COMP 116: Introduction to Scientific Programming Lecture 6: Scripts and publishing, Creating matrices

2 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’)

3 Scripts Create

4 Write the script Write the commands

5 Save to current folder

6 Run the script Script should be in the current folder To run test.m >>test

7 Comments in scripts

8 Exercise 1 Write a script that plots a square with dotted red edges/lines

9 Publishing Output your scripts as HTML or Microsoft Word Files

10 Cells in Scripts Structure your code using cells http://www.mathworks.com/demos/matlab/developi ng-code-rapidly-with-cells-matlab-video- tutorial.html

11 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’);

12 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

13 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.

14 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

15 Matrices Each element of the matrix is a variable (memory location) To create this 3x4 matrix ◦ y=[3 4 8 41; 35 7 22 11; 8 11 27 2] An array is a 1xn matrix 34841 3572211 8 272

16 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)

17 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

18 Matrix indexing m=[3 6 4 5; 1 2 3 9; 11 2 1 9] Individual elements ◦ m(2,3) ◦ m(2,1)=7 Rows ◦ m(2,:)=[7 1 6 4] ◦ m(3,:) Columns ◦ m(:,2) ◦ m(:,3)=[1; 9; 7] Block ◦ m(2:3,2:end)

19 Assignment 1 Images as matrices


Download ppt "COMP 116: Introduction to Scientific Programming Lecture 6: Scripts and publishing, Creating matrices."

Similar presentations


Ads by Google