CS101 Lecture 1 - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

CS101 Lecture 1

Description:

linspace(a,b,N) creates a row vector of N equally spaced values starting at a ... The size of an vector is its number of rows and columns. ... – PowerPoint PPT presentation

Number of Views:137
Avg rating:3.0/5.0
Slides: 33
Provided by: tomga
Category:
Tags: cs101 | lecture | row

less

Transcript and Presenter's Notes

Title: CS101 Lecture 1


1
Lecture 1
2
What will I learn in this lecture?
What is the Matlab environment? How can you
create vectors ? What does the colon operator
do? How does the use of the built-in linspace
function differ from the use of the colon
operator? Readings Matlab by Pratap Chapter
3.
3
MATLAB - MATrix LABoratory
  • engineering visualization and computation
    software
  • 2-D contour, histogram, line, mesh, polar,
    polygon and scatter plots.
  • 3-D line, mesh, polygon, scatter and wire frame
    plots.
  • The interactive environment is user friendly.
  • MATLAB provides its own high-level language in
    which users can extend the capabilities of
    MATLAB.
  • C-like programming language
  • Many user defined functions

4
Interactive Environment
MATLAB is interactive, if you type (without )
gtgt 2 3 The system responds with ans 5 ans
is a built-in variable that contains the results
of the last evaluated expression that has not
been assigned to another variable. e.g. if we
type gtgt 3 3 ans 9
5
Output Suppression
To suppress the screen output use a
semicolon gtgt 5 2 ( is the power
operator, 5 raised to the second power
in this example) gtgt ( no response
from matlab) To examine the current value of ans
type gtgt ans ans 25
6
Variables
All variables are created as array variables. For
example, gtgt x 1.5 (this is called an
assignment statement) creates a (1 x 1) array
of type real and assigns the value 1.5 to x.
Variables only change values when you assign
values to them using an assignment statement. To
see the value of x type, gtgt x x
1.5000 Variable names must begin with a
character and are case sensitive.
RAM
7
Assignment statements
The variable to be assigned a value must be on
the left hand side of the assignment
statement gtgt 1.5 x (illegal assignment
statement) The above is illegal because 1.5
does not specify a legal address in memory. An
assignment statement is not a math equation.
For example, the following sequence is legal in
Matlab gtgt x 2.0 gtgt x x 1.0 x 3.0000
8
Workspace
The current environment for our computing session
is called the workspace. We can have many
variables and functions in our workspace. To see
what variables we have, typegtgt who ans
x For this session (up to now) the workspace
contains two variables ans and x. To see the
size (number of rows and columns) of these
variables, typegtgt whos Name Size Bytes Class
Both x and ans are 1 x 1 arrays, ans 1x1
8 double array we call these scalars. x 1x1
8 double array double refers to double
precision)
9
System Commands
(see p. 15 in Pratap for a list of Matlab system
commands) (extend a command that doesnt fit
on one line) gtgt save la3 (saves all the
workspace variables to the file la3.mat) gtgt load
la3 (loads the la3.mat file from the Unix
pwd) gtgt save (uses default file
matlab.mat) gtgt load (uses the default file
matlab.m at from the Unix pwd)
10
System Commands
gtgt clc clears the command window gtgt clf
clears the figure window gtgt clear var1 var2
clears only the variables var1 var2 gtgt clear
clears(deletes) all variables in the
workspace gtgt exit exit Matlabgtgt quit
same as exitgtgt what lists Matlab files
11
Output Format
format options (controls how numeric output is
displayed) Examples-- two of the options are
shown below gtgt x 12.34567890123456789 x
12.34567890123457 gtgt format short gtgt x x
12.3457 gtgt format long e gtgt x x
1.234567890123457e01
12
Arrays - Vectors and Matrices
In Matlab (for CS101) all data will be stored in
arrays. An array can represent a vector -
size 1 x n (row vector with 1 row and n columns)
or n x 1 (column vector with n rows and 1
column) matrix - any size m x n ( a table of m
rows and n columns). Conventions Two arrays
are defined to be equal if they are of the same
size and at each position, they have the same
value. The values in an array are called
components or elements.
13
Vectors
  • Some examples of vector quantities that occur
    frequently in
  • science and engineering problems are
  • velocity
  • acceleration
  • force
  • electric, magnetic, gravitational fields
  • surface normal

