0% found this document useful (0 votes)
12 views38 pages

MEEN210 L04 Arrays+Matrix

This document covers the fundamentals of vectors, arrays, and matrices in MATLAB, including how to create and manipulate them using commands like the colon operator and linspace. It explains matrix multiplication rules, provides examples of creating vectors and matrices, and discusses plotting functions using linspace. Additionally, it includes class problems and exercises to reinforce the concepts learned.

Uploaded by

uddinnasim09me
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views38 pages

MEEN210 L04 Arrays+Matrix

This document covers the fundamentals of vectors, arrays, and matrices in MATLAB, including how to create and manipulate them using commands like the colon operator and linspace. It explains matrix multiplication rules, provides examples of creating vectors and matrices, and discusses plotting functions using linspace. Additionally, it includes class problems and exercises to reinforce the concepts learned.

Uploaded by

uddinnasim09me
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

LECTURE 4: MEEN 210

Vectors and Arrays


Matrix multiplications
Colon operator and linspace command
Vectors, Arrays and Matrices
>> a = [2 3 4]
Brackets are used to assign vectors
>> b = [5 6 7]’
What does the [ ]’ do?

>> c= [ 8 9 10 11]

MATLAB Session 2.1 MATLAB Fundamentals Chapter 2


Vectors, Arrays and Matrices

a = [2 3 4] b= [5 c = [ 8 9 10 11]
6
7]
What are “a”, “b”, and “c” called?
Vectors (Arrays)

Vector “a” is a 1 x 3 (Row x Columns)


Vector “b” is a 3 x 1 (Row x Columns)
Vector “c” is a 1 x 4 (Row x Columns)

MATLAB Session 2.1 MATLAB Fundamentals Chapter 2


Vectors, Arrays and Matrices

Vectors and Matrices will be a large


part of your engineering life from now size (a) – 1 3
Number of rows
on. Number of columns

Lots of rules will be covered about using


vectors and matrices, but for right now;
Multiplication
d = a*b = = > [1x3] * [3x1] = ?
e = a*c = = > [1x3] * [1*4] = ?
MATLAB Session 2.1 MATLAB Fundamentals Chapter 2
Vectors, Arrays and Matrices

>> d=a*b = = > 2*5 + 3*6 + 4*7


d=
56
>> e = a*c
Error using *
Inner matrix dimensions must agree.
Why?
[1x3] * [1x4] = 3 & 1 Do Not Agree

MATLAB Session 2.1 MATLAB Fundamentals Chapter 2


Vectors, Arrays and Matrices

Multiplication (continued)
f1234 = b*a = = > [3x1] * [1x3] = ?
MATLAB

>> f1234=b*a
f1234 =
10 15 20
12 18 24
14 21 28
MATLAB Session 2.1 MATLAB Fundamentals Chapter 2
Vectors, Arrays and Matrices

Multiplication (continued)
f1234 is a,
Matrix [3x3]
This can be system of coefficients from a
equations – you will solve later this
semester.

MATLAB Session 2.1 MATLAB Fundamentals Chapter 2


Vectors, Arrays and Matrices

Multiplication (continued)
f12345 = 2 * f1234 ?
MATLAB
>> f1234=b*a
f12345 = 2 * f1234
20 30 40
24 36 48
28 42 56

MATLAB Session 2.1 MATLAB Fundamentals Chapter 2


Class Problem 2.1.1 Review of Vectors
>> w = [23 31 57]

>> hh = [(sin(.1)) (cos(.2)) (tan(.3))]

>> hhtrans=hh’
>> wtrans = w’

>> tt_tt = w * wtrans


>> ssSam = hhtrans * hh
MATLAB Session 2.1 MATLAB Fundamentals Chapter 2
Colon and linspace commands
COMMANDS TO CREATE A SERIES OF
NUMBERS – GREAT FOR MAKING PLOTS!

Like: X = [1,2,3,4,5,6,7,8,9 etc]


Syntax
>> Variable_name = (start:stepsize:end)
: used for creating arrays from start to <=end
>> Variable_name2 = linspace(start,stop,number of steps)
used for creating arrays of equally spaced real numbers
starting at start and ending at end
colon and “linspace” Commands
“colon” Command in MATLAB (page 33)
Example-1: Colon_Dan=1:2:5
MATLAB
>> Colon_Dan = 1:2:5
Colon_Dan =
1 3 5
Colon Command:
Start = 1 Increment = 2 (default =1)
Stop 5 (= < 5, not >)
MATLAB Session 2.1 MATLAB Fundamentals Chapter 2
Colon and “linspace” Commands
Colon Command in MATLAB
Example-2: Colon_Dan2=1:2:6
MATLAB
>> Colon_Dan2 = 1:2:6
Colon_Dan2 =
1 3 5

