Title: Engineering Applications
1Engineering Applications
- Dr. Darrin Leleux
- Lecture 2 MATLAB Introduction
- Chapter 1
2Introduction
- Class MATLAB Experience
- What is MATLAB
- The MATLAB System
- MATLAB Documentation
3Typical Class MATLAB Experience
4What is MATLAB?
- MATLAB is an acronym for Matrix Laboratory
- High-Performance Language for Technical Computing
- Typical uses Include
- Mathematics and computation
- Algorithm development
- Modeling, simulation, and prototyping
- Data analysis, exploration, and visualization
- Scientific and engineering graphics
- Application development, including graphical user
interface (GUI) building
5The MATLAB System
- Development Environment
- The MATLAB Mathematical Function Library
- The MATLAB Language
- Handle Graphics
- The MATLAB Application Program Interface (API)
- Toolboxes
- MATLAB Central
- http//www.mathworks.com/matlabcentral/
6MATLAB Development Environment
- A set of tools and facilities that help use
MATLAB functions and files - MATLAB desktop
- Command Window
- Command History
- Browsers for viewing Help
- The Workspace
- Files (M-files, etc.)
- Search Path
7MATLAB Desktop
View or Change Current Directory
Enter MATLAB functions
Expand to view Documentation, demos, tools
Use tabs to go to Workspace browser
View or use previously run functions
8MATLAB Structure
9Example 1.1
maximum number of elements allowed in a matrix on
this version of MATLAB. My version 2.1475e009
Windows
10Command Window
- Enter variables and run functions and M-files in
interactive mode - HINT use up-arrow to retrieve previously
typed commands quickly
11Command History
- Lines entered in the Command Window are logged in
this display - You can view, copy and execute previously used
functions - Right Click on selection
- Copy
- Evaluate Selection
- Create M-file
12Workspace Browser
- Consists of the set of variables built up during
a MATLAB session - These can be saved off as a .mat file and loaded
into MATLAB with the load command - Save off variables using save command
13Array Editor
- Double-click on a variable in the Workspace
browser to see it in the Array editor
14Launch Pad
- Provides easy access to tools, demos, and
documentation - Can also get to these via the Start menu
15Help Browser
- Search and view documentation
- It is also a Web Browser that displays HTML
- Consists of 2 panes, the Help Navigator (used to
find information), and the display pane (for
viewing information)
16Current Directory Browser
- View or change current directory
- Search for, view, open, or make changes to
MATLAB-related folders and files.
17Search Path
- To determine how to execute functions
- Any file to run must reside in the search path
- To change the search path, select Set Path from
the File menu on the desktop
18Editor/Debugger
- Create and debug M-files
- Provides a GUI for basic editing, as well as for
M-file debugging
19MATLAB Mathematical Function Library
- Vast collection of computational algorithms
- Elementary functions, such as
- Sum
- Sine
- Cosine
- Complex arithmetic
- Advanced functions, such as
- Matrix inverse
- Matrix Eigenvalues
- Bessel functions
- Fast Fourier transforms
20MATLAB Language
- High-level matrix/array language
- Control flow statements
- Functions
- Data structures
- Input/output
- Object-oriented programming features
21Handle Graphics
- High-level commands
- 2-D 3-D data visualization
- Image processing
- Animation
- Presentation graphics
- Low-level graphics customization
- GUI building in MATLAB applications
22The MATLAB Application Program Interface (API)
- Library that allows users to write C and Fortran
programs that interact with MATLAB - Dynamic linking
- Calling MATLAB as a computational engine
- Reading/writing MAT-files
23MATLAB General Commands
- helpbrowser - Bring up the help browser.
- doc - On-line help, displayed in the
help browser. - help - M-file help, displayed at the
command line. - helpwin - M-file help, displayed in the
help browser. - lookfor - Search all M-files for keyword.
- syntax - Help on MATLAB command syntax.
- support - Open MathWorks Technical Support
Web - demo - Run demonstrations.
- ver - MATLAB, SIMULINK, and toolbox
info. - version - MATLAB version information.
- whatsnew - Access Release Notes.
24Example 1.2
25Managing the Workspace
- who - List current variables.
- whos - List current variables, long form.
- workspace - Display Workspace Browser, a GUI for
managing - the workspace.
- clear - Clear variables and functions from
memory. - All or just some of the variables
- pack - Consolidate workspace memory.
- load - Load workspace variables from
disk. - save - Save workspace variables to disk.
- quit - Quit MATLAB session.
- clc - Clear command window
26Example 1.3
MATLAB Script Example 1.3 gtgt Use of Strings
and eval gtgtf1exp(x) Define a string
function f1 exp(x) gtgtx1 2 3 x
27Managing commands and functions
- what - List MATLAB-specific files in
directory. - dir - List all files in current directory
- type - List M-file.
- edit - Edit M-file.
- open - Open files by extension. Does
different tasks - depending on extension type.
- which - Locate functions and files by path
- inmem - List functions in memory.
- mex - Compile MEX-function.
28Whats In An M-File
29Differences between Script M-files and Functions
30Block Comments
31Example 1.4
32Controlling the command window
- echo - Echo commands in M-files.
- more - Control paged output in command
window. - diary - Save text of MATLAB session.
- format - Set output format.
- beep - Produce beep sound.
33Operating System Commands
- cd - Change current working directory.
- copyfile - Copy a file or directory.
- movefile - Move a file or directory.
- delete - Delete file.
- pwd - Show (print) current working
directory. - dir - List directory.
- fileattrib - Get or set attributes of files and
directories. - isdir - True if argument is a directory.
- mkdir - Make directory.
- rmdir - Remove directory.
- getenv - Get environment variable.
- ! - Execute operating system command
- dos - Execute DOS command and return
result.
34Operating System Commands(continued)
- unix - Execute UNIX command and return
result. - system - Execute system command and return
result. - perl - Execute Perl command and return
result. - web - Open Web browser on site or files.
- computer - Computer type.
- isunix - True for the UNIX version of
MATLAB. - ispc - True for the PC (Windows) version
of MATLAB.
35Example 1.5 from Text
- M-file (EX1_5.M) to evaluate the function
- y aexp(-1.2t) - 3.0exp(-2t)
- INPUT Coefficient a and time t
- OUTPUT y(t) displayed
- fprintf('Compute y aexp(-1.2t) - 3.0exp(-2t)
\n') - ainput('Coefficient a, a ')
- tinput('Variable t, t ')
-
- Display results
- fprintf('\n Function ygexp(-1.2g)-3.0exp(-2
g) \n',a,t,t) - yaexp(-1.2t) - 3.0exp(-2t)
- End M-file
36Output of Example 1.5
37Debugging M-files
- debug - List debugging commands.
- dbstop - Set breakpoint.
- dbclear - Remove breakpoint.
- dbcont - Continue execution.
- dbdown - Change local workspace
context. - dbstack - Display function call stack.
- dbstatus - List all breakpoints.
- dbstep - Execute one or more lines.
- dbtype - List M-file with line numbers.
- dbup - Change local workspace
context. - dbquit - Quit debug mode.
- dbmex - Debug MEX-files (UNIX only).
38Control Flow
- if - Conditionally execute statements.
- else - IF statement condition.
- elseif - IF statement condition.
- end - Terminate scope of FOR, WHILE,
SWITCH, TRY - and IF statements.
- for - Repeat statements a specific
number of times. - while - Repeat statements an indefinite
number of times. - break - Terminate execution of WHILE or
FOR loop. - continue - Pass control to the next iteration
of FOR or WHILE - loop.
- switch - Switch among several cases based
on expression. - case - SWITCH statement case.
- return - Return to invoking function.
39For loop syntax
- for variable expression
- statement
- ...
- statement
- end
- EXAMPLE
- a zeros(k,k) Preallocate matrix
- for m 1k
- for n 1k
- a(m,n) 1/(mn -1)
- end
- end
40If syntax
- if expression1
- statements1
- elseif expression2
- statements2
- else
- statements3
- end
41While loop syntax
42Switch syntax
43Special variables and constants
- ans - Most recent answer.
- eps - Floating point relative
accuracy. - realmax - Largest positive floating point
number. - realmin - Smallest positive floating point
number. - pi - 3.1415926535897....
- i, j - Imaginary unit.
- inf - Infinity.
- NaN - Not-a-Number.
- isnan - True for Not-a-Number.
- isinf - True for infinite elements.
- isfinite - True for finite elements.
- why - Succinct answer.