MatlabGetStart Course
MatlabGetStart Course
MatlabGetStart Course
Topics:
- User Interface - Simple calculations - Matrices/Vectors - Functions - Loops/conditions - Plots
User Interface
(A semicolon at the end of the command suppresses the output. Interally, the variable ans is set to 25.)
Defining Variables:
>> a = 3; >> b = 5; >> c = a+b;
(c = 8)
- Variables are available during your entire session and appear in the workspace - The workspace can be cleared with the command >> clear - Matlab has some pre-defined constants, e.g. pi
5 digits (the default) 15 digits try to represent the answer as a rational number
Examples for pi :
short: long: rat:
Examples:
>> 4*x^5;
'( 4x 5 )
Simple alculations
Exercise
alculate the following e$pressions"
a b b a b ca
(for a=1, b=5, c=-2)
log e
2 cos
Simple alculations
Exercise
alculate the following e$pressions"
Solutions:
>> >> >> >> a = 1; b = 5; c = -2; b-a/(b+(b+a)/(c*a))
a b b a b ca
(for a=1, b=5, c=-2)
ans = 4.500
log e
2 cos
Entering Matrices:
Separate the elements of a row with blanks or commas. Use a semicolon to indicate the end of each row. Surround the entire list of elements with square brakets [ ]
Example:
>> A = [3 2 4; 1 2 0; -2 4 3] A = 3 1 -2 2 2 4 4 0 3
column-vector:
Scalar opreations are applied component-wise and a matrix of same dimension is returned.
Operations which are well-defined for matrices are NOT applied component-wise
Example: Matrix- Multiplication
>> B = [1 2 1; 1 2 1; 1 2 1] >> A.*B ans = 1 2 1 4 2 4 3 0 1 >> A = [1 2 3; 2 1 0; 1 2 1]; >> A*B ans = 6 3 4 12 6 8 6 3 4
component-wise:
Generating matrices: zeros(m,n) ones(m,n) eye(m) rand(m,n) magic(m) nxm-matrix with all zeros nxm-matrix with all ones mxm-identity matrix nxm-matrix with random values mxm-magic matrix
You can use the keyword end to access the last entry: >> A(end,2); last entry of the second column-vector
#$ercise
Prerequisite: Define the Matrix
. * !( . 4
. 0 2 /
/ 1 * .
* 2 3 *
Tasks: - Extract the upper left 3x3- submatrix of A - Compute the dot product of the main diagonal of A and the fourth column vector of A. - Define a Matrix B which arises from interchanging the 1st and the 4th row of A - Set the entire 3rd column of A to '1' - Insert the row-vector (1 2 3 4) at the bottom of A:
#$ercise
Prerequisite: Define the Matrix
. * !( . 4
. 0 2 /
/ 1 * .
* 2 3 *
Tasks: - Extract the upper left 3x3- submatrix of A - Compute the dot product of the main diagonal of A and the fourth column vector of A. - Define a Matrix B which arises from interchanging the 1st and the 4th row of A - Set the entire 3rd column of A to '1' - Insert the row-vector (1 2 3 4) at the bottom of A:
Solutions:
>> A(1:3,1:3) >> diag(A)'*A(:,4)
>> A(:,3) = 1;
All .m-files in the current directory are available in your command window
Example:
Function which returns the sum of the top-left and the bottom-right element of a matrix: nonsense.m
Note: Neither the type, nor the dimensions of the parameters has to be specified
function [ AB BA ] = MatrixMult( A,B ) AB = A*B; BA = B*A; end >> A = [1 2 3; 2 3 4; 1 0 2]; >> B = [0 1 2; 2 4 1; 5 3 1]; >> [M1,M2] = MatrixMult(A,B); 9 M1= 6 18 12 14 25 5 7 12 8 M2 = 13 14 11 20 23 4 4 7
Solution:
function [ r ] = ld( x ) r = log(x)/log(2); end
Loops / Loops
onditions
repeats the commands for each element of a vector N. The currently processed vector element is available inside the loop via the running parameter (here: i)
A vector with incremental elements can easily be created with the colon (:) -operator >> t = 1:10 t = 1 2 3 4
default-increment is 1
6 7
10
2.4
2.6
2.8
3.0
Loops /
onditions
Conditions
if (expression) ... commands ... end
If the expression is true, the commands are executed, otherwise the program continues with the next command immediately beyond the end statement
You can create alternative command sections with else and elseif: if (expression1) ... elseif (expression2) ... elseif (expression3) ... else ... end
as soon as one of the expressions is true, the respective commands are executed and the program continues beyond the end statement. The command after an else-statement are executed when none of the conditions are true.
Loops /
onditions
Conditions
Examples for expressions:
(a < b) (a == b) (a ~= b)
Compound statements:
Loops / Exercise:
onditions
Write a function fact_or_sum(n) which computes i if i =1 n is even and n! if n is odd (for n>0)
n
Loops / Exercise:
onditions
Write a function fact_or_sum(n) which computes n is even and n! if n is odd (for n>0)
i
i =1
if
Loops /
onditions
Conditional Loops
while (condition) ... commands ... end &he loop is repeated as long as the condition is satisfied
Plots
The plot function has different forms, depending on the input arguments. - If y is a vector, plot(y) produces a piecewise linear graph of the elements of y versus the index of the elements
>> y = [3 1 2 5 2 4]; >> plot(y)
Note: Graphing functions automatically open a new figure window if there are no figure windows already on the screen. If a figure window exists, it is used for graphics output.
Plots
- If you specify two vectors as arguments, plot(x,y) produces a graph of y versus x.
>> x = [0:2:10]; >> y = [1 2 4 4.5 3 0]; >> plot(x,y)
$ 7
2 .
* /
0 0
6 083
4 /
.2 2
Plots
Specifying line styles, colors and markers
plot(x,y,'color_style_marker') color_style_marker is a string containing from one to four characters (enclosed in single quotation marks) constructed from a color, a line style, and a marker type. The strings are composed of combinations of the following elements: olor" 'c' 'm' 'y' 'r' 'g' 'b' 'w' 'k'
cyan magenta yellow red green blue white black
888
Plots
#$amples"
>> plot(x,y,'ks') plots blac9 s:uares at each data point; but does not connect the mar9ers with a line8 >> plot(x,y,'r:+') plots a red-dotted line and places plus signs at each data point
Plots
Plotting multiple data sets in one graph
Multiple $-7 pairs as arguments create multiple graphs with a single call to plot >> plot(x,y,x,y2,x,y3)
Plots
#$ercise
reate a plot of sin'$) with .22 data points in the inter>al ?2;*
Plots
#$ercise
reate a plot of sin'$) with .22 data points in the inter>al ?2;*
Solution:
>> s = 2*pi/99; >> x = 0:s:2*pi; >> y = sin(x); >> plot(x,y,'r:+')