Matlab Basics: Tutorial - Mathematical Sciencesjarciero/Matlab_Basics.pdfMatlab Basics: Tutorial...

12
Matlab Basics: Tutorial Julia Arciero Math 426 - Spring 2012 1

Transcript of Matlab Basics: Tutorial - Mathematical Sciencesjarciero/Matlab_Basics.pdfMatlab Basics: Tutorial...

Page 1: Matlab Basics: Tutorial - Mathematical Sciencesjarciero/Matlab_Basics.pdfMatlab Basics: Tutorial Julia Arciero Math 426 - Spring 2012 1 . Introduction – Command Window The Command

Matlab Basics: Tutorial

Julia Arciero

Math 426 - Spring 2012

1

Page 2: Matlab Basics: Tutorial - Mathematical Sciencesjarciero/Matlab_Basics.pdfMatlab Basics: Tutorial Julia Arciero Math 426 - Spring 2012 1 . Introduction – Command Window The Command

Introduction – Command Window

The Command Window is the window with a prompt at which you enter your commands.

• Commands: enter commands directly at prompt

• Call an M file: just type name of file

• Save your command history by typing: save name_of_file.mat

2

Page 3: Matlab Basics: Tutorial - Mathematical Sciencesjarciero/Matlab_Basics.pdfMatlab Basics: Tutorial Julia Arciero Math 426 - Spring 2012 1 . Introduction – Command Window The Command

Vectors • All data should be entered as a vector or matrix • To generate a vector:

• Row vector: >> x = [1, 2, 3] OR >> x = [1 2 3] • Column vector: >> x = [1; 2; 3] • To transpose a vector, use apostrophe; >> x = [1 2 3]’

• Once vectors are assigned, can perform operations on them. Let x = [1 2

3] and y = [2 3 4]. • To sum the vectors, >> x + y • To multiply x by a constant: >> 3.*x • To get a product of the vectors element wise: >> x.*y • To divide terms in the vectors element wise: >> x./y • To get an inner product: >> x*y’

• To access an element of a vector:

• >> x(2) (would give out the number 2 for the second value of vector) • >> x(2) = 4 would replace the second value of the vector with a 4

3

Page 4: Matlab Basics: Tutorial - Mathematical Sciencesjarciero/Matlab_Basics.pdfMatlab Basics: Tutorial Julia Arciero Math 426 - Spring 2012 1 . Introduction – Command Window The Command

Special commands - vectors

• >> zeros(3,1) produces a 3x1 vertical vector with all 0 entries

• >>ones(4,4) produces a 4x4 matrix with all 1 as entries

• >>max(x) gives the max entry of the vector

• >>max(max(A)) gives the maximum entry of a matrix

• (similarly, the command min(x) finds the minimum entry)

• >>linspace(1,10) creates a vector of 100 values equally spaced between 1 (starting value) and 10 (ending value)

• >>linspace(0,100,1000) creates a vector of 1000 equally spaced values from 0 to 100

• X = 1:2:15 creates a vector from 1 to 15 and includes every second number (i.e., result is [1 3 5 7 9 11 13 15])

4

Page 5: Matlab Basics: Tutorial - Mathematical Sciencesjarciero/Matlab_Basics.pdfMatlab Basics: Tutorial Julia Arciero Math 426 - Spring 2012 1 . Introduction – Command Window The Command

Inputting a Matrix

• To generate a matrix , type the following:

• >> A = [1 2 3; 4 5 6]

• Could also define each row:

• A(1,:) = [1 2 3]

• A(2,:) = [4 5 6]

• Could also define each column:

• A(:,1) = [1;4]

• A(:,2) = [2; 5]

• A(:,3) = [3; 6]

• Notation: the first component u of A(u,v) gives the row and the second component v gives the column. A colon accounts for all elements in a row or column

6 5 4

3 2 1A

5

Page 6: Matlab Basics: Tutorial - Mathematical Sciencesjarciero/Matlab_Basics.pdfMatlab Basics: Tutorial Julia Arciero Math 426 - Spring 2012 1 . Introduction – Command Window The Command

Matrix operations

• Add or subtract matrices: >>A + B

• Multiply matrixes: >>A*B

• Elementwise multiplication: >>A.*B

• Division

• >>A/B means AB-1

• >>A./B means elementwise division

• Powers of matrices: >>A^3 means A*A*A

• >>inv(A) finds the inverse of a matrix, A-1

