Matlab Environment Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour Chapter 2 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers In this chapter we’ll… Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 • Get started with Matlab • Explore the Matlab windows • Solve some problems using Matlab • Learn how to save our work 500 100 200 300 400 500 Matlab for Engineers Section 2.1 Getting Started Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour • In Windows or Apple operating systems click on the desktop icon 100 200 • In Unix type 300 400 500 100 200 300 400 500 matlab At the shell prompt Matlab for Engineers Matlab opens to a default window configuration Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 • Matlab uses a standard windows menu bar • To exit Matlab use the close icon Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour Command Window Current Directory Enter commands at the prompt 100 200 MATLAB Windows 300 400 500 100 200 300 400 500 Command History Workspace Window Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 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 Matlab for Engineers To watch this demonstration, click in the figure window. To move to the next slide use the down arrow Section 2.2 Matlab Windows Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 • Matlab uses several different windows to display data, commands and results. • They are not necessarily all open at once 400 500 100 200 300 400 500 Matlab for Engineers Let’s look at the windows one at a time Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers Command Window Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 • Similar to a scratch pad • Once you hit enter, you can’t edit Command Window any commands • You can retype them or use the arrow keys to retrieve commands and edit them before hitting enter again Matlab for Engineers Command History Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 • Records the commands you issue in the command window • When you exit the command window, or when you issue the clc Command History command, the command window is cleared • But the command history remains Matlab for Engineers Command History Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour • You can transfer commands from the command history to the command window – Double click on a command – Click and drag 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers To watch this demonstration, click in the figure window. To move to the next slide use the down arrow Workspace Window Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour • Keeps track of the variables you’ve defined – Name – Value Workspace Window – Class – Size – Bytes 100 200 300 400 500 100 200 300 400 500 • You may need to click on the name bar and select size and bytes in order to see these parameters Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers To watch this demonstration, click in the figure window. To move to the next slide use the down arrow Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 When you define variables in the command window, they are listed in the workspace window Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 Scalar -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 Vector 5 time, hour 100 2-D Matrix 200 300 400 500 100 200 300 400 500 Matlab for Engineers Current Directory Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 • 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 Matlab for Engineers Document Window Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 • 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 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour Document Window 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers Figure Window Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 • When Figures are created a new window opens • It’s extremely easy to create graphs in Matlab 200 300 400 500 100 200 300 400 500 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour The semicolon suppresses the output from each command 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers Editing Window Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 • This window allows you to type and save a series of commands without executing them • There are several ways to open an editing window 400 500 100 200 300 400 500 – From the file menu – With the new file icon Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour Open an editing window from the file menu or with the new file icon 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 Save and Run -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 Write your code in the editing window, then run it using the Save and Run icon 300 400 500 100 200 300 400 500 Matlab for Engineers Section 2.3 Solving Problems with Matlab Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 • We’ve already solved some simple problems • We need to understand how Matlab works to solve more complicated problems Matlab for Engineers Variables Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 • 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 Matlab for Engineers Naming Variables Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 • 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 Matlab for Engineers Use the iskeyword function for a list of keywords Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 iskeyword ans = 'break' 'case' 'catch' 'continue' 'else' 'elseif' 'end' 'for' 'function' 'global' 'if' 'otherwise' 'persistent' 'return' 'switch' 'try' 'while' Matlab for Engineers You can reassign function names Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour • Matlab will let you use built-in function names as variables – but it’s a really bad idea 100 200 • sin = 3 300 400 500 100 200 300 400 500 changes sin from a function to a variable name Matlab for Engineers Practice Exercise 2.2 Which of these names are allowed in Matlab? Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 • • • • • • • • • • • • test Test if x x my-book my_book x Thisisoneverylongnamebutisitstillallowed? 1stgroup x group_one zzaAbc z34wAwy?12# x x sin bad log idea Matlab for Engineers Matrices in Matlab The basic data type Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour • Group of numbers arranged into rows and columns • Single Value (Scalar) – Matrix with one row and one column 100 200 • Vector (One dimensional matrix) 300 400 500 100 200 300 400 500 – One row or one column • Matrix (Two dimensional) Matlab for Engineers Scalar Calculations Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour • You can use MATLAB like you’d use a calculator Command Prompt 100 200 300 >> 9 + 10 ans=19 400 500 100 200 300 400 500 Result Matlab for Engineers Assignment Operator Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 • To define a variable a we might type a=1+2 which should be read as: a is assigned a value of 1+2 400 500 100 200 300 400 500 Matlab for Engineers How is the assignment operator different from an equality? Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 • 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 Matlab for Engineers Is that really different? Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 • 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 Matlab for Engineers Order of Operation Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 • Same as you’ve learned in math class • Same as your calculator – Parentheses first – Exponentiation – Multiplication / division – Addition / subtraction Matlab for Engineers Order of Operation Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 5*(3+6) = 45 5*3+6 = 21 100 White space does not matter!!! 200 300 400 500 100 200 300 400 500 5*3 + 6 = 21 Matlab for Engineers Parentheses Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 • Use only ( ) • { } and [ ] mean something different • MATLAB does not assume operators 500 100 200 300 400 500 5 * (3+4) not 5(3+4) Matlab for Engineers Compute from left to right Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 5*6/6*5 = 25 5*6/(6*5) = 1 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers Here’s an example Find the surface area of a cylinder Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour r = radius r=5 h = height h = 10 π r2 2π r * h 100 π r2 200 300 400 500 100 200 300 400 500 SA 2r 2rh 2r (r h) 2 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers To watch this demonstration, click in the figure window. To move to the next slide use the down arrow Array Operations Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour • Using Matlab as a glorified calculator is OK, but its real strength is in matrix manipulations 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour To create a row vector, enclose a list of values in brackets 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 You may use either a space or a comma as a “delimiter” in a row vector 200 300 400 500 100 200 300 400 500 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 Use a semicolon as a delimiter to create a new row 200 300 400 500 100 200 300 400 500 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 Use a semicolon as a delimiter to create a new row 200 300 400 500 100 200 300 400 500 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 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 Matlab for Engineers Shortcuts Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 • 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 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 The default increment is 1, but if you want to use a different increment put it between the first and final values Matlab for Engineers To calculate spacing between elements use… Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour • linspace • logspace 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 number of elements in the array -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour Initial value in the array Final value in the array 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour number of elements in the array Initial value in the array expressed as a power of 10 100 200 300 Final value in the array expressed as a power of 10 400 500 100 200 300 400 500 Matlab for Engineers Hint Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 • You can include mathematical operations inside a matrix definition statement. • For example 200 300 400 500 100 200 300 400 500 a = [0: pi/10: pi] Matlab for Engineers Mixed calculations between scalars and arrays Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 • 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 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour Addition between arrays is performed on corresponding elements 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour Multiplication between arrays is performed on corresponding elements if the .* operator is used 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 Matlab interprets * to mean matrix multiplication. The arrays a and b are not the correct size for matrix multiplication in this example Matlab for Engineers Array Operations Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour • Array multiplication • Array division • Array exponentiation 100 200 300 In each case the size of the arrays must match 400 500 100 200 300 400 500 Matlab for Engineers .* ./ .^ The matrix capability of Matlab makes it easy to do repetitive calculations Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour • 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 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 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 Matlab for Engineers More about pi Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 • 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. Matlab for Engineers Transpose Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour • The transpose operator changes rows to columns or vice versa. 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 The transpose operator makes it easy to create tables Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 table =[degrees;radians]’ would have given the same result Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 The transpose operator works on both one dimensional and two dimensional arrays Matlab for Engineers Number Display Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 • 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.) Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 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 1023 ) Matlab for Engineers Display Format Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 • 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 Matlab for Engineers Default Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 • 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 Matlab for Engineers Other formats Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 • 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 Matlab for Engineers Really Big and Really Small Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 • 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 Matlab for Engineers Common Scale Factor Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 • 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. Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 Common Scale Factor 200 300 400 500 100 200 300 400 500 Matlab for Engineers Two other formats Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour • format + • format rat 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers Spacing in the command window Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour • The format command also allows us to control how tightly information is spaced in the command window – format compact – format loose – (default) 100 200 300 400 500 100 200 300 400 500 Notice that the value of A is still being displayed using the rat format, because we haven’t changed it back to format short • Most of the examples in this presentation use format compact Matlab for Engineers Section 2.4 Saving Your Work Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour • If you save a MATLAB session, all that is saved are the values of the variables you have named 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour Variables are saved, not the work space 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour Save either by using the file menu or... 100 Save with a command in the command window 200 300 400 500 100 200 300 400 500 Matlab for Engineers Matlab automatically saves to a .mat file Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour • If you want to save to another format, such as .dat, you need to explicitly tell the program 100 200 save <file_name> <variable_list> -ascii 300 400 500 100 200 300 400 500 Matlab for Engineers Script M-files Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 • 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 500 100 200 300 400 500 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour • The file is saved into the current directory • It runs in the command window 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers Comments Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 300 400 500 100 200 300 400 500 • Be sure to comment your code – Add your name – Date – Section # – Assignment # – Descriptions of what you are doing and why Matlab for Engineers Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour The % sign identifies comments You need one on each line 100 200 300 400 500 100 200 300 400 500 Matlab for Engineers Summary Rate of Change Rate of temperature change, degrees/hour -5 -4.5 -4 -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time, hour 100 200 • • • • Introduced the MATLAB Windows Basic matrix definition Save and retrieve MATLAB data Create and use script M-files 300 400 500 100 200 300 400 500 Matlab for Engineers