Introduction to matlab 1

Page 1

Introduction to Matlab: Part 1 by Dr. Luma Naji Mohammed Tawfiq


Topics Covered • Basic Ideas: – MATLAB Desktop manipulation – Basic Commands – Matrix Operations – Format – Using Help – Save • Using MATLAB Functions: – Quick function guide – Trigonometric functions – Logical Operations • Plotting with MATLAB: – Optimization Problem – Help Plot – Importing


BASIC IDEAS


Open MATLAB •Programs>Math and Simulation Software>MATLAB>R2008b>MATLAB R2008b


Menu Bar • Contains the commands you can use to perform certain tasks


Current Directory • Shows the active directory.


Start Button 1/3 • Shows items such as: Help, Demos, Product Webpage.


Start Button 2/3 • Select the “Start Button” • Mouse over the “Web button” • Select the “The MathWorks Web Site” to access the product webpage


Start button 3/3


Command Window • This is where the variables, and MATLAB commands are entered


Workspace • Creates, imports, displays the name, value, min, and max size of the variables.


Command History Window • Shows the time/date at which commands were issued.


Window Manipulation 1/8 • Can customize the desktop layout on the local machine. Here is the default layout:


Window Manipulation 2/8 • First on the menu bar: Help>Product help. A window should shift from the left to the right


Window Manipulation 3/8 • Select the curved arrow on the blue bar in the help window. This will un-dock the help browser. • Exit the help browser


Window Manipulation 4/8 • Press with the left mouse button and hold on the workspace blue bar • Drag the workspace window from right to left until the outline matches the picture below • Release the mouse button


Window Manipulation 5/8 • The workspace window is now “tabbed” with the working directory window on the left side of the desktop.


Window Manipulation 6/8 • Drag the Command history window on the right side of the screen to the left bottom side until it matches the outline in the picture below.


Window Manipulation 7/8 • The desktop layout should now be something like this:


Window Manipulation 8/8 • Now save the layout of desktop: – In the menu bar: Desktop>Save Layout...>Type: “practicum default”>press OK


BASIC COMMANDS


Basic Commands 1/6 • x=5 – Press the enter button to enter a variable, or command • y=2; • x+y • 2*x+3*x/y • z=3*y^2


Basic Commands 2/6 • To review the value of the variables recently entered look at the “Workspace window: – Shows the name of the variable – Value of the variable – Min and max values


Basic Commands 3/6 • Use up arrow commands to re-enter a previous command, in reverse sequence – Press the up arrow key once, look at the command window, and press enter

– Press the up arrow key 5 times, then press the down arrow key twice, then press enter: • After the 5 times using the arrow key, it shows the x variable • Then the down arrow key sequence moves down to show the x+y variable


Basic Commands 4/6 • Instead of using the arrow keys, use the Command History window: – Inside the Command History window, single “left click”: “2*x+3*x/y”

– “Right click” the highlighted command and select “Copy”:

– Click anywhere inside the “Command Window” and right click next to “>>” – Press “Paste button”, the command is now entered into the command window, now press enter button on the keyboard.


Basic Commands 5/6 • Enter in the “clc” command: – Clears the contents of the command window, but does not erase variables that are not in the current memory – Commands in MATLAB is case sensitive • Enter “clear x”: – Clears the x-variable, and only that variable in the workspace • Enter “clear all”: – Clears all of the variables in the workspace


Basic Commands 6/6 • Generate a range of values: – Type “x=0:0.25:100” • This generates an array from 0 to 100, in increments of 0.25 • array= Start value: Increment: End value – Type “x=0:100” • This generates an array from 0 to 100, in increments of 1 • array=Start value: End value – Default increment is 1 if it is not specified – Type “countdown=5:-1:0” • This generates an array with decreasing value, dependent on the increment. • Array=Maximum value (Start value): decrease increment: minimum value


Matrix Operations 1/5 • Go to Page 422 in the MATLAB Handout 0 5 0   4 6  2  1     A  8 3 7, B  7 2 3 , C    2  9  2 9 1 3  4  5 

• Matrix addition: 0 5 0   4 A  B  8 3 7  7 9  2 9 1  4 6  2  0 B  A  7 2 3   8 1 3  4 9 A  B  B  A