Stop Value = < not greater than!


What is “Colon_Dan2” = = > [1x3]
MATLAB Session 2.1 MATLAB Fundamentals Chapter 2
Vectors, Arrays and Matrices

“linspace” Command (page 33)


Example-3: lin_dan1=linspace(10, 20, 5)
MATLAB
>> lin_Dan1=linspace(10, 20, 5)
lin_Dan1 =
10.0000 12.5000 15.0000 17.5000 20.0000
linspace(10, 20, 5)
Start 10 End 20
Make Equal Spacing for 5 Intervals
MATLAB Session 2.1 MATLAB Fundamentals Chapter 2
Vectors, Arrays and Matrices

Example-4: lin_Dan2=linspace(10, 20,


13)
MATLAB
>> lin_Dan2=linspace(10, 20, 13)
lin_Dan2 =
Columns 1 through 7
10.0000 10.8333 11.6667 12.5000 13.3333
14.1667 15.0000
Columns 8 through 13
15.8333 16.6667 17.5000 18.3333 19.1667
20.0000

13 Equal Intervals between 10 and 20


MATLAB Session 2.1 MATLAB Fundamentals Chapter 2
Class Problem 2.1.2

Type in the following commands in the


MATLAB Command Window:
>> abcdefgh = 1:2.579:10000
What Happened?
Try,
>> abcdefgh = 1:-2.579:10000
What Happened?

MATLAB Session 2.1 MATLAB Fundamentals Chapter 2


Class Problem 2.1.3

Type in the following commands in the


size (matrix_name)
MATLAB Command Window: Number of rows
Number of columns
>> xxx = 1:1:100
>> Xxx_99 = xxx’
>> Yy_zz = linspace(1, 100, 100)
>> Mary_had_a_little_lamb = xxx * Xxx_99
>> ZZTop = Xxx_99 * Yy_zz

MATLAB Session 2.1 MATLAB Fundamentals Chapter 2


Class Problem 2.2.3

Type in the following commands in the MATLAB


Command Window:
>> x=linspace(0,0.13,1000);
>> y=linspace(0,9,1000);
>> plot(x, y, 'r--'), grid
>> hold on
>> yy=linspace(1,15, 1000);
>> plot(x, yy, ‘b-.’), grid
>> xx = linspace(0,1.9, 999);
>> plot(xx,yy, ‘g:’), grid

MATLAB Session 2.1 MATLAB Fundamentals Chapter 2


PLOTTING FUNCTIONS USING
LINSPACE
Plotting in MATLAB
2-D is all we will do in MEEN 210, but
MATLAB can do a lot more in plotting.
Class Problem 2.1.13
>> x = linspace(0, 20, 12);

>> y = x^3 – 2*x^2 + 7;


Error using ^
Incorrect dimensions for raising a matrix to a power.
Check that the matrix is square and the power
is a scalar. To perform elementwise matrix powers, use '.^’.
MATLAB Session 3.1 MATLAB Text Chapters 2 & 3
Plotting in MATLAB
MATLAB has to be told when you have a vector or
matrix and you are preforming:

* / ^ Operations on a Vector or Matrix by;


So,
x.*3 2*x.^2
>> x = linspace(0, 20, 12);
>> y = x.^3 – 2*x.^2 + 7;

Now, x (1, 12) and y (1,12)


MATLAB Session 3.1 MATLAB Text Chapters 2 & 3
Plotting in MATLAB

>> x = linspace(0, 20, 7);


>> y = x^3 – 2x^2 + 7;

We want to do “Element-by-Element”
operations in MATLAB
MATLAB wants to know when a variable is a
vector or matrix!

MATLAB Session 3.1 MATLAB Text Chapters 2 & 3


Plotting in MATLAB

>> x = linspace(0, 20, 12);


>> y = x.^3 – 2*x.^2 + 7;

>> plot(x, y, ‘b’), grid

MATLAB Session 3.1 MATLAB Text Chapters 2 & 3


 function y = yfunc(x)
 y = x.^3-2*x.^2+7;
 end

 ____________________________________________________
 >> y = yfunc(x); (to call the function)

TRY SETTING UP THE Y EQUATION AS A


