Presentation is loading. Please wait.

Presentation is loading. Please wait.

Matlab for Engineers Matlab Environment Chapter 2.

Similar presentations


Presentation on theme: "Matlab for Engineers Matlab Environment Chapter 2."— Presentation transcript:

1 Matlab for Engineers Matlab Environment Chapter 2

2 Matlab for Engineers In this chapter we’ll… Get started with Matlab Explore the Matlab windows Solve some problems using Matlab Learn how to save our work

3 Matlab for Engineers Section 2.1 Getting Started In Windows or Apple operating systems click on the desktop icon In Unix type matlab At the shell prompt

4 Matlab for Engineers Matlab opens to a default window configuration

5 Matlab for Engineers Matlab uses a standard windows menu bar To exit Matlab use the close icon

6 Matlab for Engineers Command Window Enter commands at the prompt Current Directory MATLAB Windows Command History Workspace Window

7 Matlab for Engineers You can use the command window much like you’d use a calculator The standard order of operation rules apply Let’s try Practice Exercise 2.1 To watch this demonstration, click in the figure window. To move to the next slide use the down arrow

8 Matlab for Engineers Section 2.2 Matlab Windows Matlab uses several different windows to display data, commands and results. They are not necessarily all open at once

9 Matlab for Engineers Let’s look at the windows one at a time

10 Matlab for Engineers Command Window Similar to a scratch pad Once you hit enter, you can’t edit any commands You can retype them or use the arrow keys to retrieve commands and edit them before hitting enter again Command Window

11 Matlab for Engineers Command History Records the commands you issue in the command window When you exit the command window, or when you issue the clc command, the command window is cleared But the command history remains Command History

12 Matlab for Engineers Command History You can transfer commands from the command history to the command window –Double click on a command –Click and drag

13 Matlab for Engineers To watch this demonstration, click in the figure window. To move to the next slide use the down arrow

14 Matlab for Engineers Workspace Window Keeps track of the variables you’ve defined –Name –Value –Class –Size –Bytes You may need to click on the name bar and select size and bytes in order to see these parameters Workspace Window

15 Matlab for Engineers To watch this demonstration, click in the figure window. To move to the next slide use the down arrow

16 Matlab for Engineers When you define variables in the command window, they are listed in the workspace window

17 Matlab for Engineers Scalar Vector 2-D Matrix

18 Matlab for Engineers Current Directory The current directory window is a list of files When you try to load information from a file or try to save information – Matlab uses the current directory

19 Matlab for Engineers Document Window If you double click on any variable in the workspace window Matlab launches a document window containing the array editor You can edit variables in the array editor

20 Matlab for Engineers Document Window

21 Matlab for Engineers Figure Window When Figures are created a new window opens It’s extremely easy to create graphs in Matlab

22 Matlab for Engineers The semicolon suppresses the output from each command

23 Matlab for Engineers

24 Editing Window This window allows you to type and save a series of commands without executing them There are several ways to open an editing window –From the file menu –With the new file icon

25 Matlab for Engineers Open an editing window from the file menu or with the new file icon

26 Matlab for Engineers Save and Run Write your code in the editing window, then run it using the Save and Run icon

27 Matlab for Engineers Section 2.3 Solving Problems with Matlab We’ve already solved some simple problems We need to understand how Matlab works to solve more complicated problems

28 Matlab for Engineers Variables MATLAB allows you to assign a value to a variable A=3 Should be read as A is assigned a value of 3 Use the variables in subsequent calculations

29 Matlab for Engineers Naming Variables All names must start with a letter They may contain letters, numbers and the underscore ( _ ) Names are case sensitive There are certain keywords you can’t use

30 Matlab for Engineers Use the iskeyword function for a list of keywords iskeyword ans = 'break' 'case' 'catch' 'continue' 'else' 'elseif' 'end' 'for' 'function' 'global' 'if' 'otherwise' 'persistent' 'return' 'switch' 'try' 'while'

