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

Basic Matlab Operations

Uploaded by

s. magx
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Basic Matlab Operations

Uploaded by

s. magx
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Interactive Computing with Basic Matlab Operations

Matlab
Starting Matlab
Using Matlab as a calculator
Introduction to variables and functions

1 2

Starting Matlab
Matlab Workspace (version 6)
•Double click o the Matlab icon, or on UNIX systems type
matlab at the command line.
•After startup Matlab displays a command window that is
used to enter commands and display text-only results.
•Enter Commands at the command prompt:
>>for full version
EDU>for educational version
•Matlab responds to commands by printing text in the
command window, or by opening a figure window for
graphical output.
•Toggle between windows by clicking on them with the
mouse.

3 4

1
Matlab as a Calculator Built-in Variables
Enter formulas at the command prompt pi (=Л) and ans are a built-in variables
>>2 +6 -4 (press return after “4”)
ans=
4 >>pi
>>ans/2 ans=
ans= 3.1416
2

Or, define and use variables >>sin(ans/4)


>>a =5 ans=
a=
5 0.7071
>>b = 6 Note: There is no “degrees” mode. All angles are
b= measured in radians.
6
>>c =b/a
c=
1.2000

5 6

Looking for Functions


Built-in Functions Syntax
Many standard mathematical functions, such as
sin, cos, log, and log10, are built-in lookfor string
Searches first line of function descriptions
>>log(256) for “string”.
ans= Example:
5.5452 >>lookfor cosine
Produces
>>log10(256) ACOS Inverse cosine.
ans= ACOSH Inverse hyperbolic cosine.
2.4082 COS Cosine.
>>log2(256) COSH Hyperbolic cosine.
ans=
8

7 8

2
On-line Help
Ways to Get Help Syntax:
•Use on-line help to request info on a specific
function help functionName
>>help sqrt
Example:
•The helpwin function opens a separate >>help log
window for the help browser Produces
>>helpwin(“sqrt “) LOG Natural logarithm.
LOG(X) is the natural logarithm of the
•Use lookfor to find functions by keywords elements of X.
>>lookfor functionName Complex results are produced if X is not
positive.
See also LOG2, LOG10, EXP, LOGM.

9 10

Mathematical Operation Mathematical Operation


The common operators, in order of priority; >> y = pi/2

^ Exponentiation >> y ^ 2.45


- Negation
* and / Multiplication and Division >> y = -4 ^ 2
\ Left division y = -16
+ and - Addition and subtraction >> y = (-4)^ 2
y = 16

11 12

3
Suppress Output with Semicolon Multiple Statements per Line
Results of intermediate steps can be suppressed with semicolons.

Example: Use commas or semicolons to enter more than one


Assign values to x, y ,a d z ,but only display the value of z in the
command window:
statement at once. Commas allow multiple
>>x =5; statements per line without suppressing output.
>>y =sqrt(59);
>>z =log(y) + x^0.25
z=
>>a =5; b =sin(a), c =cosh(a)
3.5341 b=
-0.9589
Type variable name and omit the semicolon to print the value of a variable
(that is already defined)
c=
>>y 74.2099
y=
7.6811 (=log(sqrt(59))+5 ^0.25 )

13 14

Matlab Variables Names Built-in Matlab Variables


Legal variable names:
Name Meaning
Begin with one of a .z or A .Z
ans value of a expression when that expression is not
Have remaining characters chosen from a -z, A .Z,0 .9,or
assigned to a variable
Have a maximum length of 31 characters
eps Floating point precision
Should not be the name of a built-in variable, built-in function, or
pi л. (3 141492 …)
user-defined function
realmax largest positive floating point number
realmin smallest positive floating point number
Examples:
Inf ∞, a number larger than realmax,
xxxxxxxxx
the result of evaluating 1/0.
pipeRadius
NaN not a number, the result of evaluating 0/0.
widgets_per_baubble
mySum
Rule: Only use built-in variables on the right hand side of an expression.
mysum
Reassigning the value of a built-in variable can create problems with built-
in functions.
Note: mySum and mysum are different variables. Matlab is case
Exception: i and j are preassigned to √-1. One or both of i or j are often
sensitive.
preassigned as loop indices.

