january 24, 2005 lecture 3 - by p. lin 1 cpet 190 lecture 3 problem solving with matlab lin

32
January 24, 2005 January 24, 2005 Lecture 3 - By P. Lin Lecture 3 - By P. Lin 1 CPET 190 CPET 190 Lecture 3 Lecture 3 Problem Solving with Problem Solving with MATLAB MATLAB http://www.etcs.ipfw.edu/~lin http://www.etcs.ipfw.edu/~lin

Upload: aron-foster

Post on 25-Dec-2015

220 views

Category:

Documents


3 download

TRANSCRIPT

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 11

CPET 190CPET 190

Lecture 3Lecture 3

Problem Solving with MATLABProblem Solving with MATLAB

http://www.etcs.ipfw.edu/~linhttp://www.etcs.ipfw.edu/~lin

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 22

Lecture 3: MATLAB Lecture 3: MATLAB Development EnvironmentDevelopment Environment

3-1 Desktop Tools3-1 Desktop Tools• Starting and Quitting MATLABStarting and Quitting MATLAB• Command WindowCommand Window

Running FunctionsRunning Functions Entering VariablesEntering Variables Controlling Input/OutputControlling Input/Output Searching ItemsSearching Items Running ProgramsRunning Programs Preferences for the Command Preferences for the Command

WindowWindow

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 33

Lecture 3: MATLAB Lecture 3: MATLAB Development EnvironmentDevelopment Environment (continue)(continue)

3-1 Desktop Tools (continue)3-1 Desktop Tools (continue)• Command HistoryCommand History• The MATLAB WorkspaceThe MATLAB Workspace• Help BrowserHelp Browser• Workspace BrowserWorkspace Browser• Current Directory BrowserCurrent Directory Browser• Edit/DebugEdit/Debug

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 44

Lecture 3: MATLAB Lecture 3: MATLAB Development EnvironmentDevelopment Environment (continue)(continue)

3-2 Useful Commands3-2 Useful Commands• The MATLAB Search Path The MATLAB Search Path • Number Display FormatsNumber Display Formats• Session LogSession Log• MATLAB Managing Commands and FunctionsMATLAB Managing Commands and Functions

demo - Run demosdemo - Run demos helphelp - Online help- Online help ver - Current MATLAB and toolbox versionsver - Current MATLAB and toolbox versions version - Current MATLAB version numberversion - Current MATLAB version number pathpath - MATLAB Search path- MATLAB Search path diarydiary - Save typed commands on a - Save typed commands on a

named filenamed file SummarySummary

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 55

Starting and Quitting MATLABStarting and Quitting MATLAB

Starting MATLABStarting MATLAB From command window: start -> Run -> MATLABFrom command window: start -> Run -> MATLAB Start -> All Programs -> MATLABStart -> All Programs -> MATLAB

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 66

Starting and Quitting MATLABStarting and Quitting MATLAB

Quitting MATLABQuitting MATLAB At the MATLAB At the MATLAB

command window: command window:

>> quit >> quit

>> exit>> exit Close MATLAB Close MATLAB

Window Window

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 77

Desktop ToolsDesktop Tools

Started MATLAB and see the three sub-windows:Started MATLAB and see the three sub-windows: Command WindowCommand Window Current DirectoryCurrent Directory Command HistoryCommand HistoryAt the commandAt the commandwindow, we enterwindow, we enter>>help help>>help help

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 88

Command WindowCommand Window

Running FunctionsRunning Functions Entering VariablesEntering Variables Controlling Input/OutputControlling Input/Output Searching ItemsSearching Items Running ProgramsRunning Programs Setting Preferences for the Command Setting Preferences for the Command

WindowWindow

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 99

Running CommandsRunning Commands

An Example:An Example:

>> magic(3)>> magic(3)

ans =ans =

8 1 68 1 6

3 5 73 5 7

4 9 24 9 2

>> help magic>> help magic

MAGIC Magic square.MAGIC Magic square. MAGIC(N) is an N-by-N MAGIC(N) is an N-by-N matrix constructed from the matrix constructed from the integersintegers 1 through N^2 with equal 1 through N^2 with equal row, column, and diagonal row, column, and diagonal sums.sums. Produces valid magic Produces valid magic squares for all N > 0 except squares for all N > 0 except N = 2.N = 2.

15

15

15

15 1515 15 15

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 1010

Entering VariablesEntering Variables

>> x = 10>> x = 10x = 10x = 10>> y = 10>> y = 10y = 10y = 10>> x * y>> x * yans = 100ans = 100>> x / y>> x / yans = 1ans = 1>> x + y>> x + yans = 20ans = 20>> x - y>> x - yans = 0ans = 0>>>>

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 1111

Controlling Input/OutputControlling Input/Output