FUNCTION SO WE CAN REUSE IT!
More Plotting
The graph is not smooth – not enough data points.

>> x1 = linspace(0, 20, 100);


Changed the Domain (x to x1), so
dependent variable (y to y1) has to be
calculated again!

>> y1 = x1.^3 – 2*x1.^2 + 7;


Or…
>> y1 = yfunc(x1);

>> plot(x, y, ‘b’, x1, y1, ‘r--’), grid


MATLAB Session 3.1 MATLAB Text Chapters 2 & 3
MATLAB Session 3.1 MATLAB Text Chapters 2 & 3
HW Problem 3.1.4

Write a plot command in MATLAB that will plot the


variables; x1, y1, y2 on the same plot. Make x1 vs.
y1 a green solid line and x1 vs. y2 a red dashed
line.

>> x1 = 0:0.1:pi;
>> y1 = cos(x1);
>> y2 = sin(x1);

MATLAB Session 3.1 MATLAB Text Chapters 2 & 3


SOLUTIONS TO IN-CLASS PROBLEMS
Class Problem 2.1.4

Type in the following commands in the MATLAB


Command Window (in Text Book, Problem 2.9, page 49):

>> A = [3 2 1; 0:0.5:1; linspace(6, 8, 3)]


>> AA = 3 * A
What does MATLAB Produce (in the Command
Window)?

MATLAB Session 2.1 MATLAB Fundamentals Chapter 2


Class Problem 2.1.5 (output from MATLAB - Blue)

>> w = [23 31 57]


w=
23 31 57

>> hh = [(sin(.1)) (cos(.2)) (tan(.3))]


hh =
0.0998 0.9801 0.3093 Radians? Degrees?

>> hhc=hh’
hhc =
0.0998
0.9801
0.3093
MATLAB Session 2.1 MATLAB Text Chapters 2 & 3
Class Problem 2.1.6 (output from MATLAB - Blue)

>> wc = w’
wc =
23
31
57
>> tt_tt = w * wc
tt_tt =
4739
>> ssSam = hhc * hh
ssSam =
0.0100 0.0978 0.0309
0.0978 0.9605 0.3032
0.0309 0.3032 0.0957
MATLAB Session 2.1 MATLAB Text Chapters 2 & 3
Class Problem 2.1.7
Type in the following commands, MATLAB Command Window:
>> xxx = 1:1:100
xxx =
Columns 1 through 16
1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 16
Columns 17 through 32
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
Columns 33 through 48
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
Columns 49 through 64
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
Columns 65 through 80
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
Columns 81 through 96
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
Columns 97 through 100
97 98 99 100

MATLAB Session 2.1 MATLAB Text Chapters 2 & 3


Class Problem 2.1.8
Type in the following commands, MATLAB Command Window:

>> Xxx_99 = xxx’ >> Xxx_99 = xxx’;


Sizes of; xxx (1,100) = = > Xxx_99 (100,1)

>> Yy_zz = linspace(1, 100, 100);


Used “;” so I compressed the output
Size of Yy_zz (1,100)

MATLAB Session 2.1 MATLAB Text Chapters 2 & 3


Class Problems 2.1.9
Type in the following commands, MATLAB Command Window:

>> Mary_had_a_little_lamb = xxx * Xxx_99


Mary_had_a_little_lamb =
338350

>> ZZTop = Xxx_99 * Yy_zz;

ZZTop (100, 100) Matrix

MATLAB Session 2.1 MATLAB Text Chapters 2 & 3


Class Problems 2.1.10
Type in the following commands in the MATLAB Command
Window:

>> x=linspace(0,0.13,1000);
>> y=linspace(0,9,1000);

>> plot(x, y, 'r--'), grid

MATLAB Session 2.1 MATLAB Text Chapters 2 & 3


MATLAB Session 2.1 MATLAB Text Chapters 2 & 3
Class Problems 2.1.11
Type in the following commands in the MATLAB Command
Window:

>> hold on

>> yy=linspace(1,15, 1000);

>> plot(x, yy, ‘b-.’), grid

MATLAB Session 2.1 MATLAB Text Chapters 2 & 3


MATLAB Session 2.1 MATLAB Text Chapters 2 & 3
Class Problems 2.1.12
Type in the following commands in the MATLAB Command
Window:

>> xx = linspace(0,1.9, 999);

>> plot(xx,yy, ‘g:’), grid

Error using plot


Vectors must be the same length.

MATLAB Session 2.1 MATLAB Text Chapters 2 & 3

You might also like