1 introduction to matlab matlab is all of the following: 1.computational environment 2.plotting...

Post on 25-Dec-2015

235 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

Introduction to MATLAB

MATLAB is all of the following:

1. Computational environment2. Plotting software3. Programming language

Typical applications:

1. Calculations2. Engineering/Scientific programs3. Audio processing4. Image processing

2

% Scalars

x = 1.23;y = pi;disp(x)format longdisp(y)format shortdisp(y)

1.2300

3.141592653589793

3.1416

3

% Scalar arithmetic

x = 3;y = 2;disp(x+y) % addition: +disp(x-y) % subtraction: -disp(x*y) % multiplication: *disp(x/y) % division: /disp(x^y) % power: ^

5

1

6

1.5000

9

4

Exercise

1. Calculate 4 times 5.

2. Raise 2 to the power 10.

Hints: * (multiplication) ^ (raising to a power)

5

% vectors

x = [1,2,3]; % row vectorx = [1 2 3]; % commas can be replaced by spacesdisp(x)disp(length(x))disp(size(x))y = [1;2;3]; % column vectordisp(y)disp(size(y))

1 2 3

3

1 3

1 2 3

3 1

6

% Transpose

x = [1 2 3];y = x'; % transpose of real vector xdisp(y)z = y';disp(z)

1 2 3

1 2 3

7

Exercise

1. Create a row vector consisting of the whole numbers (integers) from 0 to 5. Display it.

2. Have MATLAB identify the size of this “matrix.”

3. Convert your row vector into a column vector. Display it.

4. Have MATLAB identify the size of this new “matrix.”

Hints: size() ' (transpose)

8

% Concatenation of row vectors

x = [1 2 3];y = [4 5 6];z = [x y]; % concatenate row vectorsdisp(z)z = cat(2,x,y); % concatenate by adding columns % this accomplishes the same thing as abovedisp(z)

1 2 3 4 5 6

1 2 3 4 5 6

9

% Concatenation of column vectors

x = [1; 2];y = [3; 4];z = [x; y]; % concatenate column vectorsdisp(z)z = cat(1,x,y); % concatenate by adding rows % this accomplishes the same thing as abovedisp(z)

1 2 3 4

1 2 3 4

10

Exercise

1. Create a row vector consisting of the whole numbers (integers) from 1 to 5 and a second row vector consisting of the whole numbers 6 to 8.

2. Concatenate these two row vectors into one long row vector using two different methods: square brackets the function cat

3. Create the transpose of each of the two component row vectors that you created in step 1.

4. Concatenate these two column vectors into one long column vector using two different methods: square brackets the function cat

11

sum Sum of array elements

Syntax B = sum(A) B = sum(A,dim)

My comments:

A is a matrix, and dim is either 1 or 2.If A is a vector, then a scalar (the sum of all elements) results.If A is a matrix (with at least 2 rows and at least 2 columns), then either a row (dim = 1) or a column (dim = 2) vector results.

MATLAB Documentation for sum

12

% sum function with vector argument

x = [1 2 3 4]; % row vectordisp(x)disp(sum(x))y = [1; 2; 3]; % column vectordisp(y)disp(sum(y))

1 2 3 4

10

1 2 3

6

13

% The functions maximum and minimum

x = [1 2 3];% The functions maximum and minimum work equally well for% row and column vectors.disp(max(x))disp(max(x'))disp(min(x))disp(min(x'))

3

3

1

1

14

Exercise

Create a column vector consisting of the whole numbers 0 to 10. Have MATLAB find the length of this vector, the sum of its elements, the maximum and minimum elements.

Hints: length() sum() max() min()

15

% Create vectors with the functions zeros and ones

x = zeros(1,5);disp(x)y = zeros(2,1);disp(y)y = ones(1,4);disp(y)

0 0 0 0 0

0 0

1 1 1 1

16

% Create row vectors with the colon operator

a = 0:2:10; % start at 0, increment by 2, end at 10disp(a)b = 0:5; % by default, increment by 1disp(b)c = 0:-1:-5;disp(c)

0 2 4 6 8 10

0 1 2 3 4 5

