Ch02 Matlab Arrays

advertisement
PowerPoint to accompany
Introduction to MATLAB 7
for Engineers
William J. Palm III
Chapter 2
Numeric Arrays
Copyright © 2005. The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Specification of a position vector using Cartesian
coordinates.
Figure 2.1–1
The vector p can be
specified by three
components: x, y, and
z, and can be written
as:
p = [x, y, z].
However, MATLAB can
use vectors having
more than three
elements.
2-2
This week

Creating vectors
row vs column vectors
 colon (:) operator
 linspace( )






vector math operations
finding roots of polynomials
simple 2D plotting
Matrices
creating and using script files
What is a vector?

A series of values stored in a single variable
x = [1, 2, 5, 2, 5];
 y = [1; 2; 5; 2; 5];

 row vector
 column vector

Only one dimension of data

Two dimensions = Matrix

The term Array refers to both vectors or
matrices
Vectors can be used to represent


A 3 dimensional force vector f = [3, 2, 9];
An evenly spaced series of values
t = [0:0.001:1];
 1000 samples spaced 1 millisecond apart


A math formula applied to a series of values


wave = cos(2*pi*t);
A polynomial

p = [1, 3, -2, 4]
representing x3 + 3x2 – 2x + 4
Creating Vectors

Typing values in square brackets
v = [1 , 2, 3];
 u = [1; 4; 9];


Using the colon operator


(1, 1.1, 1.2, ... 9.9, 10)
Using linspace


t = [1:0.1:10];
x = linspace(1,50, 200);
( 200 points from 1 to 50)
Using transpose ( ' ) to convert row to column

u = u';
 turns u from column to row
Vector Math operations

Addition, subtraction


 must have same dimensions
Multiplication, division by scalar


z = u + v;
p = 3*u;
q = v/5;
Element-by-element multiplication

r = u.*v;
 dot operator is a must, often forgotten
Polynomial Roots


Use vectors when you need to find roots of a
polynomial
A polynomial p = [1, 3, -2, 4]



representing f(x) = x3 + 3x2 – 2x + 4
The roots of a polynomial are the values of x
such that f(x) = 0
Very useful in many math problems
Plotting with Vectors


The idea is to create a vector of x-values
Use a math formula to create y-values



or just enter x and y values manually
Then plot(x,y);
use title('my plot title'); xlabel('text');
ylable('stuff'); to complete the plot
Matrices

A matrix can be entered directly using ; to start
new lines:


A matrix can be constructed from vectors:



M = [1, 2, 3; 4, 5, 6; 7, 8, 9];
M = [ u' , v' ];
Matrices can be added, scaled, or element-byelement operated on just as vectors can
The colon (:) operator can select parts of a
matrix
Script files





Matlab problem solutions should be developed
in a script file to allow easy execution and
debugging
Create an m-file in your local workspace
Put matlab statements in the file, save
At the matlab command line, type the filename
(without .m) and the statements in the file
execute.
Great for all kinds of problem solving
Download