210 likes | 311 Views
COMP 116: Introduction to Scientific Programming . Lecture 5 : Plotting, Scripts and publishing. Recap. Matrices and vectors Creating: A=[1:8] B = [ 1 2 3; 4 5 6] Array Indexing x = A(3 ) x = A(1:4). Today. Plotting Scripts Publishing scripts. Very Very useful commands.
E N D
COMP 116: Introduction to Scientific Programming Lecture 5: Plotting, Scripts and publishing
Recap • Matrices and vectors • Creating: • A=[1:8] • B= [1 2 3; 4 5 6] • Array Indexing • x = A(3) • x = A(1:4)
Today • Plotting • Scripts • Publishing scripts
Very Veryuseful commands • doc <command_name> • Documentation for <command_name> • E.g. >> doc rand • help <command_name> • lookfor <query> • finds commands with matching description
2D Plotting Basics • The basic line plot • plot(x, y) % plots y vs. x Try >> x = [1:5]; >> y = 2*x; >> plot(x, y);
More on plot plot( x, y, ‘LineSpec’, ‘PropertyName’, ‘PropertyValue’, … ); Try: >> x = linspace(-2,2,100); >> y = x.^3; >> plot( x, y )
Line Specifiers • Line Style: Solid, dashed, dotted, dash-dot • Line Color: Red, green, blue, yellow, … • Marker Style: <None>, Circle, Square, … Try:(dashed red circle) >> x = linspace(-2,2,100);; >> y = x.^3; >> plot( x, y, ‘--ro’ ); >> axis equal;
Explore the following commands • title • xlabel, ylabel • title • legend
Exercise 1 • Plot x vs. sin(x) in the range [0,2*pi] • Solid blue line • Dotted red line • Dashed green line with square markers
Scripts • Create
Write the script • Write the commands
Run the script • Script should be in the current folder • To run test.m >>test
Exercise 1I • Write a script that plots x vs. sin(x) in the range [0,2*pi] • Solid blue line
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 • 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. Try: %% Publishing Reports - Simple Example %% Area of a Circle % Let us draw a circle in polar coordinates given by % t = 0:0.01:2*pi; % r(t) = t; %% Create Vectors t and r t = linspace( 0, 2*pi, 360 ); r = t; % make r same size as t r(1:end) = 2; % set radius to 2 %% Now plot t vs. r using polar plot polar( t, r, 'ro' ); Remember to save script file as pub_circle.m
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 1II • Write and publish a script that plots x vs. sin(x) in the range [0,2*pi] • Solid blue line • Dotted red line • Dashed green line with square markers • Use cells and comments in the script so that the published report can 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