31 Matlab for Engineers You can reassign function names Matlab will let you use built-in function names as variables – but it’s a really bad idea sin = 3 changes sin from a function to a variable name

32 Matlab for Engineers Practice Exercise 2.2 Which of these names are allowed in Matlab? test Test if my-book my_book Thisisoneverylongnamebutisitstillallowed? 1stgroup group_one zzaAbc z34wAwy?12# sin log x x x x x x bad idea

33 Matlab for Engineers Matrices in Matlab The basic data type Group of numbers arranged into rows and columns Single Value (Scalar) –Matrix with one row and one column Vector (One dimensional matrix) –One row or one column Matrix (Two dimensional)

34 Matlab for Engineers Scalar Calculations You can use MATLAB like you’d use a calculator >> 9 + 10 ans=19 Command Prompt Result

35 Matlab for Engineers Assignment Operator To define a variable a we might type a=1+2 which should be read as: a is assigned a value of 1+2

36 Matlab for Engineers How is the assignment operator different from an equality? In algebra the equation x=3+5 means that both sides are the same In computers when we say x=3+5 we are telling the machine to store the value on the right hand side of the equation in a memory location, and to name that location x

37 Matlab for Engineers Is that really different? Yes!!! In algebra this is not a true statement x=x+1 In computers (assignment statements) it means replace the value in the memory location named x, with a new value equal to x+1

38 Matlab for Engineers Order of Operation Same as you’ve learned in math class Same as your calculator –Parentheses first –Exponentiation –Multiplication / division –Addition / subtraction

39 Matlab for Engineers Order of Operation 5*(3+6)= 45 5*3+6= 21 White space does not matter!!! 5*3 + 6= 21

40 Matlab for Engineers Parentheses Use only ( ) { } and [ ] mean something different MATLAB does not assume operators 5 * (3+4) not 5(3+4)

41 Matlab for Engineers Compute from left to right 5*6/6*5= 25 5*6/(6*5)= 1

42 Matlab for Engineers Here’s an example Find the surface area of a cylinder r = radius r = 5 h = height h = 10 π r 2 2π r * h

43 Matlab for Engineers To watch this demonstration, click in the figure window. To move to the next slide use the down arrow

44 Matlab for Engineers Array Operations Using Matlab as a glorified calculator is OK, but its real strength is in matrix manipulations

45 Matlab for Engineers To create a row vector, enclose a list of values in brackets

46 Matlab for Engineers You may use either a space or a comma as a “delimiter” in a row vector

47 Matlab for Engineers Use a semicolon as a delimiter to create a new row

48 Matlab for Engineers Use a semicolon as a delimiter to create a new row

49 Matlab for Engineers Hint: It’s easier to keep track of how many values you’ve entered into a matrix, if you enter each row on a separate line. The semicolons are optional

50 Matlab for Engineers Shortcuts While a complicated matrix might have to be entered by hand, evenly spaced matrices can be entered much more readily. The command b= 1:5 or the command b = [1:5] both return a row matrix

51 Matlab for Engineers The default increment is 1, but if you want to use a different increment put it between the first and final values

52 Matlab for Engineers To calculate spacing between elements use… linspace logspace

53 Matlab for Engineers Initial value in the array Final value in the array number of elements in the array

54 Matlab for Engineers Initial value in the array expressed as a power of 10 Final value in the array expressed as a power of 10 number of elements in the array

55 Matlab for Engineers Hint You can include mathematical operations inside a matrix definition statement. For example a = [0: pi/10: pi]

56 Matlab for Engineers Mixed calculations between scalars and arrays Matrices can be used in many calculations with scalars There is no confusion when we perform addition and subtraction Multiplication and division are a little different In matrix mathematics the multiplication operator (*) has a very specific meaning

57 Matlab for Engineers

58 Addition between arrays is performed on corresponding elements

59 Matlab for Engineers Multiplication between arrays is performed on corresponding elements if the.* operator is used