6  2  0  4 5  6 2 3   8  7 3  2 3  4  9  1  2  3 5 0 4  0 65   3 7  7  8 23  2 9 1  9 3  (2)

0  (2)  4 11  2 7  3   15 5 10  9  (4)  10 1 5   2  0  4 11  2 3  7   15 5 10   4  9 10 1 5 

• Matrix Multiplication: 0 5 0   4 A B  8 3 7  7 9  2 9 1  4 6  2  0 B A  7 2 3   8 1 3  4 9 A B  B A

6  2  0  4  5  7  0  1 06  52  03 0   2  5  3  0   4  35 10 15    2 3    8  4  3  7  7 1 8 6  3 2  7 3 8   2  3  3  7   4   60 75  35 3  4 9  4   2  7  9  1 9  6   2  2  9  3 9   2   2  3  9   4  31 77  60 0 4  0  6  8   2  9 4  5  6  3   2   2 4  0  6  7   2  9  30 42 24  3 7   7  0  2  8  3  9 7  5  2  3  3   2 7  0  2  7  3  9    43 35 41   2 9 1  0  3  8   4  9 1  5  3  3   4   2 1  0  3  7   4  9   12 22  15 5


Matrix Operations 2/5 • Matrix multiplication can also be considered as linear equations.  A11 A12 D  A C    A21 A22  A31 A32 A11  C1  A12  C 2  A13  C 3

A13   C1   D1  A23   C 2    D2  A33  C 3   D3   D1

A21  C1  A22  C 2  A23  C 3  D2 A31  C1  A32  C 2  A33  C 3  D3

0 5 0  1  0   1  5  2  0  5  10  D  A C   8 3 7   2    8   1  3  2  7  5   33 9  2 9  5  9   1   2   2  9  5 32

• The transpose of any matrix switches the column with row AT

 A11   A21  A31

A12 A22 A32

T

A13   A11  A23    A12  A13 A33 

A21 A22 A23

T

A31  0 5 0 0 8 9     A32   8 3 7  5 3  2 0 7 9  A33  9  2 9

• The determinant a matrix  A11 A   A21  A31

A12 A22 A32

A13  A23   A11   A22  A33  A32  A23   A12   A21  A33  A31  A23   A13   A21  A32  A31  A22  A33 

0 5 0  A  8 3 7  0  3  9   2  7   5  8  9  9  7   0  8   2  9  3  45 9  2 9


Matrix Operations 3/5 • The inverse of the A matrix: – Find the determinant of the matrix – Find the transpose of the matrix – Find the cofactors of the matrix – Insert the cofactors into the matrix A1

 1 1 3  2 41    A11  A 7 9  45   1 1 8 9 9   A21  1    A 7 9  45   1 1 8 9  43    A31  A 3  2  45 

A

 0.9111 1  0.7778    0.2 0 0   0.9556  1 0.8889 

1

1 5  A 0 1 0 1 A22   A 0 1 0 1 A32  1   A 5

A121  1 

 2  45  9 45 9 0 9 9 45   2 45

1 5 3 35     A 0 7 45   1 0 8 1 A23  1    0 A 0 7   0 8 1  40 1 A33     A 5 3  A131 


Matrix Operations 4/5 • To create a matrix inside MATLAB: – Brackets “[]” – Space or comma “,” , indicates a new column – A semicolon “;” indicates a new row • Follow the directions inside example 15.4 (pg. 422-423) • Add Matrices (A+B) • Subtract Matrices (A-B) • Multiply Matrices (A*B) • Find the determinant of a Matrix (det(A)) • Follow the directions inside example 15.5 (pg. 424) – Solve a set of linear equations – Gauss elimination (A\B) – Inverse of a matrix (A-1=inv(A)) – A\B=inv(A)*B


Matrix Operations 5/5 • Element by element operation:  A11 A.  B   A21  A31

A12 A22 A32

A13   B11 A23 .   B21 A33   B31

B12 B22 B32

B13   A11  B11 B23    A21  B21 B33   A31  B31

A12  B12 A22  B22 A32  B32

A13  B13  A23  B23  A33  B33 

0  0 5 0 4 6  2 0  4 5  6 0   2  0 30        A.  B  8 3 7.  7 2 3   8  7 3  2 7  3   56 6 21  9  2 9 1 3  4  9  1  2  3 9   4  9  6  36

