Introduction to Matlab for Engineers Instructor: Anh Thai Nhan Math 111, Ohlone Introduction to Matlab for Engineers Ohlone, Fremont 1/23
Reading materials Chapters 1, 2, and 3, Moore s textbook Introduction to Matlab for Engineers Ohlone, Fremont 2/23
What is Matlab? Matlab (Matrix Laboratory) is a multi-paradigm numerical computing environment and fourth-generation programming language. Originally designed as a tool for matrices, developed by MathWorks. Introduction to Matlab for Engineers Ohlone, Fremont 3/23
Why Matlab? Matlab is very easy to use. Matlab excels at computations involving matrices. We can use Matlab as your scientific calculator. Matlab has a bunch of built-in functions. Matlab excels at graphics, whereas high-level programming languages such as C, C++, or FORTRAN do not offer easy access to graphing. Introduction to Matlab for Engineers Ohlone, Fremont 4/23
Example: 3D graphic (Heat equation) Introduction to Matlab for Engineers Ohlone, Fremont 5/23
Matlab Interface Command Window: where you type commands Workspace: the current variables. Look at the workspace after typing x=[1:5] Current Directory: a list of paths recognized by Matlab Current Folder Toolbar: the file in the active directory Editor Window: where you will do most of your work Introduction to Matlab for Engineers Ohlone, Fremont 6/23
Moving Windows Docking and Undocking Menus allows you to choose what is shown Introduction to Matlab for Engineers Ohlone, Fremont 7/23
Working in the Command Window The Command Window is Good for quick work Good for calling scripts and functions Good for getting help Not editable Not easy to fix mistakes Not for long work Introduction to Matlab for Engineers Ohlone, Fremont 8/23
Getting Help Help Browser from the Desktop Help menu Type doc and the command name Type help and the command name Introduction to Matlab for Engineers Ohlone, Fremont 9/23
Getting Help Introduction to Matlab for Engineers Ohlone, Fremont 10/23
Operations Between Two Scalars Introduction to Matlab for Engineers Ohlone, Fremont 11/23
Special Characters, Separates commands ; Hides the output of a command % Creates a comment statement pi π eps The smallest number understood by Matlab inf Infinity i or j i = 1 NaN Not a number Introduction to Matlab for Engineers Ohlone, Fremont 12/23
Elementary Functions Functions use the format function-name(arguments) sqrt(x) Square root sin(x) Sine in radians sind(x) Sine in degrees atan(x) Arctangent log(x) Natural logarithm (ln x) log10(x) Common logarithm (log 10 x) log2(x) base 2 logarithm (log 2 x) who A list of variables whos A list of variables and sizes why For fun ans The result of last command clear Clear the workspace window clc Clears the command window Introduction to Matlab for Engineers Ohlone, Fremont 13/23
Elementary Functions Introduction to Matlab for Engineers Ohlone, Fremont 14/23
Elementary Functions Introduction to Matlab for Engineers Ohlone, Fremont 15/23
Elementary Functions Introduction to Matlab for Engineers Ohlone, Fremont 16/23
Elementary Functions Introduction to Matlab for Engineers Ohlone, Fremont 17/23
Elementary Functions Introduction to Matlab for Engineers Ohlone, Fremont 18/23
Numeric Output format short format short e format long format rat format bank Four places after the decimal Scientific with 4 places after the decimal Fifteen places after the decimal Rational Dollars and cents To see the effects, create x=[1,pi,1/4,1/7], change the type using one of the format commands, and type x. Repeat for y=[1,2,3,4] Repeat for y+1. What is happening? Look up format in the Help files for more options Introduction to Matlab for Engineers Ohlone, Fremont 19/23
Numeric Output Table 2.2, Moore s textbook, page 41 Introduction to Matlab for Engineers Ohlone, Fremont 20/23
Table 2.2, Moore s textbook, page 41 Numeric Output Introduction to Matlab for Engineers Ohlone, Fremont 21/23
Scripts A script a text file that contains all of the commands to be entered in sequence. It is very important to know how to write and edit your code on a script! Editable Easy to fix mistakes Used for long work Easily saved and sent to others Two types: scripts and functions With scripts, variables are global With functions, variables are local Introduction to Matlab for Engineers Ohlone, Fremont 22/23
Scripts I will show you how to: Open a new script Save a script with a proper name Run a script Introduction to Matlab for Engineers Ohlone, Fremont 23/23