Annotation - From Loren on the Art of Matlab
Plotting in Matlab
Matlab Commands List
Complex Data (off topic!)
Notes on Matlab Basic and Complex Numbers (pdf file)
Plot help from Matlab (online version)
Wednesday, October 10, 2007
Tuesday, October 9, 2007
Basic Matrix, collected
17.1 Basic Matrix Functions, from GNU Octave Manual by John W. Eaton
Octave Examples for Solving Linear Algebra Equations, from Hanson's website.
Note: very useful for the Intro to Numerical Analysis course.
Bad Octave/Matlab 1-norm! The 1-norm is not the regular one!
Monday, October 8, 2007
Basic Plotting
Plotting set of points (vector)
angles = [o:pi/3:2*pi]
y = sin(angles)
plot(angles, y)
Plot smoother curves (vector)
angles = linspace(0, 2*pi, 100) % 100 points equally spaced btw 0 and 2pi
y = sin(angles)
plot(angles, y)
Basic options
'.' = Set dots plot style
'@' = Set points plot style
'-@' = Set linespoints plot style
'^' = Set impulses plot style
'L' = Set steps plot style
'N' = Interpreted as the plot color if N is an integer in the range 1 to 6
'NM' = if N an integer in the range 1 to 6 -> color
if M an integer in the range 1 to 6 -> point style
This is only valid in combination with the '@' or '-@' specifier
'C' = plot color
'";title;"' = here '"title"' is the label for the key
1 = red / *
2 = greed / +
3 = blue / o
4 = magenta / x
5 = cyan / house
6 = brown / there exists
Example:
plot(x, y, "@12", x, y2, x, y3, "4", x, y4, "+"), This command plot
'y' with color 1 (red) and points of type 2 (+),
'y2' with lines
'y3' with lines of color 4 (magenta)
'y4' with points displayed as '+'
plot(b, '*'), plot the data in the var. b w. points displayed as '*'.
t = 0:0.1:6.3;
plot(t, cos(t), "-;cos(t);", t, sin(t), "+3; sin(t);")
Title and Labels
title('Graph of y = sin(x)')
xlabel('Angle')
ylabel('Value')
Colors and Styles for symbols and lines in the plot command (see help plot)
(M = only available in Matlab)
w = white
m = magneta
c = cyan
r = red
g = green
b = blue
y = yellow
k = black
. = point
o = circle
x = x-mark
+ = plus
* = star
s = square (M)
d = diamond (M)
v = triangle (down) (M)
^ = triangle (up) (M)
< = triangle (left) (M)
> = triangle (right) (M)
p = pentagram (M)
h = hexagram (M)
- = solid
: = dotted (M)
-. = dashdot (M)
-- = dashed (M)
Extra
grid on % turn on grid
grid off
hold on % keep current plot to add more
hold off
figure (N) % plot graph in window N. If no number is specified, the next number is called. If command not called, graph will be plotted in exactly the same window.
More info:
1) plot is the user-defined function from the file /usr/share/octave/2.9.9/m/plot/plot.m
2) Additional help for built-in functions and operators is
available in the on-line version of the manual. Use the command
`doc' to search the manual index.
3) See also: semilogx, semilogy, loglog, polar, mesh, contour,
__pltopt__ bar, stairs, errorbar, replot, xlabel, ylabel, title,
print.
angles = [o:pi/3:2*pi]
y = sin(angles)
plot(angles, y)
Plot smoother curves (vector)
angles = linspace(0, 2*pi, 100) % 100 points equally spaced btw 0 and 2pi
y = sin(angles)
plot(angles, y)
Basic options
'.' = Set dots plot style
'@' = Set points plot style
'-@' = Set linespoints plot style
'^' = Set impulses plot style
'L' = Set steps plot style
'N' = Interpreted as the plot color if N is an integer in the range 1 to 6
'NM' = if N an integer in the range 1 to 6 -> color
if M an integer in the range 1 to 6 -> point style
This is only valid in combination with the '@' or '-@' specifier
'C' = plot color
'";title;"' = here '"title"' is the label for the key
1 = red / *
2 = greed / +
3 = blue / o
4 = magenta / x
5 = cyan / house
6 = brown / there exists
Example:
plot(x, y, "@12", x, y2, x, y3, "4", x, y4, "+"), This command plot
'y' with color 1 (red) and points of type 2 (+),
'y2' with lines
'y3' with lines of color 4 (magenta)
'y4' with points displayed as '+'
plot(b, '*'), plot the data in the var. b w. points displayed as '*'.
t = 0:0.1:6.3;
plot(t, cos(t), "-;cos(t);", t, sin(t), "+3; sin(t);")
Title and Labels
title('Graph of y = sin(x)')
xlabel('Angle')
ylabel('Value')
Colors and Styles for symbols and lines in the plot command (see help plot)
(M = only available in Matlab)
w = white
m = magneta
c = cyan
r = red
g = green
b = blue
y = yellow
k = black
. = point
o = circle
x = x-mark
+ = plus
* = star
s = square (M)
d = diamond (M)
v = triangle (down) (M)
^ = triangle (up) (M)
< = triangle (left) (M)
> = triangle (right) (M)
p = pentagram (M)
h = hexagram (M)
- = solid
: = dotted (M)
-. = dashdot (M)
-- = dashed (M)
Extra
grid on % turn on grid
grid off
hold on % keep current plot to add more
hold off
figure (N) % plot graph in window N. If no number is specified, the next number is called. If command not called, graph will be plotted in exactly the same window.
More info:
1) plot is the user-defined function from the file /usr/share/octave/2.9.9/m/plot/plot.m
2) Additional help for built-in functions and operators is
available in the on-line version of the manual. Use the command
`doc
3) See also: semilogx, semilogy, loglog, polar, mesh, contour,
__pltopt__ bar, stairs, errorbar, replot, xlabel, ylabel, title,
print.
Sunday, October 7, 2007
Basic Functions
cos : cosin of an angle (in radians)
sin : sin of an angle (in radians)
tan : tan of an angle (in radians)
exp : exponential function
log : natural logarithm
log10 : logarithm to base 10
sinh : hyperbolic sine
cosh : hyperbolic cosine
tanh : hyperbolic tanh
acos : inverse cosine
asin : inverse sine
atan : inverse tan
atan2 : two-argument form of inverse tan
atanh : inverse tanh
abs
sign
round : round to the nearest integer
floor : round down (towards minus infinity)
ceil : round up (towards plus infnity)
fix : round towards zero
rem : remainder after integer division
Function Programming
BASIC
1. Function is defined in a text file, just like a script, exept that the first line has the following form:
function [output1, output2, ... ] = name ( input1, input2, ... )
2. Each function is stored in a different M-file, which MUST HAVE THE SAME NAME AS THE FUNCTION.
i.e., a function called sind() must be defined in a file called sind.m
EXAMPLE 1. Sine in degrees
function s = sind(x)
%SIND(X) Calculates sine(x) in degrees
s = sin(x*pi/180);
Line 1: Tells OCTAVE that this file defines a function.
- The function is called sind.
- It takes one argument, called x.
- The result is to be known, internally, as s.
- Whatever s is set to in this function is what the user will get when they use the sind function
Line 2: Is a comment line.
- The first set of comments in the file should discribe the function.
- This line is the one printed when the user types 'help sind'.
- It is usual to use a similar format to that which is used by Octave's built-in functions.
Line 3: Does the actual work in this function. ...
End of the function
- Functions in Octave do not need to end with 'return'
- (although you can use the 'return' command to make Octave jump out of a function in the middile)
- B/c each function is in a separate M-file, once it reached the end of the file, Octave knows that it is the end of the function.
- The value that s has at the end of this function is the value that is returned.
EXTRA
- The function AUTOMATICALLY works with vectors:
If you call the sind function with a vector, it means that the x parameter inside the function will be a vector.
In this case, the sin function knows how to work with vectors, so can give the correct response.
Example 2: Unit step
Here is a more sophisticated function which generates a unit step, defined as:
y = 0 if t < t0, 1 otherwise.
function y = ustep(t, t0)
%USTEP(t, t0) unit step at t0
% A unit step is defined as
% 0 for t <>
% 1 for t >= t0
[m,n] = size(t);
% Check that this is a vector, not a matrix i.e. (1 x n) or (m x 1)
if m ~= 1 & n ~= 1
error('T must be a vector');
end
y = zeros(m, n); %Initialise output array
for k = 1:length(t)
if t(k) >= t0
y(k) = 1; % Otherwise, leave it at zero, which is correct
end
end
Line 1: The first line says that
- this function is called ustep
- the user must supply two arguments (known internally as t and t0)
- the result of the function is one variable, called y
Line 2-5: Description of the function.
- This time the help message contains several lines.
Line 6-12:
- The error function print out a message and aborts the function if there is a problem.
- The length function tells us how many elements there are in the vector t
Extra:
We can use this function to create signal.
For example, to create a unit pulse of duration one second, starting at t = 0:
t = -1:0.1:4; % Define a time scale
v = ustep(t,0) - ustep(t,1)
plot(t,v)
axis([-1 4 -1 2])
This should display the pulse.. If we then type
who
We'll get:
*** dynamically linked functions:
dispatch
*** currently compiled function:
_ptl2_ _plt_ isscalar isvector rows
_ptl2vv_ axis isstr plot ustep
*** local user variables:
t v
1. Function is defined in a text file, just like a script, exept that the first line has the following form:
function [output1, output2, ... ] = name ( input1, input2, ... )
2. Each function is stored in a different M-file, which MUST HAVE THE SAME NAME AS THE FUNCTION.
i.e., a function called sind() must be defined in a file called sind.m
EXAMPLE 1. Sine in degrees
function s = sind(x)
%SIND(X) Calculates sine(x) in degrees
s = sin(x*pi/180);
Line 1: Tells OCTAVE that this file defines a function.
- The function is called sind.
- It takes one argument, called x.
- The result is to be known, internally, as s.
- Whatever s is set to in this function is what the user will get when they use the sind function
Line 2: Is a comment line.
- The first set of comments in the file should discribe the function.
- This line is the one printed when the user types 'help sind'.
- It is usual to use a similar format to that which is used by Octave's built-in functions.
Line 3: Does the actual work in this function. ...
End of the function
- Functions in Octave do not need to end with 'return'
- (although you can use the 'return' command to make Octave jump out of a function in the middile)
- B/c each function is in a separate M-file, once it reached the end of the file, Octave knows that it is the end of the function.
- The value that s has at the end of this function is the value that is returned.
EXTRA
- The function AUTOMATICALLY works with vectors:
If you call the sind function with a vector, it means that the x parameter inside the function will be a vector.
In this case, the sin function knows how to work with vectors, so can give the correct response.
Example 2: Unit step
Here is a more sophisticated function which generates a unit step, defined as:
y = 0 if t < t0, 1 otherwise.
function y = ustep(t, t0)
%USTEP(t, t0) unit step at t0
% A unit step is defined as
% 0 for t <>
% 1 for t >= t0
[m,n] = size(t);
% Check that this is a vector, not a matrix i.e. (1 x n) or (m x 1)
if m ~= 1 & n ~= 1
error('T must be a vector');
end
y = zeros(m, n); %Initialise output array
for k = 1:length(t)
if t(k) >= t0
y(k) = 1; % Otherwise, leave it at zero, which is correct
end
end
Line 1: The first line says that
- this function is called ustep
- the user must supply two arguments (known internally as t and t0)
- the result of the function is one variable, called y
Line 2-5: Description of the function.
- This time the help message contains several lines.
Line 6-12:
- The error function print out a message and aborts the function if there is a problem.
- The length function tells us how many elements there are in the vector t
Extra:
We can use this function to create signal.
For example, to create a unit pulse of duration one second, starting at t = 0:
t = -1:0.1:4; % Define a time scale
v = ustep(t,0) - ustep(t,1)
plot(t,v)
axis([-1 4 -1 2])
This should display the pulse.. If we then type
who
We'll get:
*** dynamically linked functions:
dispatch
*** currently compiled function:
_ptl2_ _plt_ isscalar isvector rows
_ptl2vv_ axis isstr plot ustep
*** local user variables:
t v
Basic Symbols
Boolean expression
Symbol meaning example
== equal if x==y
~= not equal if x~=y
> greater than if x>y
>=
<
<=
& AND if x==y & y>2
| OR if x==1 | y>2
~ NOT if x = ~y
Symbol meaning example
== equal if x==y
~= not equal if x~=y
> greater than if x>y
>=
<
<=
& AND if x==y & y>2
| OR if x==1 | y>2
~ NOT if x = ~y
Vectors
Creating Vectors
a=[1 4 5] % row vector
a=[1;4;5] % column vector
d=[a 6] % add 6 at the end of vector a
a= 1:6 % row vector, value from 1 to 6, increment 1
b= 1:.3:6 % row vector, value from 1 to 6, increment .3
zeros(m,n) % create m x n matrix, all elements are 0
ones(m,n) % create m x n matrix, all elements are 1
linspace(x1,x2,N) % create a vector of N elements,
% evenly spaced btw x1 and x2
logspace(x1,x2,N) % create a vector of N elements,
% logarithmically spaced btw x1 and x2
a=[1:2:6 -1 0] gives: 1 3 5 -1 0
Extracting elements
a(3) % third element, result: 5
a(3:5) % elements from 3 to 5, result: 5 -1 0
a(1:2:5) % elements from 1 to 5 w. increment 2, result: 1 5 0
Vector maths
b=[1,2,3,4,5]
a*2 % regular scalar multiplication
a.*b % element-by-element multiplication
a./b % element-by-element division
% '.' means element-by-element
b.^2 % square each element of b, result: [1,4,9,16,25]
2.^b % raise 2 to each of the powers given in b, result: result: [2,4,8,16,32]
a=[1 4 5] % row vector
a=[1;4;5] % column vector
d=[a 6] % add 6 at the end of vector a
a= 1:6 % row vector, value from 1 to 6, increment 1
b= 1:.3:6 % row vector, value from 1 to 6, increment .3
zeros(m,n) % create m x n matrix, all elements are 0
ones(m,n) % create m x n matrix, all elements are 1
linspace(x1,x2,N) % create a vector of N elements,
% evenly spaced btw x1 and x2
logspace(x1,x2,N) % create a vector of N elements,
% logarithmically spaced btw x1 and x2
a=[1:2:6 -1 0] gives: 1 3 5 -1 0
Extracting elements
a(3) % third element, result: 5
a(3:5) % elements from 3 to 5, result: 5 -1 0
a(1:2:5) % elements from 1 to 5 w. increment 2, result: 1 5 0
Vector maths
b=[1,2,3,4,5]
a*2 % regular scalar multiplication
a.*b % element-by-element multiplication
a./b % element-by-element division
% '.' means element-by-element
b.^2 % square each element of b, result: [1,4,9,16,25]
2.^b % raise 2 to each of the powers given in b, result: result: [2,4,8,16,32]
Subscribe to:
Posts (Atom)