15 16

4
Matrices and Vectors
All Matlab variables are matrices
A Matlab vector is a matrix with one row or one
column
Matrices and Vectors column
A Matlab scalar is a matrix with one row and one

Overview of Working with matrices and vectors


•Creating vectors:
Creating matrices and vectors linspace and logspace
•Creating matrices:
Subscript notation ones ,zeros ,eye ,diag ,...
Colon notation •Subscript notation
•Colon notation
•Vectorization

17 18

Creating Matlab Variables Manual Entry


Matlab variables are created with a assignment For manual entry, the elements in a vector are enclosed in square brackets.
statement When creating a row vector, separate elements with a space.
>>v =[7 3 9 ]
>>x =expression v=
where expression is a legal combinations of 7 3 9
numerical values, mathematical operators, Separate columns with a semicolon
>>w =[2;6;1 ]
variables, and function calls that evaluates to a w=
matrix, vector or scalar. 2
The expression can involve: 6
1
•Manual entry In a matrix, row elements are separated by spaces, and semicolons
•Built-in functions that return matrices separate columns
•Custom (user-written) functions that return >>A = [1 2 3; 5 7 11; 13 17 19]
A=
matrices 1 2 3
•Loading matrices from text .les or “mat” files 5 7 11
13 17 19

19 20

5
Transpose Operator Matrix Operation
Once it is created, a variable can be transformed with other operators. The transpose
operator converts a row vector to a column vector (and vice versa), and it changes the Product of a matrix and a constant
rows of a matrix to columns. >>F= 3*A
>>v = [2 4 1 7 ]
v= Matrix multiplication
2 4 1 7 >>A = [1 2 3; 4 5 6];
>>v ’ = >>B = [2 4 6 8; 1 2 3 4; 1 3 5 7];
ans= >>C = A*B
2 C=
4
1 >>D = B*A
7 %???
>>A = [1 2 3; 4 5 6; 7 8 9] Matrix division
A= >>C = A/B
1 2 3
4 5 6 Inverse of a Matrix
7 8 9 >>C = A*Inv(B)
>>A’ = Determinant of a Matrix
ans = >>C = det(B)
1 4 7
2 5 8
3 6 9

21 22

Matrix Operation Overwriting Variables


Left Division
Given: -x1+x2+2x3 =2 Once a variable has been created, it can be
3x1-x2+x3 =6
-x1+3x2+4x3 =4 reassigned
A= [-1 1 2;3 -1 1;-1 3 4] >>x = 2;
B= [2; 6; 4] >>x = x +2
>>X=inv(A)*B x=
X=
4
1.000
-1.000 >>y = [1 2 3 4]
2.000 y=
>>X= A\B 1 2 3 4
X= >>y = y’
1.000 y=
-1.000 1
2.000 2
3
Array raising to a power
4
>>B = A.^2

23 24

6
Creating vectors with linspace Example: A Table of Trig Functions
The linspace function creates vectors with elements having uniform >>x =linspace(0,2*pi,6)’; (note transpose)
linear spacing. >>y =sin(x);
Syntax: >>z =cos(x);
x = linspace(startValue,endValue) >>[x y z ]
x = linspace(startValue,endValue,nelements) ans=
Examples: 0 0 1.0000
>>u = linspace(0.0,0.25,5) 1.2566 0.9511 0.3090
u= 2.5133 0.5878 -0.8090
0 0.0625 0.1250 0.1875 0.2500 3.7699 -0.5878 -0.8090
>>u =linspace(0.0,0.25); 5.0265 -0.9511 0.3090
>>v =linspace(0,9,4)’ 6.2832 0 1.0000
v=
0 The expressions y =sin(x) and z =cos(x) take advantage of vectorization. If
3 the input to a vectorized function is a vector or matrix, the output is often a
6 vector or matrix having the same shape.
9
Note: Column vectors are created by appending the transpose operator
to linspace

