Matlab - PowerPoint PPT Presentation

1 / 48
About This Presentation
Title:

Matlab

Description:

... beginning with the 'leading' coefficient (coefficient of highest power) ... MAT files binary file, used to 'save' workspace variables, extension .mat pi. ans ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 49
Provided by: martinh55
Category:
Tags: mat | matlab | power

less

Transcript and Presenter's Notes

Title: Matlab


1
Matlab
2
Textbooks
  • Required Introduction to MATLAB 7 for Engineers,
    by Palm.

3
Purchasing MATLAB Software
  • Mathworks web site (http//www.mathworks.com).
  • UH Bookstorebe sure to get version 2007
  • UH-IT Department

4
Purchasing MATLAB Software
  • Mathworks web site (http//www.mathworks.com/prod
    ucts/matlab/). The cost is 99, plus shipping.
  • IT Department ? (student) 94
  • http//www.uh.edu/infotech/php/template.php?softwa
    re_id30

5
Learning to use MATLAB Software
  • It is strongly recommended that you perform the
    labs for the MATLAB software.
  • These are Labs 7 through 12. They are available
    from the course web site.
  • The schedule is also available on the website.
    Try not to fall behind.

6
About MATLAB
MATLAB is
  • a computer programming language
  • a software environment for using the language
    effectively

7
Two modes of operation
About MATLAB
  • Interactive calculator mode
  • Script mode execution of complete programs ?
    script files

8
About MATLAB
  • Running MATLAB creates one or more windows
  • One is called the MATLAB Desktop Primary
    graphical user interface for MATLAB
  • Contains and manages all other windows that are
    part of MATLAB
  • According to the configuration some windows may
    be visible and some may not. You can see this
    information in the View Menu

9
About MATLAB Desktop
10
About MATLAB Desktop
  • MATLAB Desktop Windows
  • Command Window Primary place to interact with
    MATLAB. Place where the user issues the commands.
    The MATLAB command prompt is the symbol gtgt
  • Command History Running history of prior
    commands issued in the Command Window.

11
About MATLAB Desktop
  • MATLAB Desktop Windows
  • Workspace GUI for viewing, loading, and saving
    MATLAB variables.
  • Launch Pad Tree layout for access to tools,
    demos and documentation
  • Current Directory GUI for directory and file
    manipulation in MATLAB.

12
About MATLAB Desktop
  • MATLAB Desktop Windows
  • Help GUI for finding and viewing on-line
    documentation.
  • Array Editor GUI for modifying the content of
    MATLAB variables
  • Editor/Debugger Text editor and debugger for
    MATLAB text files.

13
What does Matlab understand?
  • Real numbers 1 -14 7.65432 2e3
  • Complex numbers 23i 4-6j 2i
  • Arrays of numbers 1,2,3 1,23,4
  • Strings I wish I knew that!

14
MATLAB Take 1 Basics
15
Basics Basic Operations
16
Basics Rules of Precedence
17
Basics Rules of Precedence
  • precedence examples
  • 8 3 5
  • (8 (3 5)) 23
  • 4 2 12 8 / 4 2
  • ((4 2) 12 ((8 / 4) 2))) 0
  • 3 4 2 5
  • ((3 (4 2)) 5) 53
  • 27 1 / 3 32 0.2
  • (((27 1) / 3 ) (32 0.2))11

18
Basics Variables
  • Variable is a symbol used to contain a value
  • MATLAB has some rules to name variables
  • Variables names are case sensitive
  • BOB, Bob, bob, BoB ? all different!
  • Variables names can contain up to 31 characters.
    Any additional characters are ignored
  • Variable names must start with a letter, followed
    by any number of letters, digits or underscores.
    Punctuation characters are not allowed, because
    most of them have a special meaning in MATLAB

19
Basics Variables
  • Some names cannot be used for variables for,
    end, if, while, function, return, elseif, case,
    otherwise, switch, continue, else, try, catch,
    global, persistent, break
  • If you try to use any of these reserved words as
    a variable, MATLAB will report an error
  • Of course, you can use words similar by
    capitalizing one or more letters
  • Also, MATLAB has a number of special variables
    and constants

20
Basics Special Variables and Constants
21
Basics
  • Assignment or replacement operator
  • It works different than the equal sign the
    variable on the left-hand side is replaced by the
    value generated by the right-hand side.
    Therefore, only one variable name must be in the
    left-hand side
  • x x 2
  • 6 x ? NOT VALID!!!
  • The right-hand side must have a computable value
  • x 5 y Is y defined?

22
MATLAB Take 2 Programming with MATLAB
23
The Problem Solving Process - 5 Steps
  • 1. Understand the problem - diagram or sketch is
    good.
  • 2. Collect information - data, theories,
    assumptions, approximations.
  • 3. Generate an algorithm, or maybe more than one.
  • 4. Refine and implement - use a tool
    spreadsheet, MATLAB, or other computer
    program/language.
  • 5. Verify and test - estimate, work another way,
    try other data input, compare with a plot, check
    dimensional consistency.

24
Programming
  • Example Volume of a circular cylinder
  • Given a circular cylinder with height 15m and
    radius 8 m, find the radius of a cylinder of the
    same height but with a volume that is 20 larger.

25
Solution Algorithm
  • Calculate current volume
  • Calculate desired volume
  • Determine new radius by rewriting volume equation
    in terms of radius

