Eng. 8054 Advanced Marine Vehicles - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

Eng. 8054 Advanced Marine Vehicles

Description:

Eng. 8054 Advanced Marine Vehicles Todays agenda: Lab tomorrow at 2pm (structures lab) Advanced Marine Party Introduction to Matlab Eng. 8054 Advanced Marine ... – PowerPoint PPT presentation

Number of Views:102
Avg rating:3.0/5.0
Slides: 43
Provided by: Dave1396
Category:

less

Transcript and Presenter's Notes

Title: Eng. 8054 Advanced Marine Vehicles


1
Eng. 8054 Advanced Marine Vehicles
  • Todays agenda
  • Lab tomorrow at 2pm (structures lab)?
  • Advanced Marine Party
  • Introduction to Matlab

2
Eng. 8054 Advanced Marine Vehicles
  • Introduction to Matlab

3
What is Matlab?
Matlab is a commercial "Matrix Laboratory"
package which operates as an
interactive programming environment. Matlab is
available for PC's, Macintosh and UNIX
systems. Matlab is well adapted to numerical
experiments. Matlab program and script files
(m-files) always have filenames ending with ".m"
The programming language is
exceptionally straightforward since almost
every data object is assumed to be an
array. Graphical output (figure) is
available to supplement numerical results.
Online help is available from the Matlab prompt
(a double arrow) by typing help.
4
What kind of graphics are possible in Matlab?
Polar plot t0.012pi
polar(t,abs(sin(2t).cos(2t)))
Line plot x00.055,ysin(x.2),plot(x,y)
Stem plot x 00.14, y
sin(x.2).exp(-x) stem(x,y)
5
What kind of graphics is possible in Matlab?
Surface plot zpeaks(25), surf(z),
colormap(jet)
Mesh plot zpeaks(25), mesh(z)
Contour plot
zpeaks(25),contour(z,16)
Quiver plot
6
Using Help in Matlab
Online help is available from the Matlab prompt
(gtgt a double arrow), both generally (listing of
all available commands) gtgt help a long list
of help topics follows and for specific
commands gtgt help fft a help message on the
fft function follows.
7
What is Matlab?
  • MATLAB consists of
  • The MATLAB language
  • a high-level matrix/array language with control
    flow statements, functions, data structures,
    input/output, and object-oriented programming
    features.
  • The MATLAB working environment
  • the set of tools and facilities that you work
    with as the MATLAB user or programmer, including
    tools for developing, managing, debugging, and
    profiling
  • Handle Graphics
  • the MATLAB graphics system. It includes
    high-level commands for two-dimensional and
    three-dimensional data visualization, image
    processing, animation, and presentation graphics.
  • (contd)?

8
What is Matlab?
  • The MATLAB function library.
  • a vast collection of computational algorithms
    ranging from elementary functions like sum, sine,
    cosine, and complex arithmetic, to more
    sophisticated functions like matrix inverse,
    matrix eigenvalues, Bessel functions, and fast
    Fourier transforms as well as special image
    processing related functions
  • The MATLAB Application Program Interface (API)?
  • a library that allows you to write C and Fortran
    programs that interact with MATLAB. It include
    facilities for calling routines from MATLAB
    (dynamic linking), calling MATLAB as a
    computational engine, and for reading and writing
    MAT-files.

9
What is Matlab?
  • Some facts for a first impression
  • Everything in MATLAB is a matrix !
  • MATLAB is an interpreted language, no
    compilation needed (but possible)?
  • MATLAB does not need any variable declarations,
    no dimension statements, has no packaging, no
    storage allocation, no pointers
  • Programs can be run step by step, with full
    access to all variables, functions etc.

10
What does matlab code look like?
  • t 0pi/1002pi
  • y sin(t)
  • plot(t,y)?

11
What does matlab code look like?
  • t 0pi/1002pi
  • y sin(t)
  • plot(t,y)?

Remember EVERYTHING IN MATLAB IS A MATRIX !
creates 1 x 200 Matrix
Argument and result 1 x 200 Matrix
12
(No Transcript)
13
  • Rows and columns are always numbered starting at
    1
  • A single number is really a 1 x 1 matrix in
    Matlab!
  • Matlab variables are not given a type, and do
    not need to be declared
  • Any matrix can be assigned to any variable

14
Building matrices with A 2 7 4 A 2
7 4 A 2 7 4 3 8 9 B A A
2
7
4
15
(No Transcript)
16
  • A matrix can be indexed using another matrix, to
    produce a subset of its elements
  • a 100 200 300 400 500 600 700 b 3 5
    6
  • c a(b)
  • 300 500 600

17
Matrices
  • a vector x 1 2 5 1
  • x
  • 1 2 5 1
  • a matrix x 1 2 3 5 1 4 3 2 -1
  • x
  • 1 2 3
  • 5 1 4
  • 3 2 -1

18
Matrices
  • x(i,j) subscription
  • whole row
  • whole column

yx(2,3)? y 4 yx(3,)? y 3 2
-1 yx(,2)? y 2 1 2
19
Operators (arithmetic)?
  • addition
  • - subtraction
  • multiplication
  • / division
  • power
  • complex conjugate transpose

. element-by-element mult ./ element-by-element
div . element-by-element power . transpose
20
Operators (relational, logical)
  • equal
  • not equal
  • lt less than
  • lt less than or equal
  • gt greater than
  • gt greater than or equal
  • AND
  • OR
  • NOT

