210 likes | 318 Views
In this lecture, we will cover essential scientific programming concepts such as matrices, vectors, and basic plotting. Learn how to create and manipulate arrays, index values, and utilize helpful commands like `doc`, `help`, and `lookfor`. We will explore 2D plotting basics, including line types, styles, and colors, and will have hands-on exercises to create plots. Additionally, the session will guide you in writing and publishing scripts, ranging from outputs in HTML to Microsoft Word format, ensuring your work is well-documented and presentable.
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