0% found this document useful (0 votes)
142 views

Lecture 13 Matlab Octave FreeMat pt1

This document provides an overview of using Matlab, Octave, and FreeMat (M/O/F) for engineering applications. It discusses M/O/F environments, basic operations like arrays and matrices, using script files, and key functions. Examples demonstrate how to perform operations like matrix multiplication and division, element-by-element operations, and writing custom functions. The goal is to explain how to use M/O/F as a calculator, create scripts, and develop more advanced functions and analyses for engineering problems.

Uploaded by

navinars
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
142 views

Lecture 13 Matlab Octave FreeMat pt1

This document provides an overview of using Matlab, Octave, and FreeMat (M/O/F) for engineering applications. It discusses M/O/F environments, basic operations like arrays and matrices, using script files, and key functions. Examples demonstrate how to perform operations like matrix multiplication and division, element-by-element operations, and writing custom functions. The goal is to explain how to use M/O/F as a calculator, create scripts, and develop more advanced functions and analyses for engineering problems.

Uploaded by

navinars
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

Lecture 13: M/O/F/ for

Engineering Applications -
Part 1
BJ Furman
26NOV2012
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
Resources for more information

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)
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)
Key point!
Matlab (ver. 6.5) Environment (GUI)
Default Window Layout
Command
Window
Command History
Window
Workspace/Current
Directory Window
Interactive scratch pad
Workspace: lists variables
Directory: files in current dir
Octave Command Line
Enter commands at the prompt
QtOctave GUI
Enter commands at the prompt
Matlab as a scratch pad
Variables are
dynamically
typed
Info on variables in
the workspace
Octave as a scratch pad
dynamically
typed
M/O/F Basics - 1
Fundamental element: the array
even scalars are 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. In QtOctave, View Clear Terminal)
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)
Comments
Matlab: %
Octave: % or #
FreeMat: %
Example: look at cool_plot.m
Verify directory containing cool_plot.m is in the file path
MATLAB: File | Set Path | select folder
Octave: addpath(dir_path) find from Windows Explorer
Script File Example: cool_plot.m
4 , 4 over ) , ( of Plot
] ) ( 5 . 0 [ 5 . 0
2 2
s s =
+
y x e y x z
y x x
Octave Script File Example
4 , 4 over ) , ( of Plot
] ) ( 5 . 0 [ 5 . 0
2 2
s s =
+
y x e y x z
y x x
cool_plot.m needs to be in the
load path
Octave: addpath(dir-path)
Example (yours may be different):
addpath('C:\Octave\Octave_code')
Matlab: File | Set Path
Matlab Script File Example
Ch Example
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
-4
-3
-2
-1
0
1
2
3
4
-4
-3
-2
-1
0
1
2
3
4
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
z
function exp(-0.5(x*x+0.5(x-y)(x-y)))
x
This plot is generated by Ch Student Edition
y
z
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
A = [1 2 3; 4,5,6; 7 8 9]
size(A)
Indexing elements
A(1,2)
Adding elements
A(4,2) = 13
A(4,2) = 13;
Separate elements by spaces or
commas, and rows by semicolon
or carriage return
Which element does this choose? Contrast with C.
Index by enclosing indices in ( )
What does this do?
What does this do? Contrast with C.
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
C = 1 : 0.25 : 2
D = 0 : 5
Linspace
E = linspace(0,5,3)
Logspace
F = logspace(1,3,5)

base : increment : limit
start : end : n For n elements
linearly spaced between start and
end
start : end : n For n elements
logarithmically spaced between
start and end
1 0.25 2
For an increment of 1: base : limit
Format:
Format:
Format:
Note: limit is reached only
if (limit-base)/increment is
an integer
M/O/F Basics - 5
Manipulating arrays
Add a row
A = [ A ; [ 10 11 12 ] ]
Extract a sub-array
G = A(1:3, 1:2)
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?
Examples
H = [1:4; linspace(10,40,4); 400:-100:100]
I = H( [1 2], : )
J = H( : )
What will this produce?
Assuming A = [ 1 2 3; 4,5,6 ; 7 8 9 ]
What does this do?
Matlab stores array
elements in column-
major order.
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)
N=L*7
O=N+eye(4)
P=B*E
P=B*E
Q=A+B
Q=B+E
[1 2]*[3 ; 4]
What does the do?
A=[1 2 3; 4,5,6; 7 8 9]
B=[sin(pi/4), -2^3, size(A,1)]
E=linspace(0,5,3)
L=ones(4)
M/O/F Basics - 8
Matrix operations, cont.
Matrix division
Recall the circuit analysis problem
R1=10k
R2=R3=5k
V=10V
Matrix solution
V \ R i
i for solve to division left' ' use
1 1 1
=
= = =

V R i V R Ri R V Ri
( ) (
(
(

=
(
(
(

(
(
(

+

V
V
i
i
i
R R
R
0
0 0
0 0
1 1 1
3
2
1
3 2
1
R i V
If we had iR = V instead, wed use right division:
( i = R / V ) roughly the same as: i = VR
-1
R2
R
1

R
3

+V
i1
i2
i3
R = [1 -1 -1; 0 0 10e3; 0 10e3 0];
V = [0 10 10];
I = R \ V
M/O/F Basics - 9
Matrix operations, cont.
Element-by-element operations use . (dot) and the operator (other
than addition/subtraction)
dot product of two vectors
52 50 8 6 ) 10 )( 5 ( ) 4 )( 2 ( ) 2 )( 3 (
? is what

10

3
2 1
2 1
2 1
= = + + = -
-
+ = + =
v v
v v
k j i v k j i v



) 2 * . 1 (
] 10 4 2 [ 2 ] 5 2 3 [ 1
: M/O/F in
v v sum
v v = =
1
v

2
v

u
) cos(
2 1 2 1
u v v v v

= -
Note!! period-asterisk means
element-by-element multiplication
N
x
RMS x x x x
N
i
i
N

=
= =
1
2
2 1
] ,..., , [ Given,
M/O/F Basics - 10
Functions
Like script M-files, but several differences:
first line must be of the form:
function [output args] = function_name(input args)
variables generated in the function are local to the function,
whereas for script files (.m files), variables are global
the file must be named, function_name.m
Make sure you add comments at the start that
describe what the function does
Example: root-mean-square function,
rms1.m
keyword
Name that you assign
M/O/F Basics - 10.1
Functions, cont.
Example: root-mean-square
function, cont.
Pseudocode:
square each element of x
sum the squares
divide by N
take the square root
Expression to square each
element in vector x
xs = x .^2
Sum the squares
sums = sum(xs)
Divide by N
N = length(x)
ms = sums/N
N
x
RMS x x x x
N
i
i
N

=
= =
1
2
2 1
] ,..., , [ Given,
Take the square root
rms = sqrt(ms)

Before you write the
function, make sure the
name you propose is
not already used!
help fn_name
M/O/F Basics - 10.2
Functions, cont.
Example: root-mean-square
function, cont.
function [y] = rms(v)
% rms(v) root mean square of the elements of the column vector v
% Function rms(v) returns the root mean square of the elements
% of the column vector, v. If v is a matrix, then rms(v) returns
% a row vector such that each element is the root mean square
%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?
y = 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?
H1 comment line
(used in lookfor)
Comments that
will be displayed
by help command
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
See rms3.m
Vector Dot Product Example
v

u
X
Y
j

Find the X and Y components of the vector, V


v

x
v

y
v

) sin( ) 90 cos( ) 1 ( ) 90 cos(



u u u
y y y y
v v j v j v v

= = = - =
v v v v v
y x y x

= + that so , and find is, that
u
) cos(

u i v
x

= ) cos( ) 1 ( u
x
v

= ) cos(u
x
v

=
i v v
x

- =

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:
http://www.mathworks.com/academia/student_center/tutorials/launchpad.html
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/
(http://octave.sourceforge.net/ access to pre-built installers)
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

You might also like