• In the MATLAB command window type: a.*b


Format • Display Functions: – format: • Allows you to display values in certain ways. – disp: • Used to display text or values. Rarely used, often you can just type in the variable name, but disp will leave off the name. – fprintf: • Also a display command that allows you to print text and/or values with a desired number of digits. \n and \t are line breaks and tabs, respectively. • Example 15.1 (pg 404) – Uses the fprintf function to display formatted information to the screen • The %g term is a more compact form of exponential notation, insignificant zeros do not print • %s is for character strings, \\ is a backslash • For more information on fprintf commands, properties, etc. go to MATLAB help and type the “fprintf”


Using Help • How to launch help: – Three ways: • Press the F1 key on the keyboard, a window will appear. On the bottom left corner of that window select the “Open Help Browser”.


Using Help • On the menu bar Select Help> Product Help

• Instead of typing a function name in the help browser a faster way to determine what the function does is to do the following: – Highlight the function>Right click the highlighted text>select “Help on Selection”.


Using Help


Save • If there are many variables in the directory, rather than retyping them, they can be saved for a later time in a data file. – The data file is similar to a *.dat file format but using MATLAB‟s format called a *.mat. • To save the variable workspace: – File>Save Workspace As> Type in filename: “practicum_lab_001”>select the “create new file folder icon > type: “Practicum_lab” in the popup window – Double click the new folder created.


Save • • • •

Change the current directory: Below the menu bar select the button Click the “+” (plus sign) next to MATLAB, and select the “Practicum _lab” folder Press the “OK” button


Save • To create a m-file: – File> New>M-file


Save • Example of a function inside MATLAB • In the command window: – Type “why” • Now type “open why”


USING BASIC MATLAB FUNCTIONS


Basic MATLAB functions • All of the functions (name, description, examples) in MATLAB can be found using help • Anyone using MATLAB uses this resources constantly • In the handout go to page 409-410 and look at table 15.7 and 15.8 – Shows some basic MATLAB functions • Do example 15.2 in pg 409 • Most trigonometric functions in MATLAB are in radians – Radian trigonometric functions: • sin, cos, tan, atan, atan2 – Degree trigonometric functions: • sind, cosd, tand, atand – Less number of degree trig functions – Conversion from radians to degree • rad2deg(angle in) – Conversion from degree to radians • deg2rad(angle in)


Basic MATLAB functions •

Logical operators: – Table 15.10 and 15.11 pg 412, – If statements use logical operators to determine if something is true/false – Example of a simple if statement • if test • statement • end – Example of a if/else statement • if test • statement • else • statement • end – Example of a if/elseif/else statement • if test • statement • elseif test • statement • else • statement • end


PLOTTING WITH MATLAB


Plotting with MATLAB • Go to pg 414, look at Example 15.3 • Optimization problem: – How to determine the maximum volume of a sheet of paper square cuts at every corner by „x‟ centimeters

V  10  2  x   10  2 x   x  10  2 x   x 2

– This can easily be solved using calculus by taking the differentiation of volume with respect to x and making it equal to zero dV 2  0  10  2  x    4  x  10  2  x   12  x 2  80  x  100 dx x

  80 

 802  4  12  100 2  12

 5.0,1.6667

– Now plug the values into the volume equation, and the cut size will be where the volume is at maximum 2   V1  10  2  5.0  5.0  0   xcut  1.6667 Vmax  max    V  10  2 1.6667 2 1.6667  74.0741  2 

• Also looking at the volume equation, the larger of the two x values (5 cm) will make the volume zero, or at least smaller


Plotting with MATLAB • Do Example 15.3 (pg 414) • When finished play around with the plot and line styles – Go on the help browser and type “plot” in keyword search • This will have links to other functions, and line styles – Line styles=Can be found in help (“LineSpec”, or Table 15.12 (pg. 416)) • Look up some other types of plot anotation like “legend”, grid, zooming (axis command), x-y-z labels, titles, 3d plots, 3d mesh plots, 3d surface plots, text properties which use the TEX commands which can have greek alphabet, symbols, etc. • Try some of the demos found in help • Best way to learn MATLAB is from the help browser, and by actually using it.


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.