User:Eml4500.f08.lulz.strack/hw1

Part 14: Managing M-files
This section of the primer explains how to manage M-files.

Creating an M-file is done in three ways.
 * 1) From the MATLAB menu bar, select File -> New -> M-File
 * 2) Pressing Ctrl + N from the keyboard
 * 3) Right clicking in the Current Directory window and selecting New -> M-File

Three sample M-files were created to test each method: mfile.m, petals.m, fact.m. The files petals.m and fact.m are a script file and a function file, respectively, taken from tutorials in MATLAB's Help section. The code below displays the data entered into the files.

% This is an M-file. % An M-file script to produce      % Comment lines % "flower petal" plots theta = -pi:0.01:pi;               % Computations rho(1,:) = 2 * sin(5 * theta) .^ 2; rho(2,:) = cos(10 * theta) .^ 3; rho(3,:) = sin(theta) .^ 2; rho(4,:) = 5 * cos(3.5 * theta) .^ 3; for k = 1:4 polar(theta, rho(k,:))         % Graphics output pause end function f = fact(n) % Compute a factorial value. % FACT(N) returns the factorial of N, % usually denoted by N!
 * mfile.m:
 * petals.m:
 * fact.m:

% Put simply, FACT(N) is PROD(1:N). f = prod(1:n);

In order to edit an M-file after it has been created, the primer uses non-current commands to call the editor from the command line. An example of the correct command edit is shown below.

>> edit mfile

After this command has run, MATLAB opens Editor and displays the current version of the file where it can now be edited.

To simply display the contents of the file in the current window, the type and dbtype commands are used.

Commands:
 * type mfile - displays entire M-file
 * dbtype mfile - displays entire M-file with line numbers
 * dbtype mfile #:# - displays lines # through # of the M-file

Examples: >> type mfile

% This is an M-file. >> dbtype petals

1    % An M-file script to produce       % Comment lines 2    % "flower petal" plots 3    theta = -pi:0.01:pi;                % Computations 4    rho(1,:) = 2 * sin(5 * theta) .^ 2; 5    rho(2,:) = cos(10 * theta) .^ 3; 6    rho(3,:) = sin(theta) .^ 2; 7    rho(4,:) = 5 * cos(3.5 * theta) .^ 3; 8    for k = 1:4 9        polar(theta, rho(k,:))          % Graphics output 10       pause 11   end >> dbtype fact 1:3

1    function f = fact(n) 2    % Compute a factorial value. 3    % FACT(N) returns the factorial of N,

Working with directories
The current directory is the folder location on the hard drive where current files are being initialized from. The current directory should be displayed in a window in MATLAB.

Commands:
 * pwd OR cd - displays the current directory
 * cd .. - navigates the current directory to the one above it
 * cd ( 'directory ') - navigate the current directory to the specified one
 * dir OR ls - lists the files in the current directory in an array
 * dir *.m OR what - lists the M-files in the current directory in an array
 * delete filename - deletes a file from the directory

Examples: >> pwd

ans =

C:\Documents and Settings\...

>> cd

C:\Documents and Settings\...

>> cd .. >> cd ('C:\Documents and Settings\...') After navigating back to the original directory... >> dir

.        ..        fact.m    mfile.m   petals.m

>> ls

.        ..        fact.m    mfile.m   petals.m

>> dir *.m

fact.m   mfile.m   petals.m

>> what

M-files in the current directory C:\Documents and Settings\...

fact     mfile     petals

>> delete mfile.m >> what

M-files in the current directory C:\Documents and Settings\...

fact     petals

Part 15: Comparing efficiency of algorithms: flops, tic, toc
The first section of this part discusses flops. However, MATLAB no longer supports the counting of flops. Therefore, no commands with flops are able to be performed. Below shows the script of the flop fuunction provided by MATLAB.

function f = flops(x) %FLOPS Obsolete floating point operation count. %  Earlier versions of MATLAB counted the number of floating point %  operations. With the incorporation of LAPACK in MATLAB 6, this %  is no longer practical.

%  Copyright 1984-2004 The MathWorks, Inc. %   $Revision: 5.12.4.1 $  $Date: 2004/01/24 09:21:21 $

if (nargin < 1) || (x ~= 0) warning('MATLAB:flops:UnavailableFunction', ...       'Flop counts are no longer available.') end if nargout > 0 f = 0; end

The command including tic and toc are used to measure the elapsed time of an execution. They are timers that start and stop the clock before and after a statement.

Command: tic, statement, toc - displays the time required to execute the statement

Example: >> A = 1

A =

1

>> B = 1

B =

1

>> tic, x = A / B, toc

x =

1

Elapsed time is 0.000038 seconds.

The last command was computed multiple times. Elapsed time varied and was never less than 0.000035 seconds. Measuring efficiency with tic and toc are not reliable measurement of efficiency because elapsed time depends on how busy the computer is during execution.