25 26

Creating vectors with logspace Functions to Create Matrices


The logspace function creates vectors with elements having uniform
logarithmic spacing. Name Operation(s) Performed
diag create a matrix with a specified diagonal entries, or extract
Syntax: diagonal entries of a matrix
x =logspace(startValue,endValue) eye create an identity matrix
x=logspace(startValue,endValue,nelements ) ones create a matrix filled with ones
rand create a matrix filled with random umbers
creates nelements elements between 10 startValue and 10endValue . zeros create a matrix filled with zeros
The default value of nelements is 100. linspace create a row vector of linearly spaced elements
logspace create a row vector of logarithmically spaced elements
Example:
>>w = logspace(1, 4, 4)
w= Use ones and zeros to set initial values of a matrix or vector.
10 100 1000 10000

27 28

7
Syntax: Syntax:
A = ones(nrows ,ncols) A = eye(n )
A = zeros(nrows ,ncols) A = eye(nrows ,ncols )
Examples:
Examples:
>>D = ones(3,3)
D=
>>C = eye(5)
1 1 1 C=
1 1 1 1 0 0 0 0
1 1 1 0 1 0 0 0
>>E = ones(2,4) 0 0 1 0 0
E= 0 0 0 1 0
0 0 0 0 0 0 0 0 1
0 0 0 0 >>D = eye(3,5)
ones and zeros are also used to create vectors. To do so, set either nrows or ncols to 1.
>>s=ones(1,4)
D=
s= 1 0 0 0 0
1 1 1 1 0 1 0 0 0
>>t =zeros(3,1) 0 0 1 0 0
t=
0 The diag function can either create a matrix with specified diagonal elements,
0 or extract the diagonal elements from a matrix
0
The eye function creates identity matrices of a specified size. It can also create non-
square matrices with ones o the main diagonal.

29 30

Syntax:
A = diag(v )
v = diag(A)
Subscript Notation
Use diag to create a matrix
>>v = [1 2 3];
>>A = diag(v) If A is a matrix, A(i,j) selects the element in the ith row and jth column.
A= Subscript notation can be used on the right hand side of an expression to refer to
1 0 0 a matrix element.
0 2 0
0 0 3 >>A = [1 2 3; 4 5 6; 7 8 9];
Example:
>>b = A(3,2)
Use diag to extract the diagonal of a matrix
>>B = [1:4; 5:8; 9:12] b=
B= 8
1 2 3 4 >>c = A(1,1)
5 6 7 8 c=
9 10 11 12 1
>>w = diag(B)
w=
1
6
11
Note: The action of the diag function depends on the characteristics and number of the
input(s). This polymorphic behavior of Matlab functions is common. The on-line documentation
(help diag ) explains the possible variations.

31 32

8
Subscript Notation Subscript Notation
Subscript notation is also used to assign matrix elements Assigning an element outside of current matrix dimensions
causes the matrix to be resized!
>>A(1,1)=c/b
A= >>A = [1 2 3; 4 5 6; 7 8 9];
0.2500 2.0000 3.0000 A=
4.0000 5.0000 6.0000 1 2 3
7.0000 8.0000 9.0000 4 5 6
7 8 9
Referring to elements outside of current matrix dimensions results in
an error >>A(4,4) = 11
A=
>>A = [1 2 3;4 5 6;7 8 9]; 1 2 3 0
>>A(1,4) 4 5 6 0
7 8 9 0
???Index exceeds matrix dimensions. 0 0 0 11
Matlab automatically resizes matrices on the fly.

33 34