60 Matlab for Engineers Matlab interprets * to mean matrix multiplication. The arrays a and b are not the correct size for matrix multiplication in this example

61 Matlab for Engineers Array Operations Array multiplication.* Array division./ Array exponentiation.^ In each case the size of the arrays must match

62 Matlab for Engineers The matrix capability of Matlab makes it easy to do repetitive calculations For example, assume you have a list of angles in degrees that you would like to convert to radians. –First put the values into a matrix. –Perform the calculation

63 Matlab for Engineers

64 Either the * or the.* operator can be used for this problem, because it is composed of scalars and a single matrix The value of pi is built into Matlab as a floating point number, called pi

65 Matlab for Engineers More about pi Because pi is an irrational number, it can not be expressed exactly with a floating point representation The Matlab constant, pi, is really an approximation. If you find sin(pi) Matlab returns a very small number.

66 Matlab for Engineers Transpose The transpose operator changes rows to columns or vice versa.

67 Matlab for Engineers The transpose operator makes it easy to create tables

68 Matlab for Engineers table =[degrees;radians]’ would have given the same result

69 Matlab for Engineers The transpose operator works on both one dimensional and two dimensional arrays

70 Matlab for Engineers Number Display Scientific Notation –Although you can enter any number in decimal notation, it isn’t always the best way to represent very large or very small numbers –In Matlab, values in scientific notation are designated with an e between the decimal number and exponent. (Your calculator probably uses similar notation.)

71 Matlab for Engineers It is important to omit blanks between the decimal number and the exponent. For example, Matlab will interpret 6.022 e23 as two values (6.022 and 10 23 )

72 Matlab for Engineers Display Format Multiple display formats are available No matter what display format you choose, Matlab uses double precision floating point numbers in its calculations Matlab handles both integers and decimal numbers as floating point numbers

73 Matlab for Engineers Default The default format is called short If an integer is entered it is displayed without trailing zeros If a floating point number is entered four decimal digits are displayed

74 Matlab for Engineers Other formats Changing the format affects all subsequent displays –format long results in 14 decimal digits –format bank results in 2 decimal digits –format short returns the display to the default 4 decimal digits

75 Matlab for Engineers Really Big and Really Small When numbers become too large or too small for Matlab to display using the default format, it automatically expresses them in scientific notation You can force scientific notation with –format short e –format long e

76 Matlab for Engineers Common Scale Factor For long and short formats, a common scale factor is applied to the entire matrix if some of the elements become very large, or very small. This scale factor is printed along with the scaled values.

77 Matlab for Engineers Common Scale Factor

78 Matlab for Engineers Two other formats format + format rat

79 Matlab for Engineers Spacing in the command window The format command also allows us to control how tightly information is spaced in the command window –format compact –format loose – (default) Most of the examples in this presentation use format compact Notice that the value of A is still being displayed using the rat format, because we haven’t changed it back to format short

80 Matlab for Engineers Section 2.4 Saving Your Work If you save a MATLAB session, all that is saved are the values of the variables you have named

81 Matlab for Engineers Variables are saved, not the work space

82 Matlab for Engineers Save either by using the file menu or... Save with a command in the command window

83 Matlab for Engineers Matlab automatically saves to a.mat file If you want to save to another format, such as.dat, you need to explicitly tell the program save -ascii

84 Matlab for Engineers Script M-files If you want to save your work, you need to create an M-file File->New->M-file Type your commands in the edit window that opens

85 Matlab for Engineers The file is saved into the current directory It runs in the command window

86 Matlab for Engineers

87 Comments Be sure to comment your code –Add your name –Date –Section # –Assignment # –Descriptions of what you are doing and why

88 Matlab for Engineers The % sign identifies comments You need one on each line

89 Matlab for Engineers Summary Introduced the MATLAB Windows Basic matrix definition Save and retrieve MATLAB data Create and use script M-files


Download ppt "Matlab for Engineers Matlab Environment Chapter 2."

Similar presentations


Ads by Google