For example, the three-dimensional components of
a force vector can be represented in Cartesian
coordinates as
F (Fx, Fy, Fz)
The xy coordinates in the plane correspond to
a two-dimensional "vector"
P (x, y)
14
Row and Column Vectors
We can represent a vector either as a row vector
or as a column vector, depending on the
application. E.g.,
15
Creating a Row-Vector
To create vector arrays in MATLAB type the
following gtgt F1 1 2.5 3.5 gtgt F2
-1.75, 2.75, 1.0e-1 gtgt F3 1 2 gtgt whos
F1 F2 F3
(The use of the , operator is optional)
Name Size Bytes Class F1 1x3 24 double
array F2 1x3 24 double array F3 1x2
16 double array
F1 and F2 are 1 row x 3 column arrays and F3 is
a 1 row x 2 column array. To display their values
type the variable name gtgt F1 F1
1.0000 2.5000 3.5000
16
Creating a Column-Vector
To create column-vector arrays in MATLAB either
use the semi-colon operator or use the
transpose operator gtgt F1 1 2.5 3.5 (
means ... append to next row) gtgt F2 -1.75,
2.75, 1.0e-1 (transpose changes row vector
to column vector and vice-versa
)gtgt F3 (empty vector) gtgt whos F1 F2 F3
Name Size Bytes Class F1 3x1 24 double
array F2 3x1 24 double array F3 0x0
0 double array
gtgt F1 F1 1.0000 2.5000 3.5000
17
Creating a Vector Colon Operator
The (colon) operator is important in
construction of arrays gtgt x 1100
(creates a 1x100 row vector with values
1,2,3,4, 100) gtgt y (-1 0.1 1)
(creates a column vector with starting value -1
and increments by 0.1 until 1
is reached but not exceeded) gtgt z 0 0.3
1 z 0 0.3000 0.6000 0.9000 gtgt z
5 -1 2 z 5 4 3 2
18
Creating a Vector Subscripting
Use (parenthesis) to select a subset of the
elements of an array. gtgt x 10 9 8 7 gtgt y
3 4 5 gtgt z x(2) (note the use of
parenthesis and not the brackets) z
9 (subscripting x does not change x)gtgt z
x(13) (note the use the colon operator) z
10 9 8 gtgt x x(23) (now x has changed) x
9 8 gtgt z y(12) z 3 4
19
Creating a Vector Subscripting
Use the end function to obtain the last element
in a vector. Example gtgt x 10 9 8 7 gtgt y
3 4 5 gtgt z x(end) z 7 gtgt z
y(2end) z 4 5
20
Creating a Vector Subscripting
Use subscripting to change the value of a
vecor. Example gtgt x 10 9 8 7 gtgt y 3
4 5 gtgt x(2, 3) 5 x 10 5 5 7 gtgt
y(2end) 6 7 y(2end) 6 7 works
too y 3 6 7
21
Creating a Vector Function linspace
The built-in Matlab function linspace (see p.
57)works in a similar manner to the colon
operator . linspace(a,b,N) creates a row
vector of N equally spaced values starting at a
and ending at b. This is equivalent to a
(b-a)/(N-1) b Example Create a row vector
named vec1 of 4 elements starting at -1 and
ending at 1.
gtgt vec1 linspace(-1,1,4) vec1
-1.0000 -0.3333 0.3333 1.0000
22
Creating a Vector Function linspace
Example Create a row vector named vec2 of 4
elements each equal to 7.
gtgt vec2 linspace(7,7,4) vec2 7 7 7 7
Example Create a row vector named vec3 with the
values 5,4,3,2 .
gtgt vec3 linspace(5,2,4) vec3 5 4 3 2
23
Arrays - Arithmetic operators
- / \
. ./
.\ .
(dot form)
add sub mult right div
left div power
Rules for the valid operations(see p 57,58). If
A and B are vectors then it is not always true
that gtgtA op B is defined when op is one of the
operations in the table above. A and B are called
operands.The size of an vector is its number of
rows and columns. When multiple operators are
used in an expression , use the rules of
precedence. , / , \ , have special meaning
as we will consider...
24
Vectors - Addition
A and B must be of the same size or a scalar.
gtgt A 2 3 4 gtgt B 3 gtgt A
B ans 5 6 7
gtgt B 1 2 3 gtgt A B ans 3 5 7
25
Example- Vector Addition
F resultant of two forces applied to an object.
F1
F2
F F1 F2 (Fx1 Fx2, Fy1 Fy2)
In Matlab
gtgt F1 1 , 2.5 gtgt F2 -1.75, 1.0e-1 gtgt
F F1 F2 F -0.7500 2.6000
26
Vectors - Subtraction
A and B must be of the same size or a scalar.
gtgt A 2 3 4 gtgt B 3 gtgt A -
B ans -1 0 1
gtgt B 1 2 3 gtgt A - B ans 1 1 1
27
Vectors - Scalar Product
In Cartesian coordinates, we can express the dot
or scalar product of two vectors as a matrix
multiplication
F
Physical Example Work done by a force F acting
through a displacement s
s
surface
gtgt s 1, 1, 1 gtgt F1 1 2.5 3.5 gtgt
F1 s (dot or scalar product) ans 7
28
Vectors - Scalar Product
In order for the scalar product of vectors A B
to be defined, the number of columns in the row
vector A must equal the number of rows in the
column vector B.
gtgt A 2 3 4 gtgt B 1 2 3 gtgt A
B ans 20
29
Vectors - (dot) Multiplication
The size of A and B must be the same or either
could be a scalar .
gtgt A 2 3 4 gtgt B 1 2 3 gtgt A .
B ans 2 6 12
Note the result is the product of the
individual elements of the vectors.
For the other operators in the table on slide 1 -
20 , try to determine what the operator does and
the sizes for whichA op B is valid. We will
consider the \ operator in the future. (see
section 3.2.1 in book)
30
Matlab - Help facilities
To obtain help in using Matlab make sure the Help
window is open. To do this, click Help then
Matlab Help
31
Matlab Help
If you know the name of a Matlab function or
command, click Search and type in the name.
32
Matlab Help
If you want to obtain a list of functions by
category, first, click By Category,
Write a Comment
User Comments (0)
About PowerShow.com