Lecture 3 Matlab Introduction - PowerPoint PPT Presentation

1 / 37
About This Presentation
Title:

Lecture 3 Matlab Introduction

Description:

Operational Precedence. Operational precedence means the hierarchy of the operators: ... So for Operational precedence. y = 3 8-3 | sqrt(15) = 4 ... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 38
Provided by: San59
Category:

less

Transcript and Presenter's Notes

Title: Lecture 3 Matlab Introduction


1
Lecture 3 - Matlab Introduction
  • CVEN 302
  • August 31, 2001

2
Lecture Goals
  • Data Files
  • Matlab files
  • ASCII
  • Matlab files
  • Matlab controls

3
Matlab commands for file management
  • The files can be written as a script, which
    can be loaded into the memory. From the command
    line
  • echo - causes the file to be echoed to the
    screen.
  • what - shows the type of file in the current
    directory.
  • type - will present show the file contents

4
Matlab Files
  • There are three types of files
  • Data files
  • Matlab files have their own format for saving
    data
  • ascii files are standard text files, which can
    be printed out or used in excel files.
  • m-files represent the program files.
  • function files are functions similar to sin(x),
    cos(x), etc.

5
Data Files
  • Data files can be written in two forms
  • Matlab format
  • ascii format

6
Matlab data format
  • MatLab generates a data
  • file, which will have
  • look like
  • filename.mat
  • File can be loaded into
  • the memory with
  • load filename
  • t linspace(0,2pi,40)
  • x cos(t)
  • save data1 t x
  • clear
  • whos
  • what
  • data1.mat
  • load data1
  • whos

7
ACSII format
  • An ascii file type can be
  • created by adding a flag
  • on the end of the save
  • command
  • save filename.dat -ascii
  • t linspace(0,2pi,30)
  • x sin(t)
  • save data2.dat t x -ascii
  • dir
  • data2.dat
  • clear
  • load data2.dat -ascii

8
Loading Data Files
  • Matlab Files
  • load filename
  • The file will load the file into
  • the same format as it was
  • saved.
  • ASCII Files
  • load filename.dat -ascii
  • The file will be loaded as an
  • data array and will require
  • you to modify to obtained the
  • data vectors.

9
Data file example
  • tlinspace(0,2pi,20)
  • y1 sin(x)
  • save data1 t y1
  • clear
  • (created data1.mat and
  • will show up in home
  • directory.)
  • load data1
  • (data1 will have created
  • t and y1 vectors.)
  • save data2.dat t y1 -ascii
  • clear
  • (created a ascii file with
  • the data)
  • load data2.dat -ascii

10
Data file example continued
  • (loaded an ascii file into
  • memory as data2 array)
  • whos
  • data2 2X100 double array
  • t data2(1,)
  • y1 data2(2,)
  • assigns the row to the vector.
  • NOTE that the ascii
  • file does not separate the
  • vectors.

11
M-files
  • These files load constants lists and executable
  • programs into memory. For example
  • dir
  • myCon.m
  • type myCon
  • myCon or load myCon

12
Function M-files
  • These files are similar to subroutines and will
  • act as built-in functions, i.e. a tool box
    similar
  • to functions, sin(x), log(x)
  • Open a new m-file - Go to the top of Matlab and
    open a m-file.

13
Function files
  • Function name(input variables)
  • function statements
  • There are set of example files show be in the
    working directory.
  • twosum(x,y)
  • addmult(x,y)

14
Function m-files
  • The functions act similar to any of the built-in
    functions, sin(x), ln(x), etc. The inputs and
    outputs of the functions can be a variable,
    vector and/or a matrix.

15
Input functions in m-files
  • Input function allows a program to input data
    into memory. The function is defined as
  • variable input( string request )
  • Example inputAbuse.m file

16
Output functions in m-files
  • Output function allows a program to output data
    to screen. The function is defined as
  • fprintf( g\n,variable)
  • g is a definition of type of variable
  • \n causes a carriage return.

17
Flow controls
  • There are three types of flow control devices.
  • Relational Operations
  • Arithmetic Operations
  • Logistic Operations

18
Relational Operations
  • Relational operations are
  • used for comparison of
  • different values and
  • generate a true/false
  • result.
  • lt less than
  • lt less than or equal
  • gt greater than
  • gt greater than or
  • equal
  • equal to
  • not equal

19
Logistic Operations
  • Logistic operations are
  • used to combine relational
  • operations to generate a
  • true/false result.
  • and
  • or
  • not