Format Function Format Function – control the – control the display formats display formats for numeric for numeric valuesvalues

ExamplesExamples>> >> x = 1/3x = 1/3 x = 0.3333x = 0.3333>> >> y = 1.2345e-7y = 1.2345e-7 y = 1.2345e-7y = 1.2345e-7>> >> format short eformat short e>> >> xxx = 3.3333e-001x = 3.3333e-001>> >> yyy = 1.2345e-007y = 1.2345e-007

Hit Enter keyHit Enter key

MATLAB DisplaysMATLAB Displays

Hit Enter keyHit Enter key

MATLAB DisplaysMATLAB Displays

Hit Enter keyHit Enter key

Enter xEnter x

Enter yEnter yMATLAB EchoesMATLAB Echoes

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 1212

Controlling Input/Output Controlling Input/Output (continue)(continue)

Format Function:Format Function:>> help format>> help format

FORMAT Set output format.FORMAT Set output format. All computations in MATLAB are done in All computations in MATLAB are done in

double precision.double precision. FORMAT may be used to switch between FORMAT may be used to switch between

different outputdifferent output display formats as follows:display formats as follows: FORMAT FORMAT Default. Same as SHORT. Default. Same as SHORT. FORMAT SHORTFORMAT SHORT Scaled fixed point Scaled fixed point

format with 5 digits.format with 5 digits. FORMAT LONGFORMAT LONG Scaled fixed point Scaled fixed point

format with 15 digits.format with 15 digits. FORMAT SHORT EFORMAT SHORT E Floating point Floating point

format with 5 digits.format with 5 digits. FORMAT LONG EFORMAT LONG E Floating point format Floating point format

with 15 digitswith 15 digits..

>> format long>> xx = 0.33333333333333>> yy = 1.234500000000000e-007

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 1313

Controlling Input/Output Controlling Input/Output (continue)(continue)

Format Function:Format Function:>> help format>> help format

… … FORMAT SHORT GFORMAT SHORT G Best of fixed or Best of fixed or floating point format with 5 digits.floating point format with 5 digits.

FORMAT LONG GFORMAT LONG G Best of fixed or Best of fixed or floating point format with 15 digits.floating point format with 15 digits.

FORMAT HEXFORMAT HEX Hexadecimal format. Hexadecimal format. FORMAT +FORMAT + The symbols +, - and The symbols +, - and

blank are printed for positive, negative blank are printed for positive, negative and zero elements. Imaginary parts are and zero elements. Imaginary parts are ignored.ignored.

FORMAT BANKFORMAT BANK Fixed format for Fixed format for dollars and cents.dollars and cents.

FORMAT RATFORMAT RAT Approximation by ratio Approximation by ratio of small integers. of small integers.

>> format long g>> xx = 0.333333333333333>> yy = 1.2345e-007>> format bank>> xx = 0.33>> yy = 0.00>> format rat>> xx = 1/3 >> yy = 1/8100446

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 1414

Controlling Input/Output Controlling Input/Output (continue)(continue)

File -> PreferencesFile -> Preferences

•Allows you to set Allows you to set up various up various numeric display numeric display formats through formats through GUIGUI

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 1515

Suppress Output Suppress Output

Suppressing OutputSuppressing Output• End of the statement with a semicolonEnd of the statement with a semicolon• Increase statement and program execution Increase statement and program execution

speed speed An ExampleAn Example>> x = 10;>> x = 10;>> y = 10;>> y = 10;>> product = x * y>> product = x * yProduct = 100 Product = 100

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 1616

Entering long StatementsEntering long Statements

If a statement does not fit on a line (too If a statement does not fit on a line (too long), uses an ellipsis (three periods), long), uses an ellipsis (three periods), follow by Enter or Return, to indicate the follow by Enter or Return, to indicate the continuation of the statement to the next continuation of the statement to the next lineline

For an exampleFor an example>> x = 1 + 2 + 3 + 5 + 6 + 7 + ...>> x = 1 + 2 + 3 + 5 + 6 + 7 + ...8 + 9 + 108 + 9 + 10x =x = 5151

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 1717

Command Line Editing and Command Line Editing and Command HistoryCommand History

Command Line Editing Command Line Editing Keys such as right arrow →, down arrow ↓, Keys such as right arrow →, down arrow ↓, up arrow ↑, left arrow ←, etc, can be used up arrow ↑, left arrow ←, etc, can be used to edit commandsto edit commands

Command HistoryCommand History• The entered commands at the Command The entered commands at the Command

Windows are recorded or logged in the Windows are recorded or logged in the Command HistoryCommand History

• We can view previous commands or statementsWe can view previous commands or statements• We can copy or execute rerun previous We can copy or execute rerun previous

statementsstatements

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 1818

The MATLAB WorkspaceThe MATLAB Workspace