Part 16: Output format
Displaying variables can be done in many ways. Output depends on how precise the variable needs to be. The format command adjusts this setting. The commands and examples are as follows.

Commands:
 * format - changes the output format to the default type of short
 * format type - changes the output format to the type specified

MATLAB provides the following information in tables in Help for the different types of formatting:

For floating-point variables
 * short - scaled fixed point format with 5 digits (default)
 * long - scaled fixed point format with 15 digits for double; 7 digits for single
 * short e - floating point format with 5 digits
 * long e - floating point format with 15 digits for double; 7 digits for single
 * short g - best of fixed or floating point with 5 digits
 * long g - best of fixed or floating point with 15 digits for double; 7 digits for single
 * short eng - engineering format that has at least 5 digits and a power that is a multiple of three
 * long eng - engineering format that has exactly 16 significant digits and a power that is a multiple of three

For all numeric variables
 * + - +, -, blank
 * bank - fixed dollars and cents
 * hex - hexadecimal (hexadecimal representation of a binary double-precision number)
 * rat - ratio of small integers

For all variables
 * compact - suppresses excess line feeds to show more output in a single screen*loose - adds linefeeds to make output more readable

Examples: >> A = 1.2345678910111213

A =

1.2346

>> format long; A

A =

1.23456789101112

>> format; A

A =

1.2346

>> format short e; A

A =

1.2346e+000

>> format long e; A

A =

1.234567891011121e+000

>> format short g; A

A =

1.2346

>> format long g; A

A =

1.23456789101112

>> format short eng; A

A =

1.2346e+000

>> format long eng; A

A =

1.23456789101112e+000

>> format +; A

A =

+

>> format bank; A

A =

1.23

>> format hex; A

A =

3ff3c0ca42c959f1

>> format rat; A

A =

100/81

>> format compact; A A = 100/81   >> format loose; A

A =

100/81

Part 17: Hardcopy
In order to obtain a copy of the commands and output of a session in MATLAB, MATLAB has a function called diary that directly copies the information displayed in the Workspace window into a file.

Commands:
 * diary - creates a file named diary that stores the output of the Workspace if no file name was specified
 * diary filename - creates a file with the name filename that stores the output of the Workspace
 * diary off - stops recording the output

Example:

What MATLAB outputs to Workspace: >> ls

.             fact.m         petals.m       ..             homework1.txt

>> diary record >> A = 1

A =

1

>> ls

.             fact.m         petals.m       ..             homework1.txt  record

>> diary off >> B = 2

B =

2

What the file record saves: A = 1

A =

1

ls

.             fact.m         petals.m       ..             homework1.txt  record

diary off

Part 18: Graphics
MATLAB can creat multi-dimensioned plots of functions, points, and other forms of data. In order to display these plots, certain commands must be called in order to output the correct display. The plots allow for great versatility with different formats of plots in a variety of colors and from two to three dimensions.

Planar plots
Planar plots graph functions and point in the x-y plane. Below is the basic form of the command and an example of how to implement it.

Command:

plot(x,y,string) - graphs vector y against vector x; string is a string of characters

The string may be omitted from the command so that the plot is a solid black line (plot(x,y)). Incorporating a string allows for dotted, dashed, and other forms of lines available in many colors. By typing in the command below, MATLAB displays the available formats as shown.

>> help plot

Available string possibilities (one element from any or all three columns): b    blue. point             -     solid g    green         o     circle             :     dotted r    red           x     x-mark             -. dashdot c    cyan          +     plus               --    dashed m    magenta       *     star             (none)  no line y    yellow        s     square k    black         d     diamond v    triangle (down) ^    triangle (up) <    triangle (left) >    triangle (right) p    pentagram h    hexagram

Examples:

Graphed as plots: >> figure(1); x = -4:0.01:4; y = sin(x); plot(x,y) >> figure(2); x = -1.5:0.01:1.5; y = exp(-x.^2); plot(x,y)



Graphed as functions function y = expnormal(x) y = exp(-x.^2); >> fplot('expnormal', [-1.5,1.5])
 * Created an M-file titled expnormal.m
 * Entered the data as shown below
 * Entered the command as shown below



Changing plot title and axis names and adding text to spots >> t = 0:0.001:2*pi; x = cos(3*t); y = sin(2*t); plot(x,y) >> title('Best Least Squares Fit') >> xlabel('X-axis') >> ylabel('Y-axis') >> gtext('The Spot')



Adding gridlines and changing the scale (continued from previous section) >> grid >> axis([-2,2,-2,2])



Multiple plots on on graph >> x = 0:0.01:2*pi; y1 = sin(x); y2 = sin(2*x); y3 = sin(4*x); plot(x,y1,x,y2,x,y3)

OR

>> x = 0:0.01:2*pi; Y = [sin(x)', sin(2*x)', sin(4*x)']; plot(x,Y) EDU>



Other types of planar plots include polar, bar, hist, quiver, compass, feather, rose, stairs, and fill.