# Lecture 13 - Matlab_Octave_FreeMat - part 1

Document Sample

```					Lecture 13: M/O/F/ for
Engineering Applications -
Part 1

BJ Furman
03MAY2011
The Plan for Today
   Matlab/Octave/FreeMat (M/O/F) for
engineering applications
   Overview of M/O/F
   Matlab/FreeMat environment
   Octave command line
   Basic operations
   Script files
Learning Objectives
   Explain what M/O/F are
   Navigate the Matlab user interface and
Octave command line
   Use M/O/F as a scratch pad
   Create script files to execute a sequence
of commands
Matlab and Octave
   M/O/F are „high-level‟ languages
   They abstract away the nitty-gritty details
   Numerical calculation
   Oriented toward engineering, mathematical, and
scientific computing
   Matlab (Matrix Laboratory)                    Key point!
   Particularly good at handling arrays!
   Powerful graphics and analysis tools
   Controls, signal processing, data analysis, and other
specialized „toolboxes‟ are available
   Widely used in industry
   Matlab is commercial software (a student version
is available) http://www.mathworks.com/
   Octave and FreeMat are open source and free:
http://www.gnu.org/software/octave/ (main page)
http://freemat.sourceforge.net/index.html (main page)
Matlab (ver. 6.5) Environment (GUI)
Default Window Layout

Workspace/Current
Directory Window

Workspace: lists variables
Directory: files in current dir            Command
Window

Command History
Window
Octave Command Line

Enter commands at the prompt

Info on variables in
the workspace

Variables are
dynamically
typed

dynamically
typed
M/O/F Basics - 1
   Fundamental element: the array
   even scalars --> stored as 1x1 array of double
floating point value
   How many bytes?
   See Workspace Window

   Useful commands (see documentation for more details!)
   who (information about what is in memory)
   whos (more detailed information about what is in memory)
   clc (clears the command window)
   clear (clears all user defined variables)
   help name (provides information on command name)
M/O/F Basics - 2
   Script files (.m files)
   Text files containing M/O/F statements
   Type in a text editor (M/O/F) or use
M-file editor in Matlab (File/New/M-File or Ctrl-n)
 Matlab: %
 Octave: % or #

 FreeMat: %

   Example: look at cool_plot.m
   Add the directory containing cool_plot.m to the file path
Script File Example: cool_plot.m

0.5[ x2 0.5( x  y )2 ]
Plot of z( x, y)  e                               over  4  x, y  4
Octave Script File Example

cool_plot.m needs to be in the
Example (yours may be different):
• Matlab: File | Set Path…

Plot of z( x, y)  e0.5[ x       0.5( x  y )2 ]
over  4  x, y  4
2
Matlab Script File Example
Ch Example

function exp(-0.5(x*x+0.5(x-y)(x-y)))

1
1                                                                                    0.9
0.9                                                                                    0.8
0.8                                                                                    0.7
0.7                                                                                    0.6
0.6
0.5                                                                                    0.5
0.4                                                                                    0.4
z   0.3                                                                                    0.3
0.2                                                                                    0.2
0.1                                                                                    0.1
0                                                                                    0

4
3                                                                           4
2                                                                  3
1                                                         2
0                                                1
-1                                      0
y                -2                            -1           x
-3                 -2
-3This plot is generated by Ch Student Edition
-4 -4
See: chap. 23 of The Ch Language User's Guide
and chap. 2 of The Ch Language Environment Reference
Excel Example

Data needs to be equally spaced
• Select z-data values
• Insert | Other Charts | Surface
• Use Chart Tools | Layout tab to
control display
M/O/F Basics - 3
   Array creation                     Separate elements by spaces or
commas, and rows by semicolon
   A=[1 2 3; 4,5,6; 7 8 9]        or carriage return
   size(A)    What does this do?

   Indexing elements             Index by enclosing indices in ( )

   A(1,2)        Which element does this choose? Contrast with C.

   A(4,2)=13          What does this do? Contrast with C.

   A(4,2)=13;          The semicolon at the end suppresses
the output
M/O/F Basics - 4
   Vectors (just 1xn or nx1 arrays)
   B=[sin(pi/4), -2^3, size(A,1)]
   Vectors using the colon operator
1      0.25         2
   C=1 : 0.25 : 2        Format: base : increment : limit
For an increment of 1: base : limit
   D=0 : 5
   Linspace              Format: start : end : n For n elements
linearly spaced between start and
   E=linspace(0,5,3)         end

   Logspace             Format: start : end : n For n elements
logarithmically spaced between
   F=logspace(1,3,5)        start and end
M/O/F Basics - 5
   Manipulating arrays
   A = [ A ; [ 10 11 12 ] ]         Assuming A = [ 1 2 3; 4,5,6 ; 7 8 9 ]
   Extract a sub-array
   G = A(1:3, 1:2)         What does this do?
   Colon operator by itself means, “all the elements”
   Can apply to the dimensions of the arrays to access all the elements of
a row or column
   Can apply to the entire array to get all the elements as a column vector
 What order will they come out?
Matlab stores array
   Examples                                                  elements in column-
   H = [1:4; linspace(10,40,4); 400:-100:100]            major order.
   I = H( [1 2], : )  What will this produce?
   J = H( : )
M/O/F Basics - 6
   Special matrices
   zeros, ones, eye
   K=zeros(3)
   L=ones(4)
   M=eye(3)
M/O/F Basics - 7
   Matrix operations
   Arithmetic just like with scalars! (But need to
take care that dimensions agree)
A=[1 2 3; 4,5,6; 7 8 9]
   N=L*7                             B=[sin(pi/4), -2^3, size(A,1)]
E=linspace(0,5,3)
   O=N+eye(4)
L=ones(4)
   P=B*E
   P=B*E‟     What does the „ do?
   Q=A+B
   Q=B+E
   [1 2]*[3 ; 4]
M/O/F Basics - 8                                                                i2
i1
R2
   Matrix operations, cont.
   Matrix division                              +V

R3
R1
i3
   Recall last week‟s circuit analysis
   R1=10k
   R2=R3=5k
   V=10V                              1        1          1  i1   0 
   Matrix solution                          0         0         R1  i2   V 
                          
1          1            1
0
     R2  R3       0  i3  V 
   
Ri  V  R Ri  R V  i  R V
use ' left' division to solve for i                               R               i V
iR\V                                                       R = [1 -1 -1; 0 0 10e3; 0 10e3 0];
V = [0 10 10]‟;
I=R\V
( i = R / V ) roughly the same as: i = VR-1
M/O/F Basics - 9
   Matrix operations, cont.
   Element-by-element operations (use . (dot) and

the arithmetic operator)        v1            
     v1  v2  v1 v2 cos( )
   dot product of two vectors        v2
                 ˆ
ˆ  2 ˆ  5k                     ˆ
v1  3i      j                ˆ
v2  2i  4 ˆ  10k
j
 
what is v1  v2?
 
v1  v2  (3)(2)  (2)(4)  ( 5)(10)  6  8  50  52
in M/O/F :
v1  [3 2  5]           v2  [2  4 10]
sum(v1 . * v2)
M/O/F Basics - 10
   Functions
     Like script M-files, but several differences:
   first line must be of the form:            Name that you assign

function [output args] = function_name(input args)
keyword
   variables generated in the function are local to the function,
whereas for script files (.m files), variables are global
   must be named, „function_name.m‟
describe what the function does
Example: root-mean-square function, N 2
 xi

rms.m                               i 1
Given, x  [ x1 , x2 ,..., x N ] RMS 
N
M/O/F Basics - 10.1
   Functions, cont.                                                                 N
   Example: root-mean-
square function, cont.

i 1
xi2
Given, x  [ x1 , x2 ,..., x N ] RMS 
   Pseudocode:                                                                 N
   square each element of
x
   sum the squares
   Take the square root
   divide by N
   take the square root             rms = sqrt(ms)
   Square each element
   xs = x .^2                  Before you write the
   Sum the squares                    function, make sure the
   sums = sum(xs)               name you propose is
   Divide by N                        not already used!
   N = length(x)
   ms = sums/N                    help fn_name
M/O/F Basics - 10.2
   Functions, cont.
   Example: root-mean-square
function, cont.
function rmsout = rms(v)
H1 comment line
(used in lookfor)    % rms(v) root mean square of the elements of the column vector v
% Function rms(v) returns the root mean square of the elements
Comments that       % of the column vector, v. If v is a matrix, then rms(v) returns
will be displayed
% a row vector such that each element is the root mean square
by help command
%of the elements in the corresponding column of v.
vs = v.^2; % what does this line do? Also note semicolon.
s = size(v); % what does this line do?
rmsout=sqrt(sum(vs,1)/s(1)); % what is s(1)?

Let v=sin([0: 0.0001*pi: 2*pi]‟), one period of a sine wave. The RMS value
of a sine wave is its amplitude*1/sqrt(2)

Does rms() work with a row vector? How about a matrix?
M/O/F Basics - 10.3
   Functions, cont.
   Make rms function more
robust
   to work with row or column
vector or matrix with column
vectors of data
   Pseudocode:
   Test for size of v
   if > 2, print error message
   else
 if row vector
     transpose
 calculate rms
Vector Dot Product Example
Find the X and Y components of the vector, V
                              
that is, find v x and v y , so that v x  v y  v

Y                                                  v          
vy
v                              

ˆ
j                                                      vx

iˆ                   X

                             
    ˆ  v iˆ cos( )  v (1) cos( )  v cos( )
vx v i       x               x               x

                                                      
v y  v  ˆ  v y ˆ cos(90   )  v y (1) cos(90   )  v y sin( )
j       j

Back
Review
References
   Matlab. (2009, November 6). In Wikipedia, the free encyclopedia.
Retrieved November 6, 2009, from
http://en.wikipedia.org/wiki/Matlab
   Matlab tutorials:
   GNU Octave. (2009, October 31). In Wikipedia, the free
encyclopedia. Retrieved November 6, 2009, from
http://en.wikipedia.org/wiki/GNU_Octave
   Octave main page: http://www.gnu.org/software/octave/
   Octave tutorials: http://homepages.nyu.edu/~kpl2/dsts6/octaveTutorial.html,
http://smilodon.berkeley.edu/octavetut.pdf
   FreeMat. http://freemat.sourceforge.net/index.html
   ftp://www.chabotcollege.edu/faculty/bmayer/ChabotEngineeringCour
ses/ENGR-25.htm

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 4 posted: 10/10/2011 language: English pages: 29