who functionwho function whos functionwhos function clear functionclear function

>> whoYour variables are:x y

>> whosName Size Bytes Class x 1x1 8 double array y 1x1 8 double array

Grand total is 2 elements using 16 bytes>> clear x>> whoYour variables are:y

>> clear>> whos

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 1919

The MATLAB WorkspaceThe MATLAB Workspace

save functionsave function load functionload function

>> save 8_20_2004.mat>> clear>> whos>> load 8_20_2004.mat>> whosName Size Bytes Class

ans 1x1 8 double array x 1x1 8 double array y 1x1 8 double array

Grand total is 3 elements using 24 bytes

>> clear>> whos

Current Directory

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 2020

Save CommandSave Command

We can use “help save” to see documentation of We can use “help save” to see documentation of SAVE function and learn how to Save workspace SAVE function and learn how to Save workspace variables to disk. variables to disk. • SAVE FILENAME saves all workspace SAVE FILENAME saves all workspace

variables to the binary "MAT-file“, If variables to the binary "MAT-file“, If FILENAME has no extension, .mat is assumed. FILENAME has no extension, .mat is assumed.

• SAVE, by itself, creates the binary "MAT-file" SAVE, by itself, creates the binary "MAT-file" named 'matlab.mat'. It is an error if named 'matlab.mat'. It is an error if 'matlab.mat' is not writable.'matlab.mat' is not writable.

• SAVE FILENAME X saves only X.SAVE FILENAME X saves only X.• SAVE FILENAME X Y Z saves X, Y, and Z. The SAVE FILENAME X Y Z saves X, Y, and Z. The

wildcard '*' can be used to save only those wildcard '*' can be used to save only those variables that match a pattern.variables that match a pattern.

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 2121

Save Command Save Command (continue)(continue)

MATLAB also offers various saving options: MATLAB also offers various saving options: ASCII Options:ASCII Options:

• SAVE ... -ASCII uses 8-digit ASCII form SAVE ... -ASCII uses 8-digit ASCII form instead of binary regardless of file extension.instead of binary regardless of file extension.

• SAVE ... -ASCII -DOUBLE uses 16-digit ASCII SAVE ... -ASCII -DOUBLE uses 16-digit ASCII form.form.

• SAVE ... -ASCII -TABS delimits with tabs.SAVE ... -ASCII -TABS delimits with tabs. And more will be discussed laterAnd more will be discussed later

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 2222

Help BrowserHelp Browser

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 2323

Workspace BrowserWorkspace Browser

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 2424

Current Directory BrowserCurrent Directory Browser

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 2525

The Edit/DebugThe Edit/Debug Use the Editor/Debugger to create and debug M-files, Use the Editor/Debugger to create and debug M-files,

which are programs you write to run MATLAB functions. which are programs you write to run MATLAB functions. The Editor/Debugger provides a graphical user interface The Editor/Debugger provides a graphical user interface

for basic text editing, as well as for M-file debugging.for basic text editing, as well as for M-file debugging.

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 2626

The Edit/Debug The Edit/Debug (continue)(continue)

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 2727

The Edit/Debug The Edit/Debug (continue)(continue)

Create a mlab_exs directory

Save the M-file as sine60hz.m

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 2828

The Edit/Debug The Edit/Debug (continue)(continue)

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 2929

Testing/Debugging sin60hz.mTesting/Debugging sin60hz.m

Click -> Debug -> Run

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 3030

Other CommandsOther Commands

Other Useful MATLAB Management Other Useful MATLAB Management Commands/FunctionsCommands/Functions

demo - Run demosdemo - Run demos helphelp - Online help- Online help verver - Current MATLAB and - Current MATLAB and

toolbox versionstoolbox versions version - Current MATLAB version version - Current MATLAB version

numbernumber pathpath - MATLAB Search path- MATLAB Search path diary - Save typed commands on a diary - Save typed commands on a

named filenamed file

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 3131

SummarySummary Desktop ToolsDesktop Tools Starting and Quitting MATLABStarting and Quitting MATLAB Command WindowCommand Window

• Running FunctionsRunning Functions• Entering VariablesEntering Variables• Controlling Input/OutputControlling Input/Output• Searching ItemsSearching Items• Running ProgramsRunning Programs• Preferences for the Command WindowPreferences for the Command Window

Command HistoryCommand History The MATLAB WorkspaceThe MATLAB Workspace Help BrowserHelp Browser Workspace BrowserWorkspace Browser Current Directory BrowserCurrent Directory Browser Edit/Debug - a sine wave programEdit/Debug - a sine wave program

January 24, 2005January 24, 2005 Lecture 3 - By P. LinLecture 3 - By P. Lin 3232

Question?Question?

AnswersAnswers

Email: [email protected]: [email protected]