0 -1 -2 -3 -4 -5

17

% Create row vectors with linspace and logspace

x = linspace(0,1,5); % 5 values: 0 through 1disp(x)y = logspace(0,4,5); % 5 values: 10^0 through 10^4disp(y)

0 0.2500 0.5000 0.7500 1.0000

1 10 100 1000 10000

18

Exercise

1. Create a column vector of length 6, with each element a 0.

2. Create a row vector consisting of the odd integers 1 through 11, using the colon operator.

3. Create a row vector of 21 equally-spaced values between 0 and 10.

4. Create a row vector of 7 logarithmically-spaced values between 1 and 1,000,000.

Hints: zeros() linspace() logspace()

19

% Vector input to built-in mathematical function

x = linspace(0,pi,5);disp(x)y = sin(x); % because x is a vector, sin produces a vectordisp(y)

0 0.7854 1.5708 2.3562 3.1416

0 0.7071 1.0000 0.7071 0.0000

% Basic plot

x = linspace(0,4,41);y = sqrt(x);figure(1)plot(x,y,'-b')axis([0 4 0 2])saveas(1,'basic','png')

% Bigger font size and thicker lines

x = linspace(0,4,41);y = sqrt(x);figure(2);plot(x,y,'-b')axis([0 4 0 2])set(gca,'FontSize',24)set(findobj(2,'LineWidth',0.5),'LineWidth',2) % thick linessaveas(2,'better','png')

22

Exercise

Create a plot of the squaring function ().

Experiment with different ranges for the axes. For example, you could start with:

axis([0 2 0 4])

For this exercise, you needn’t worry about appearance. In other words, you don’t have to set font size or line thickness.

Hint. Recall that we plotted square-root like this:

x = linspace(0,4,41);y = sqrt(x);figure(2);plot(x,y,'-b')axis([0 4 0 2])

23

r red

g green

b blue

c cyan

m magenta

y yellow

k black

w ‘white’

Color Specifiers

24

‘-’ solid line

‘--’ dashed line

‘:’ dotted line

‘-.’ dash-dot line

Line Style Specifiers

25

Exercise

Create a plot of the common logarithm (base 10 logarithm), . In MATLAB the common logarithm function is log10.

Experiment with using different line style specifiers and different colors.

Hint. Recall that we plotted square-root like this:

x = linspace(0,4,41);y = sqrt(x);figure(2);plot(x,y,'-b')axis([0 4 0 2])

% Two curves

x = linspace(0,4,41);y = sqrt(x);z = x./2;figure(5);plot(x,y,'-k',x,z,'--b') % 2 curves on same axesaxis([0 4 0 2])set(gca,'FontSize',24)set(findobj(5,'LineWidth',0.5),'LineWidth',2)saveas(5,'curves','png')

27

Using an Index to Address Elements of an Array

In the C/C++ programming language, an index starts at 0 and elements of an array are addressed with square brackets [∙]:

8 2 -3 7 -1

x[0] x[1] x[2] x[3] x[4]

↑ ↑ ↑ ↑ ↑

In MATLAB, an index starts at 1 and elements of an array are addressed with parentheses (∙):

8 2 -3 7 -1

x(1) x(2) x(3) x(4) x(5)↑ ↑ ↑ ↑ ↑

28

Square Brackets in MATLAB

In MATLAB, square brackets [∙] are used for building arrays, such as vectors, matrices, and three-dimensional arrays.

For example,

x = [1 2 3];y = [4 5 6];z = [x y]; % concatenate row vectorsdisp(z)

1 2 3 4 5 6

29

% Indices (spelling lesson: 1 index, 2 or more indices)

x = zeros(1,5);x(1) = 8; % change element with index 1 (first element)disp(x)x(2:5) = 7; % change elements having indices 2 through 5disp(x)y = 1:4;x(2:5) = y;disp(x)

8 0 0 0 0

8 7 7 7 7

8 1 2 3 4

30

Exercise

1. Create a row vector of length 8, with each element a 0.

2. Create a row vector consisting of the whole numbers 1 to 4.

3. In the row vector of step 1, replace the last 4 elements with the vector of step 2.

top related