• Compute the solution of a linear sysetem Ax = b by the following: >> inv(A)*b

6

Page 7: Matlab Basics: Tutorial - Mathematical Sciencesjarciero/Matlab_Basics.pdfMatlab Basics: Tutorial Julia Arciero Math 426 - Spring 2012 1 . Introduction – Command Window The Command

Saving or loading data

• Save your data after you enter it all into your command lines

• >> save mydata.mat

• Load your previously saved data in the command window

• >> load mydata.mat

7

Page 8: Matlab Basics: Tutorial - Mathematical Sciencesjarciero/Matlab_Basics.pdfMatlab Basics: Tutorial Julia Arciero Math 426 - Spring 2012 1 . Introduction – Command Window The Command

Plotting • Let’s plot sin(x) and cos(x) on the same axis for x = -2*pi to 2*pi:

>> x = linspace(-2*pi, 2*pi); >> plot(x, cos(x),’b’,’Linewidth’,2) >> hold on >> plot(x, sin(x),’r’,’Linewidth’,2) >>xlabel(‘x’) >>ylabel(‘f(x)’) >>title(‘Sample plot of trig functions’)

• In the plot command, the first component is the variable on the x (horizontal) axis, the second component is the what is being plotted on the y (vertical) axis, and then you can specify colors and linewidths of the line, (b: blue, r: red, k: black, g: green)

• hold on command keeps the first curve on the plot while plotting the second on top of it. Else, matlab would replace the old figure with the new figure.

• xlabel, ylabel, and title are ways to label axes

• Can also plot a single point: • >>plot(4,5,’.’,’Markersize’,20) • Where the ‘.’ makes the points as circles, but you could also choose +, *, >, etc • Markersize defines how big of a point you want (20 is fairly large)

8

Page 9: Matlab Basics: Tutorial - Mathematical Sciencesjarciero/Matlab_Basics.pdfMatlab Basics: Tutorial Julia Arciero Math 426 - Spring 2012 1 . Introduction – Command Window The Command

Other built in functions • Exponential function is entered as exp(x) • Natural log function is entered as log(x) • Sine and cosine are entered as sin(x) and cos(x)

• The value of pi can be referred to as pi

• Note, always end your line with a semicolon (;) to suppress the output of that

line

• For, if/else, and while loops are built in

• The command “clear” clears all values from Matlab’s command line memory

• size(v) or length(v) gives number of rows and columns in a matrix of vector

• Type help followed by the command you want to learn about directly into the command line to get information on how to enter the function. E.g., >>help linspace

9

Page 10: Matlab Basics: Tutorial - Mathematical Sciencesjarciero/Matlab_Basics.pdfMatlab Basics: Tutorial Julia Arciero Math 426 - Spring 2012 1 . Introduction – Command Window The Command

Subplots

• Can plot multiple panels of plots:

• Let’s plot y = 2x in a left panel and y = exp(3*x) in the right panel

• >> x = linspace(-5,5);

• >> subplot(1,2,1),plot(x, 2.*x)

• >> hold on

• >> subplot(1,2,2),plot(x, exp(3.*x))

• In the subplot command, the first two numbers define the number of rows and columns in the subplot and the third number defines which panel you are currently plotting in. Order goes left to right and then to subsequent rows

10

Page 11: Matlab Basics: Tutorial - Mathematical Sciencesjarciero/Matlab_Basics.pdfMatlab Basics: Tutorial Julia Arciero Math 426 - Spring 2012 1 . Introduction – Command Window The Command

M files

• In matlab, go to “file” and “new” and choose “script” in order to create an m file

• Type all commands in the m file

• Save the m file as something (filename.m)

• In command window, type: >>filename

• Note, the file must be saved to the same directory that matlab is accessing, else it will not be able to recognize/load the file. Change directories in the command window (top center) so that this is not a problem

11

Page 12: Matlab Basics: Tutorial - Mathematical Sciencesjarciero/Matlab_Basics.pdfMatlab Basics: Tutorial Julia Arciero Math 426 - Spring 2012 1 . Introduction – Command Window The Command

Example of a for loop and if/else loop

n = 100; H = zeros(n,n); for j = 1:n for k = 1:n H(j,k) = 1/(j+k-1); end end for j = 1:n for k = 1:n if H(j,k) < 0.05 H(j,k) = 0.05; elseif H(j,k) > 0.1 H(j,k) = 0.1; else H(j,k) = 0.08; end end end

12