190 likes | 317 Views
COMP 116: Introduction to Scientific Programming . Lecture 6: Scripts and publishing, Creating matrices. Recap. Very Very useful commands doc < command_name > e .g. >> doc rand help < command_name > lookfor <query> C reating arrays x= linspace (0,2*pi,100); Plotting
E N D
COMP 116: Introduction to Scientific Programming Lecture 6: Scripts and publishing, Creating matrices
Recap • Very Very useful commands • doc <command_name> • e.g. >> doc rand • help <command_name> • lookfor <query> • 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
Run the script • Script should be in the current folder • To run test.m >>test
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 http://www.mathworks.com/demos/matlab/developing-code-rapidly-with-cells-matlab-video-tutorial.html
Creating a publishable script %% 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’); • 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.
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 <scriptname> 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 *<string>* • *comment 1* • Italics • Use _<string>_ • _comment 2_ • Monospacing • Use |<string>| • |comment 3| • Hyperlinked Text • <URL string> • <http://www.google.com Google> • 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=[3 4 8 41; 35 7 22 11; 8 11 27 2] • 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 <command_name> to get detailed documentation e.g. >>doc diag
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)
Assignment 1 • Images as matrices