Colon Notation
Colon Notation
Creating row vectors:
Colon notation is very powerful and very important in the effective use of >>s = 1:4
Matlab .The colon is used as both an operator and as a wildcard. s=
1 2 3 4
Use colon notation to: >>t = 0:0.1:0.4
• create vectors t=
• refer to or extract ranges of matrix elements 0 0.1000 0.2000 0.3000 0.4000
Creating column vectors:
Syntax: >>u =(1:5)’
startValue : endValue u=
startValue : increment : endValue 1
2
3
Note: startValue, increment, and endValue do not need to be integers 4
5
>>v =1:5 ’
v=
1 2 3 4 5
v is a row vector because 1:5 ’creates a vector between 1 and the transpose of 5.

35 36

9
Colon Notation

Use colon as a wildcard to refer to an entire column or row


Example:
>>A = [1 2 3; 4 5 6; 7 8 9];
>>A(:,1) >>A = ones(8,8);
ans= >>A(3:6,3:6) = zeros(4,4)
1
4 A=
7 1 1 1 1 1 1 1 1
>>A(2,:) 1 1 1 1 1 1 1 1
ans= 1 1 0 0 0 0 1 1
4 5 6 1 1 0 0 0 0 1 1
Or use colon notation to refer to subsets of columns or rows 1 1 0 0 0 0 1 1
>>A(2:3,1) 1 1 0 0 0 0 1 1
ans= 1 1 1 1 1 1 1 1
4 1 1 1 1 1 1 1 1
7
>>A(1:2,2:3) Finally, colon notation is used to convert any vector or matrix to a column vector.
ans=
2 3
5 6
Colon notation is often used in compact expressions to obtain results that would
otherwise require several steps.

37 38

Examples:
Exercise: Formula Evaluation
>>x =1:4;
>>y =x(:)
y=
1
2 V = [√ (g m / cd)] [ tanh √( g cd t / m) ]
3
4 Where: v = velocity, m/s
>>A =rand(2,3); g = acceleration due to gravity (9.81 m/s2)
>>v =A(:) m = mass, kg
v= cd = drag coefficient, kg/m
0.9501 t = time, s
0.2311
0.6068 I. Create a column vector, t, that contains values from 0 to 20 in
0.4860 steps of 2
0.8913 II. Create the number of items in the t array with the length function
0.7621 Note: Syntax: length(variable)
0.4565 III. Assign values to the parameters of g equal 9.81, m equal 68.1
Note: The rand function generates random elements between zero and one. and cd equal 0.25.
Repeating the preceding statements will, in all likelihood, produce different IV. Enter the formula of v
numerical values for the elements of v.

39 40

10
Exercise:
Define the Matrices
Exercise:
A= B=
1 2 3 4 1 1 1 1 Create this Matrix from the given Matrices:
6 8 1 5 1 1 1 1
9 4 2 7 1 1 1 1 T=
9 4 7 2 1 1 1 1 1 2 3 4 2 2 2 2 4 3 3 3
C= D= 6 8 1 5 2 2 2 2 3 4 3 3
16 2 3 13 1 0 0 0 9 4 2 7 2 2 2 2 3 3 3 4
5 11 10 8 0 3 0 0 9 4 7 2 2 2 2 2 4 3 3 3
9 7 6 12 0 0 5 0 1 4 7 10 16 2 3 13 1 0 0 0
4 14 15 1 0 0 0 7 16 22 1 13 5 11 10 8 0 3 0 0
E= F= 25 10 4 19 9 7 6 12 0 0 5 0
1 0 0 0 3 6 9 12 25 10 19 4 4 14 15 1 0 0 0 7
0 1 0 0 18 24 3 15
0 0 0 1 27 12 6 21
1 0 0 0 27 12 21 6

Solve:
3A + 4F 2DEF+4B
ABC/4F FEB/2CB
(2B – 3D) * (2ABC)

41 42

11

You might also like