1 / 94

Introduction to MATLAB

Introduction to MATLAB. Introduction to MATLAB. Dr. Hiren D. Joshi Department of Computer Science Rollwala Computer Centre Gujarat University. What is MATLAB?.

hailey
Download Presentation

Introduction to MATLAB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to MATLAB Introduction to MATLAB Dr. Hiren D. Joshi Department of Computer Science Rollwala Computer Centre Gujarat University

  2. What is MATLAB? • MATLAB (short for MATrix LABoratory) is a special-purpose computer program optimized to perform engineering and scientific calculations. • MATLAB is a sophisticated mathematical computation and visualization (graphing, image processing and display) tool. • MATLAB is widely used across all disciplines of mathematics, scientific research & engineering.

  3. What is MATLAB? • The name MATLAB is short for Matrix Laboratory and sets the stage for how the software operates. • MATLAB is designed and optimized to perform mathematical operations on matrices. • You can choose to not take advantage of the matrix format, but your calculation time will suffer. • MATLAB also contains a high level scripting language for controlling MATLAB programatically.

  4. The Advantages of MATLAB • Ease of Use • Platform Independence • Predefined Functions • Device-Independent Plotting • Graphical User Interface • MATLAB Complier • Compile MATLAB programs into a device independent p-code, and then interpreting the p-code instruction at run-time.

  5. Disadvantages of MATLAB • It is an interpreted language and therefore can execute more slowly than complied languages. • A full copy of MATLAB is 5 to 10 times more expensive than a conventional C or Fortran complier.

  6. MATLAB Interface

  7. MATLAB Interface • MATLAB gives you a series of windows that allow you to control functions: • Command window • Current directory • Workspace • Command history • Additional windows appear as needed: • Document windows • Figure windows • Editing windows

  8. The Command Window • The R.H.S. of the default MATLAB desktop contains the Command Window. • A user can enter interactive commands at the command prompt (>>) in the command window. • >> Area = pi * 2.5 ^ 2 • Stores answer in variable area. • If a statement is too long to type on a single line, it may be continued on successive lines by typing an elllipsis(…) at the end of the first line and then continue on the second line.

  9. Command Window • >> X1 = 1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 • >> X1 = 1 + 1/2 + 1/3 + 1/4 ... + 1/5 + 1/6 • Instead of typing commands directly in the command window, a series of commands can be placed into a file and the entire file can be executed by typing its name in the command window. • Such files are called script files. • Script files also known as M-files, because they have a file-extension “.m”

  10. Getting Going – Command Window Try It! At the Command Prompt, type the following commands (press Enter after each): >> 25/5 >> 5^2 >> 25 + 5 >> sqrt(25) • To perform calculations in MATLAB, you type your command in the Command Window at the Command Prompt ( >> ) • You can type mathematical expressions just like with a scientific calculator • You can write simple programs. • You can tell MATLAB to run a program you have written in the MATLAB scripting language and have stored on the computer. MATLAB uses the standard order of operations: - Parentheses - Exponentiation - Division / Multiplication - Addition / Subtraction Tip: use your up-arrow key to bring back lines you have already typed. Press ESC to quit the edit.

  11. Getting Going – Command Window • If things went well, you should have a window that looks like the one at left (but with more spacing). • When MATLAB performs a calculation, it wants to “name” the result of that calculation. • Unless you give it a name, it will use “ans”. • If you reuse a name, MATLAB will “throw-out” the previously stored calculation. Be careful!

  12. Getting Going – Command Window Try It! To see this, at the Command Prompt (>>), type the following: >> ans [press Enter] >> x [press Enter] • The variable “ans” contains the last calculated value – 25, and “x” contains the defined value 100.

  13. Getting Going – Command Window • It is very easy to “clutter” up your Command Window. Use the command “clc” to erase it (but not get rid of any defined variables). Try It! At the Command Prompt (>>), type the following: >> clc [press Enter] Nice and clean!

  14. Getting Going – Command Window • Sometimes you would like to stop MATLAB from repeating what you typed into the Command Window. • To stop display, add a ; to the end of a command. • When first learning MATLAB unexpected results can happen, so a good way to learn is to leave the data display on. Try It! At the Command Prompt (>>), type the following: >> matrix = ones(1000) [press Enter] >> matrix = ones(1000);

  15. Getting Going – Workspace Window • The Workspace Window keeps track of the variables you have defined as you execute commands in the Command Window • Right now in our session we have three variables defined: “ans”, “matrix” and “x”

  16. Getting Going – Workspace Window • A list of the variables and arrays in the current workspace can be generated with the whos command. >> whos • Name Size Bytes Class • X1 1x1 8 double array • ans 1x1 8 double array • area 1x1 8 double array • circum 1x1 8 double array • radius 1x1 8 double array • string 1x33 66 char array • x 1x61 488 double array • y 1x61 488 double array

  17. Getting Going – Workspace Window • The contents of any variable or array may be determined by typing the appropriate name in the command window. • >> string • The area of the circle is 19.635 • A variable can be deleted from the workspace with the clear command. • Clear var1 var2 …. • The command clear [or clear variables] deletes all variables from the current workspace.

  18. Getting Going – Workspace Window • You can view and change the contents of a variable by double-clicking on it in the Workspace Window. Try It! • Perform the following steps: • Double-click on the variable “x” in the Workspace Window • Type “200” (no quotes) into the second column and press enter. • Check out the change in the Workspace Window. • Back at the Command Prompt, type “x” (no quotes) and press Enter. • Press the X at the top right of the Variable Editor to close the window

  19. Getting Going – Command History Window • The Command History Window keeps a running list of the commands you have entered at the Command Prompt. • Two ways you can execute a command that you see in the History Window: • Click and drag it to the Command Window. This allows you to edit the command if desired. • Select it in the History window and press ENTER. It automatically evaluates. Try It! • Perform the following: • Scroll through the Command History until you find sqrt(25) • Click and drag this line to the Command Window. • Press ENTER to evaluate the command.

  20. Getting Going – Current Directory Window • The Current Directory Window shows all the files in the location defined in the current directory path. This is the location MATLAB uses when accessing or saving files.

  21. Getting Going – Edit Window • An Edit Window is used to create new M-files or to modify existing ones. • The Edit Window allows you to write a series of commands (a program) and save those commands to your computer • The Edit Window is essentially a programming text editor with the MATLAB languages features highlighted in different colors. • Comments in an M-file appear in green • Variable and numbers appear in black • Complete character string appear in Magenta • Incomplete character string appear in red • Language keywords appear in Blue Try It! • Perform the following: • On the menu at the top of the screen, click on File > New > M-File

  22. Getting Going – Edit Window • After an M-file is saved, it may be executed by typing its name in the command window. • >>carea

  23. Figure Windows • A figure window is used to display MATLAB graphics. • A figure can be a 2-D or 3-D plot of data, an image or a GUI.

  24. Docking and Undocking Windows • When a window is docked, it appears as a pane within the MATLAB desktop. • When it is undocked it appears as an independent window separate from the desktop. • Dock (up arrow) • Undock (down arrow)

  25. Getting Help • Select ? From desktop toolbar • Type helpdesk in command window or • Type helpwin in the command window • Type Help at the command prompt, MATLAB will display a list of possible help topics in the command window. • Type Help FunctionName display help for perticular function • Lookfor FunctionName Ex. Lookfor inverse

  26. Getting Help • Help command searches for exact function name match, while lookfor command searches the quick summary information in each function for a match. • Lookfor slower than Help but improve chances of getting back useful information.

  27. A Few Important Command • Demo Display Demo • Clc Clear Command Window • Clf Clear Figure Window • Clear Cleared variables in the workspace • ^C Abort program execution • ! Any character after the ! Will be sent to the OS and executed as though they had been typed at the OS command prompt. • Diary Keep track of everything done during a MATLAB session. Diary off & Diary on

  28. The MATLAB Search Path • MATLAB has a search path that it uses to find M-files. • It looks for the same name as a variable. If it is a variable, MATLAB displays the current contents of the variable. • It checks to see if the name is an M-file in the current directory. If it is, MATLAB executes the function or command. • It checks to see if the name is an M-file in any directory in the search path. If it is, MATLAB executes the function or command

  29. Which filename/function name • It will display the path of your file >> which sinX • C:\MATLAB7\work\sinX.m

  30. Variables and Arrays • The fundamental unit of data in any MATLAB program is the array. • Array can be classified as either vectors or matrices. • Vector is used to describe an array with only one dimension, while the term matrix is usually used to describe an array with 2 or more dimensions. • The size of an array is specified by the number of rows and the number of columns in the array, with the number of rows mentioned first.

  31. Variables and Arrays 1 2 3 4 5 6 This is a 3 X 2 matrix containing 6 elements • A = B = [1 2 3 4] This is a 1 X 4 array containing 4 elements, known as a row vector C = 1 2 3 This is a 3 X 1 array containing 3 elements, known as a Column vector A(2,1) = 3 and C(2) = 2

  32. Variables and Arrays • a = [1 2;3 4;5 6] • b = [1 2 3 4] • c = [1;2;3]

  33. Variables and Arrays • Variables name must begin with a letter followed by any combination of letters, numbers and underscore(_) character. • Only the first 63 characters are significant; if more than 63 are used, the remaining characters will be ignored. • The MATLAB language is case-sensitive.

  34. Variables and Arrays • The most common types of MATLAB variables are double and char. • Double can hold real, imaginary or complex values. • Variables of type char consist of scalar or arrays of 16-bit values, each representing a single character • MATLAB is a weakly typed language. • Variables may be created at any time by simply assigning values to them, and the type of data assigned to the variable determines the type of variable that is created.

  35. Initializing Variables in MATLAB • MATLAB variables are automatically created when they are initialized. • Assign data to the variable. • Input data into the variable from the keyboard. • Read data from a file. • Var = expresssion • Eg. Var = 40i; • Var2 = var/5; • Array = [1 2 3 4]; • X = 1; y = 2;

  36. Initializing Variables in MATLAB • 2 3 • 4 5 6 • [1,2,3;4,5,6] = • 2 3 • 4 5 6 [1,2,3 4,5,6] = [ ] This expression contains an empty array, which contains no rows and no columns. Note that this is not the same as an array containing zeros. The number of elements in every row of an array must be the same, and the number of elements in every column must be the same.

  37. Initializing Variables in MATLAB • A = [0 1+7] • B = [A(2) 7 A] • C(2,3) = 5 • D = [1 2] • D(4) = 4 then D=[1 2 0 4] • If a ; added at the end of the statement, the echoing disapperas.

  38. Initializing with Shortcut Expression • Colon operator specifies a whole series of values by specifying the first value, the stepping increment and the last value. • First:Incr:Last • Ex. a1 =1:2:10 • A1 = 1 3 5 7 9 • angles = (0.01:0.01:1.00)

  39. Initializing with Shortcut Expression • Shortcut expression can be combined with transpose operator(‘). • f=[1:4]’ • G = 1:4; • H = [G’ G’]

  40. Initializing with Built-In Functions • z1 = zeros(2) • Z2 = zeros(2,3) • Z3 = [1 2;3 4] • Z4 = zeros(size(z3)) • Ones(3) • eye

  41. MATLAB functions useful for Initializing Variables • Zeros(n) Generate an n X n matrix of zeros • Zeros(n,m) Generate an n X n matrix of zeros • Zeros(size(arr)) Generate a matrix of the zeros the same size as arr • ones(n) Generate an n X n matrix of ones • ones(n,m) Generate an n X n matrix of ones • ones(size(arr)) Generate a matrix of the ones the same size as arr

  42. MATLAB functions useful for Initializing Variables • Eye(n) Generate an n X n identity matrix • Eye(n,m) Generate an n X m identity matrix • Length(arr) Returns the length of a vector or the longest dimension of a 2-D array. • Size(arr) Returns two values specifying the number of rows and columns in arr.

  43. Initializing variables with Keyboard Input • Use input function • Single number or array • Array must be enclosed in brackets • Input function use ‘s’ as a second argument, then the input data is returned to user as a character string.

  44. Initializing variables with Keyboard Input • In1 = Input(‘Enter Data: ‘); • In2 = input(‘Enter Data: ‘,’s’); • In3 = input(‘Enter value: ‘) - only press enter

  45. Multidimensional Array • c(:,:,1) = [1,2,3;4,5,6]; • c(:,:,2) = [7 8 9;10,11,12]; >> whos c • Name Size Bytes Class • c 2x3x2 96 double array

  46. Multidimensional Array >> c • c(:,:,1) = 1 2 3 4 5 6 • c(:,:,2) = 7 8 9 10 11 12

  47. Storing Multidimensional Arrays in Memory b a Layout of values In memory for Array a. Data Values for array a.

  48. Multidimensional Array • A = [1 2 3;4 5 6;7 8 9;10 11 12] • A(5) = 2 • Always use the proper number of dimension when addressing a multidimensional number.

More Related