Download presentation

Presentation is loading. Please wait.

Published byEaster Ross Modified over 4 years ago

1
259 Lecture 15 Introduction to MATLAB

2
2 What is MATLAB? MATLAB, which stands for “MATrix LABoratory” is a high- performance language for technical computing. It integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation. Typical uses include: Math and computation Algorithm development Data acquisition Modeling, simulation, and prototyping Data analysis, exploration, and visualization Scientific and engineering graphics Application development, including graphical user interface building

3
3 What is MATLAB? MATLAB is an interactive system whose basic data element is an array that does not require dimensioning. In many university environments, it is the standard instructional tool for introductory and advanced courses in mathematics, engineering, and science. In industry, MATLAB is the tool of choice for high-productivity research, development, and analysis.

4
4 What is MATLAB? MATLAB features a family of add-on application-specific solutions called toolboxes, which allow you to learn and apply specialized technology. Toolboxes are comprehensive collections of MATLAB functions (M-files) that extend the MATLAB environment to solve particular classes of problems. Areas in which toolboxes are available include signal processing, control systems, neural networks, fuzzy logic, wavelets, simulation, and many others.

5
5 The MATLAB System The MATLAB system consists of these main parts: Desktop Tools and Development Environment The MATLAB Mathematical Function Library The MATLAB Language (syntax) Graphics The MATLAB External Interfaces/Application Programming Interface (used to design software)

