COMP 116: Introduction to Scientific Programming Lecture 5: Plotting, Scripts and publishing.

21
COMP 116: Introduction to Scientific Programming Lecture 5: Plotting, Scripts and publishing

Transcript of COMP 116: Introduction to Scientific Programming Lecture 5: Plotting, Scripts and publishing.

COMP 116: Introduction to Scientific Programming

Lecture 5: Plotting, Scripts and publishing

RecapMatrices and vectors

◦Creating: A=[1:8] B = [1 2 3; 4 5 6]

◦Array Indexing x = A(3) x = A(1:4)

TodayPlotting

Scripts

Publishing scripts

Very Very useful 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 plotplot( x, y, ‘LineSpec’, ‘PropertyName’, ‘PropertyValue’, … );

>> x = linspace(-2,2,100);>> y = x.^3;>> plot( x, y )

Name Opt/Req Description

X Optional Values on x-axis, default values to 1,2,3,...

y Values on y-axis

'LineSpec' Optional Line specifiers, Default = solid line, no marker, blue color

'PropertyName' Optional Additional properties like LineWidth, MarkerSize, ...

'PropertyValue' Optional Numeric values associated with additional properties

Try:

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 commandstitlexlabel, ylabeltitlelegend

Exercise 1Plot x vs. sin(x) in the range

[0,2*pi]◦Solid blue line◦Dotted red line◦Dashed green line with square

markers

ScriptsCreate

Write the scriptWrite the commands

Save to current folder

Run the scriptScript should be in the current

folderTo run test.m

>>test

Comments in scripts

Exercise 1IWrite 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

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 rt = linspace( 0, 2*pi, 360 );r = t; % make r same size as tr(1:end) = 2; % set radius to 2 %% Now plot t vs. r using polar plotpolar( 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 1IIWrite 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 PublishingBold 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 markupusing the Cell→Insert Text Markup →<???> menu item