pi 3.14159265 j imaginary unit, i same as j
21
Generating Vectors from functions
  • x zeros(1,3)?
  • x
  • 0 0 0
  • x ones(1,3)?
  • x
  • 1 1 1
  • x rand(1,3)?
  • x
  • 0.9501 0.2311 0.6068
  • zeros(M,N) MxN matrix of zeros
  • ones(M,N) MxN matrix of ones
  • rand(M,N) MxN matrix of uniformly distributed
    random numbers on (0,1)?

22
Programming in Matlab (M-files)?
  • Executing commands in the command window is fine
    for short scripts but when dealing with long
    scripts for complex problem-solving (or when
    programming) M-files is a must.
  • It allows the user to write MATLAB command in a
    text file (called M-file) and then MATLAB will
    open the file and execute the commands exactly as
    it would if the user typed them at the MATLAB
    Command Window. The M-file editor is the MATLABs
    tool for creating M-files.

23
Programming in Matlab (M-files)?
  • There are two basic kinds of m-files
  • Scripts
  • Functions
  • Scripts are m-files that execute a series of
    statements
  • Functions are m-files that accept arguments and
    produce an output

24
Example Script 1
  • Find the solution, x, to the following system of
    equations

25
Example Script 1 solution
  • Use the MATLAB editor to create a new file
  • Enter the following statement
  • A 1 2 3 3 3 4 2 3 3
  • b 1 1 2
  • x A\b

26
Example Script 2
  • Plot the following cosine functions,
  • y1 2 cos(x),
  • y2 cos(x)
  • y3 0.5cos(x)?
  • in the interval

27
Example Script 1 solution
  • Use the MATLAB editor to create a new file
  • Enter the following statement
  • x 0pi/1002pi
  • y1 2cos(x)
  • y2 cos(x)
  • y3 0.5cos(x)
  • plot(x,y1,'--',x,y2,'-',x,y3,'')?
  • xlabel('0 \leq x \leq 2\pi')?
  • ylabel('Cosine functions')?
  • legend('2cos(x)','cos(x)','0.5cos(x)')?
  • title('Typical example of multiple plots')?
  • axis(0 2pi -3 3)?

28
Script shortcomings
  • All variables created in a script file are added
    to the workspace. This may be undesirable
    because
  • Variables already existing in the workspace may
    be overwritten.
  • The execution of the script can be affected by
    the state variables in the workspace.
  • Use a function for anything complicated

29
The Anatomy of a Function
  • function f factorial(n) (1)?
  • FACTORIAL(N) returns the factorial of N. (2)?
  • Compute a factorial value. (3)?
  • f prod(1n) (4)?

30
Differences between Scripts and Functions
31
Functions input output arguments
  • input arguments are listed inside parentheses
    following the function name.
  • The output arguments are listed inside the
    brackets on the left side. They are used to
    transfer the output from the function file.
  • The general form looks like this
  • function outputs function_name(inputs)?

32
Functions input output arguments
  • Examples of input and output arguments
  • function areaTrapArea(a,b,h) Three inputs and
    one output
  • function h,dmotion(v,angle) Two inputs and two
    outputs

33
Inputs to scripts?
  • Inputs are sent to script files by
  • Defining the variable in the script itself
  • Defining the variable in the workspace
  • Defining the variable when the script executes

34
Inputs to scripts?
  • We've discussed the first two, so lets look at
  • Defining the variable when the script executes
  • This means that we will ask the user to supply
    some information when the script is executed
  • Using the input command

35
The Input Command
  • Lets try an example script
  • This script file calculates the average of
    points
  • scored in three games.
  • The point from each game are assigned to a
    variable
  • by using the input' command.
  • game1 input('Enter the points scored in the
    first game ')
  • game2 input('Enter the points scored in the
    second game ')
  • game3 input('Enter the points scored in the
    third game ')
  • average (game1game2game3)/3

36
Flow Control
  • The most common decision-making (or flow control)
    structures in matlab are
  • The if ... else ... end structure
  • Example, based on quadratic formula
  • discr bb - 4ac
  • if discr lt 0
  • disp('Warning discriminant is negative, roots
    are imaginary')
  • elseif discr 0
  • disp('Discriminant is zero, roots are repeated')?
  • else
  • disp('Roots are real')?
  • end

37
Flow Control
  • Logical operators are used to compare two
    objects

38
Flow Control
  • Another common flow control structure is
  • The for ... end loop
  • Example
  • for ii15
  • xiiii
  • end

39
Flow Control
  • Can also use nested loops as well.
  • To populate a 5x5 symmetric matrix where the
    indices correspond to i/j we have
  • n 5 A eye(n)
  • for j2n
  • for i1j-1
  • A(i,j)i/j
  • A(j,i)i/j
  • end
  • end

40
Saving output to a file
  • The command fprintf is used to write output to a
    file.
  • To save the results of some computation to a file
    in a text format requires the following
  • steps
  • 1. Open a file using fopen
  • 2. Write the output using fprintf
  • 3. Close the file using fclose

41
Saving output to a file
  • write some variable length strings to a file
  • op fopen('weekdays.txt','wt')
  • fprintf(op,'Sunday\nMonday\nTuesday\nWednesday\n')
  • fprintf(op,'Thursday\nFriday\nSaturday\n')
  • fclose(op)
  • Notes
  • the option 'wt' is used in the windows platform
    to set a file for writing.
  • the command \n forces a carriage return

42
Furthering your matlab skills
  • The scripts presented in this lecture are merely
    an introduction to matlab that barely scrape the
    surface
  • To understand it yourself you must really get
    your hands dirty and dig into programming.
  • The built-in help is very good in matlab, so if
    you are unsure of how to do something you should
    check there first
Write a Comment
User Comments (0)
About PowerShow.com