6
Octave – A Free MATLAB Clone One of the “drawbacks” to MATLAB is that it is very expensive (~$1000 for an academic license). One possible alternative to MATLAB is OCTAVE, which is a free clone of MATLAB (i.e. Octave mimics the functionality of MATLAB, often in the exact same way). For more information on Octave (technically GNU Octave), see: Octave downloads from Source Forge (both Windows and Mac OS X installers can be found here: http://octave.sourceforge.net/ http://octave.sourceforge.net/ Octave Homepage: http://www.gnu.org/software/octave/ http://www.gnu.org/software/octave/ 6

7
7 MATLAB Desktop Current Directory Enter Commands here in the Command Window Current Directory Window Command History Window

8
Octave Standard Interface 8

9
Octave GUI (not as stable) 9

10
10 Calling MATLAB Help To call Help in MATLAB, either click on the MATLAB Help link, or type “helpdesk” in the Command Window. Octave does NOT have a built-in help file, so “helpdesk” doesn’t apply! Notice that the Command History window keeps track of all of the commands you type!

11
11 Ending a MATLAB Session To end a MATLAB session, type “exit” or “quit”.

12
12 MATLAB Basics If you forget how to use a command, but can remember the command name, such as “plot”, type “help plot”. A description of the command, along with syntax, options, related commands, and examples will appear!

13
13 MATLAB Basics In MATLAB, another way to get information about a command is via “doc”, which gives more detailed information than “help”. Try “doc plot”. This command does not work as well in Octave!

14
14 MATLAB Basics Many MATLAB functions are created from M-files, which are programs that contain a set of commands. These commands are run in order to implement the function. To see the contents of an m-file, use the “type” command. This command does not work as well in Octave! Try “type linspace”.

15
15 MATLAB Basics To re-enter a command, use the up or down arrow, or type the first few letters of the command, and hit return! To clear out your Command Window, use “clc”. Commands in the Command History can be re-entered by double-clicking on them! The Octave GUI must be on for this to work!

16
16 Entering Matrices Matrices are the basic “building blocks” of MATLAB - essentially everything MATLAB does is based on calculations involving a matrix. The most basic way to enter a matrix is by typing in the entries, inside square brackets, with row entries separated by spaces and new rows designated by semicolons. Try entering these!

17
17 The Colon Operator Another way to enter a matrix is via the colon, which is used to define increments within a vector. Try each of the following: x = [1:5] y = [1:0.1:5] z = [0:pi/4:2*pi]

18
18 The Colon Operator We can “pull out” parts of a matrix with the colon! Try each of the following: A(2,:) is row 2 of matrix A. A(:,3) is column 3 of matrix A. B(1:2:3,:) is rows 1 and 3 of matrix B.

19
19 The Colon Operator A(2,:) = [] removes the second row from A. To insert the row back in, we can concatenate matrices via A = [A(1,:); 0 -1 6; A(2,:)]. Try w = [x x]. For more information on the colon operator, type “help colon”.

20
20 Matrix Operations To re-display matrix A, type “A”. Matrix Operations: Addition: + Subtraction: - Matrix Multiplication: * Matrix Power: ^ Transpose – i.e. switch columns and rows (conjugate transpose in the complex case): ’ To transpose a matrix with complex entries, use.’ Inverse of a square matrix A: inv(A). Matrix Left Division: \ (usage A\B = inv(A)*B) Matrix Right Division: / (usage A/B = A*inv(B)) Example 1: Try each of the following! AA BB A+A A-A A+B A*B B*A inv(A) A*inv(A) A/A A\A A\B inv(A)*B A^3 A*A*A A’ A(1,1) = 2+i A’ A.’

21
21 Matrix Operations - Example 1 (cont.)

22
22 Array Operations Scalar Multiplication: k*A with k a scalar. “Normal” arithmetic works as expected with the usual order of operations – each number is a 1 x 1 matrix. Array Multiplication, i.e. entry-wise multiplication of matrices of the same size: A.*B Array Division, i.e. entry-wise division of matrices of the same size: A./B Array Exponentiation, i.e. entry-wise exponentiation of matrices of the same size: A.^B Example 2: Try each of the following! A(1,1)=1 C = 5*A C(:,1) = [1 1 1] A.*C A./C A.*B A.^C A.*inv(A) A.^3 1+2/3*4^7

23
23 Array Operations – Example 2 (cont.)

24
24 Special Matrices and Matrix Functions “eye(n)” gives an n x n identity matrix “zeros(m,n)” gives an m x n matrix of 0’s. “ones(m,n)” gives an m x n matrix of 1’s. “det(A)” computes the determinant of an n x n matrix A. (Recall that for A to be invertible, det(A) 0 must hold!) To clear matrix A out of the Workspace, use “clear A”. To clear ALL variables out of the Workspace, use “clear all”.

25
25 Linear Systems of Equations MATLAB can be used to solve systems of linear equations. Example 3: Solve the system x + 2y = 1 3x + 4y = -1 Solution: This system can be written in matrix form AX=b with:

26
26 Linear Systems of Equations Example 3 (cont.) Check if det(A) 0. If so, then X = inv(A)*b will follow!

27
27 Making a Table of Values for a Function MATLAB can be used to evaluate functions at input values entered as a vector! For multiplication, division, and powers, use the array versions of these operators. Example 4: Try the following commands to make a table of values of the function y = (x*sin(x 2 )-4)/(x+1). x = [1:0.5:5]; % The ; suppresses output to the screen! y = (x.*sin(x.^2)-4)./(x+1); % Array operators used here. [x; y] % Concatenate the two vectors. [x; y]’ % Transpose the matrix with x in column 1 and y in column 2 to get our table!

28
Creating an M-file One of the most powerful features of MATLAB is the M- file, which is a file containing a set of commands that can be executed by MATLAB. M-files syntax: *.m These can be created as a text file and saved as an M-file. To call the editor to make an M-file, type “edit”. M-files can also be made from within MATLAB via pull-down menus File->New->M-file or by highlighting the commands in the Command History window. In the Octave GUI, choose File->New File. Create an M-file that contains the commands used to make the table in Example 4 above! Save it on the Desktop as example4.m 28

29
29 MATLAB’s Path and Running an M- file To run the M-file we created, we need to put the directory containing the file in MATLAB’s path. To do this, type “addpath ‘DIRECTORY’ ”, where the ‘DIRECTORY’ is where the file is located, for example, a command like this should be used: “addpath ‘C:\Users\MAK\Desktop’” In MATLAB, this can also be done via the pull-down menus File->Set Path. To run the set of commands, type the name of the M-file in the Command Window!

30
30 The Plot Command MATLAB’s “plot” command is used to draw graphs of functions. This is done by plotting (x, y) ordered pairs on a coordinate plane. To plot the table created above, use the command “plot(x,y)”. Notice that the graph we get is very jagged – to smooth it out, add more x-values and corresponding y-values to the vectors x and y!

31
31 The Plot Command To the right is the same graph as above, with the commands: x = [1:0.05:5]; y = (x.*sin(x.^2)-4)./(x+1); plot(x,y) To add a title, axes labels, and a grid to the graph, use the “title”, “xlabel”, “ylabel”, and “grid” commands! title(‘y = f(x)’) xlabel(‘x’) ylabel(‘y’) grid on Try these, then put them into your example4 M-file. Try plotting y = x*cos(x) on the interval [-,3].

32
32 Plotting More Than One Function To plot more than one function on the same graph, use the plot command with the sets of input and output variables listed in order: plot(x,y,x1,y1). Colors and plot styles can also be specified – to see a complete list of the available options, type “help plot” or “doc plot”. Here is how to plot the functions f(x) = x^2 and f’(x) = 2x on the same graph! x = [-1:0.1:2]; y = x.*x; y1 = 2*x; plot(x,y,’ro’,x,y1,’b-’) title(‘y = f(x) and y = f’’(x)’) xlabel(‘x’) ylabel(‘y’) legend(’y=x^2’,’y’’=2x’)

33
33 Piecewise Defined Functions In order to plot a piecewise defined function, we concatenate the separate pieces. x1 = [-2:0.1:0]; y1 = x1.^2 + 1; x2 = [0:0.1:2]; y2 = sin(x2) +1; x = [x1 x2]; y = [y1 y2]; plot(x,y,’g’) xlabel(‘x’) ylabel(‘y’) title(’f(x)=x^2, if x is in [-2,0]; sin(x) + 1, if x is in (0,2]’)

34
34 References Using MATLAB in Calculus by Gary Jenson MATLAB Help Files – Getting Started MATLAB Tutorial from the University of Utah at http://www.math.utah.edu/lab/ms/matl ab/matlab.html http://www.math.utah.edu/lab/ms/matl ab/matlab.html Octave http://octave.sourceforge.net/ http://www.gnu.org/software/octave/

Similar presentations

© 2019 SlidePlayer.com Inc.

All rights reserved.

To make this website work, we log user data and share it with processors. To use this website, you must agree to our Privacy Policy, including cookie policy.

Ads by Google