20
Relational and Logistic Operations
  • Try the follow set of examples type out the
    logistic
  • operations. The starts a comment until you
    hit a
  • return. Example
  • a2 b4
  • Relational Operations
  • aisSmaller altb results in aisSmaller 1
    (true)
  • bisSmaller blta results in bisSmaller 0
    (false)
  • Logistic Operations
  • bothTrue aisSmaller bisSmaller bothTrue 0
    (false)
  • eithTrue aisSmaller bisSmaller eithTrue 1
    (true)
  • eithTrue 0 (false)

21
Operational Precedence
  • Operational precedence means the hierarchy of the
    operators
  • 1 Arithmetic Operations
  • 2 Relationship Operations
  • 3 Logical Operations

22
Operational Precedence example
  • Arithmetic Operations
  • x 2 32/4 is
  • x 2 ( 32 / 4 ) not x 2 3(2/4)
  • So for Operational precedence
  • y 3 gt 8-3 sqrt(15) gt 4
  • y 3 gt 5 3.873 gt 4 arithmetic operations
  • y 0 0 relational operations
  • y 0 logistic

23
Program flow controls
  • These commands control flow of the program.
  • if else end statements
  • switch structure
  • for structure
  • while statements

24
if flow control
  • The if command can be
  • used to control the flow
  • of the program. There
  • are three basic forms of
  • the if controls.
  • if (relation expression)
  • block of statements
  • end
  • Look at the m-file see
  • what it does.
  • example cond1.m

25
if flow control
  • The if command can
  • be used with an else
  • command.
  • if (relation expression)
  • block of statements
  • else(relation expression)
  • block of statements
  • end
  • example cond2.m and
  • cond3.m

26
switch flow control
  • switch(relation expression)
  • case 1(value)
  • block of statements
  • case 2(value)
  • block of statements
  • case 3 (value)
  • block of statements
  • otherwise
  • end
  • example cond4.m
  • The switch command
  • can be used for multiple
  • case of the if command.

27
for flow control
  • for(index )
  • block of statements
  • end
  • Type out the commands and
  • see what it does.
  • example
  • x4,5,6,7 sumx0
  • for k 1length(x)
  • sumx sumx x(k)
  • end
  • The for command is
  • used to do a series of
  • steps of redundant
  • process.

28
for flow control
  • example
  • (counter) startstepfinal
  • value size value
  • Try n10 find out what the
  • counter do.
  • k 12n (1,3,5,n)
  • m 0pi/15pi (0,pi/15,,pi)
  • k n -11 (n,n-1,,1)
  • The for command can
  • have various forms of
  • step sizes.

29
while flow control
  • while(expression)
  • block statements
  • end
  • Try running the example in.
  • Example
  • x4
  • while (x-1)gt 0.01
  • x sqrt(x)
  • disp(x)
  • end
  • The while command
  • can do redundant process
  • by checking on a logical
  • or relational operation or
  • combination of the two.

30
Escape commands
  • break is an escape from a loop, which will go
    the end of the loop and start the next step in
    the program.
  • return is an escape from a program or
    subroutine or a function and return to the main
    program.

31
Misc. Functions
  • disp(expression) - displaces the expression to
    the screen.
  • format (long,e,short, ...) determines the format
    of numerical values.
  • global variable makes a variable available in
    any function or program as shared memory.

32
Input
  • Function definition
  • Summary is what the program does.
  • Synopsis run program
  • Input definition of terms
  • Output what type of output
  • Notes

33
Debugging
  • Searching for and removing bugs is an inevitable
    part of programming. Bugs are caused by a number
    of factors from outright programming blunders to
    the improper application of numerical method.

34
Debugging Tools
  • type and dbtype commands
  • type prints out the file error
  • dbtype will print out the file with line
    numbers. dbtype startstop will print out
    lines from start to stop.
  • Error function - error(message) will print out
    the message and halt execution.

35
Debugging Tools
  • pause command temporarily suspends the
    execution of the m-file. Useful way to print a
    warning message without necessarily aborting an
    analysis.
  • keyboard command keyboard command suspends
    execution of an m-file and gives control to user.
    It gives the user access to internal variables
    of the function containing command.

36
Debug example
  • The example program
  • keyboard stops a program to check internal
  • variables. It is a powerful tool.
  • Kgtgt is the prompt and you can use it to check
    numbers out.
  • Kgtgt return will return to the executable
    program.

37
Homework
  • Check the Homework webpage.
  • You will be writing a simple program to generate
    a set of points and generate a plot.
Write a Comment
User Comments (0)
About PowerShow.com