26
Basics
  • Example Volume of a circular cylinder
  • V ? r2 h
  • h 15m
  • r 8m
  • Find the radius of a cylinder with volume 20
    greater, and the same height
  • pie 3.1416
  • r 8
  • h 15
  • V pier2h
  • V 1.2V
  • r sqrt(V/(pieh))

27
Alternative Approach
  • The formula for volume is V?r2h
  • The radius is given by

gtgt r 8 gtgt h 15 gtgt V pir2h gtgt newV
1.2V gtgt newr sqrt(newV/(pih)) ans 8.7636
28
Basics Commands
29
Basics Complex Numbers
  • complex number example
  • s 3 7j
  • w 5 9j
  • MATLAB Example
  • Given x -5 9j and y 6 2j
  • find
  • x y
  • 17j
  • xy
  • -12 64j
  • x/y
  • -1.2 1.1j

30
Some Simple Functions
  • sin(x)
  • sqrt(x)
  • acos(x)
  • exp(x)
  • log(x)

31
Interpretation
  • sin(180) ?
  • -0.8012
  • The argument is in radians.
  • acos(.5) ?
  • 1.0472
  • The solution is in radians.

32
Interpretation
  • exp(1) ?
  • 2.7183
  • This is e.
  • log(10) ?
  • 2.3026
  • This is the natural log, not base 10.

33
Polynomials in Matlab
  • A polynomial is represented by its array of
    coefficients, beginning with the leading
    coefficient (coefficient of highest power)

34
Special Polynomial Functions
  • polyval(a,x) evaluates polynomial whose
    coefficients are in array a at the point x
  • roots(a) returns a vector containing all of the
    roots of the polynomial whose coefficients are in
    the array a
  • poly(v) returns the coefficient vector of a
    polynomial whose roots are stored in the vector v

35
Polynomial Examples
  • gtgt a2,4,-1,5
  • gtgt polyval(a,0)
  • ans
  • 5
  • gtgt poly(1, 2, 3)
  • ans
  • 1 -6 11 -6
  • gtgt vroots(a)
  • v
  • -2.5722
  • 0.2861 0.9434j
  • 0.2861 - 0.9434j

36
Plotting in Matlab
  • The basic command to plot a set of points in the
    plane is plot(x,y)
  • x is a vector containing the horizontal
    coordinates
  • y is a vector containing the vertical coordinates
  • x y must be the same sizeeither both row
    vectors or both column vectors
  • The plot actually appears in a separate window,
    called a figure window.

37
Plotting Example
  • gtgt x00.0110 generate x values
  • gtgt ysin(x) generate y values
  • sine function
  • applied to array of xs
  • gtgt figure(1) give number to figure
  • gtgt plot(x,y),title('My first Plot'),
  • xlabel('x axis'),
  • ylabel('y axis')

38
(No Transcript)
39
Plotting in MATLAB
40
Plotting in MATLAB
  • Some Plot Options

41
Matlab Files
  • Script function filesplain ASCII, extension .m
  • MAT filesbinary file, used to save workspace
    variables, extension .mat

42
(No Transcript)
43
gtgt pi ans 3.1416 gtgt format long gtgt pi ans
3.14159265358979 gtgt format rat gtgt pi ans
355/113
44
Controlling Input and Output
  • The disp Function Useful to control the output
  • disp Display array.
  • disp(X) displays the array, without printing the
    array name. It's the same as leaving the
    semicolon off an expression except that empty
    arrays don't display. If X is a string, the text
    is displayed.
  • EXAMPLES
  • gtgt disp(X) displays the value of variable X, but
    not its name
  • gtgt disp(The value of X is) displays the
    string The value of X isbut no number

45
Controlling Input and Output
  • The num2str Function Useful to make the output
    look better
  • num2str Convert number to string.
  • Tnum2str(X) converts the array X into a
    string representation T with about 4 digits and
    an exponent if required. Allows display of label
    on same line as value.
  • gtgt x3.24567
  • gtgt disp('The value of x is ',num2str(x),'ft')
  • The value of x is 3.2457ft

46
Controlling Input and Output
  • The input Function Used to get interactive
    input.
  • input Prompt for user input.
  • R input('How many apples') gives the user
    the prompt in the text string and waits for input
    from the keyboard. The input can be any MATLAB
    expression, which is evaluated, using the
    variables in the current workspace, and the
    result returned in R. If the user presses the
    return key without entering anything, INPUT
    returns an empty matrix.
  • R input('What is your name','s') gives the
    prompt in the text string and waits for character
    string input. The typed input is not evaluated
    the characters are simply returned as a MATLAB
    string.

47
Controlling Input and Output
  • The Menu Function Used to get interactive input
    by selection of an alternative from a menu.
  • k menu(title,option1,option2,)
  • choices'B','D','F'
  • kmenu('Whodoyouwant?',choices(1),choices(2),choic
    es(3))
  • disp('You picked ',choices(k))

48
Input from File
  • load as a command or as a function, requires
    each line in file has same number of values,
    loads entire contents of file into an array
  • gtgtload data.txt name of array will be data
  • gtgtload(new.txt) name of array will be new
  • gtgtmydata load(proj3.txt) name of array
    will be mydata
Write a Comment
User Comments (0)
About PowerShow.com