Title: Eng. 6002 Ship Structures 1
1Eng. 6002 Ship Structures 1
2What 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.
3What 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)
4What 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
28 January 2003, Matlab tutorial Joanna Waniek
(jowa_at_soc.soton.ac.uk)
5Using 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.
6What 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)
7What 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.
8What 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.
9What does matlab code look like?
- t 0pi/1002pi
- y sin(t)
- plot(t,y)
10What 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
11(No Transcript)
12- 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
13Building matrices with A 2 7 4 A 2
7 4 A 2 7 4 3 8 9 B A A
2
7
4
14(No Transcript)
15- 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
16Matrices
- 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
17Matrices
- 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
18Operators (arithmetic)
- addition
- - subtraction
- multiplication
- / division
- power
- complex conjugate transpose
. element-by-element mult ./ element-by-element
div . element-by-element power . transpose
19Operators (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
20Generating 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)
21M-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.
22m-files
- My first M-file
- x0pi/102pi
- fsin(x)
- gcos(x)
- plot(x,f,x,g,)
- xlabel(x')
- ylabel(y')
- title(sin(x) and cos(x))
- legend(sin(x),'cos(x))
We wish to write a MATLAB code (script) to plot
f(x) and g(x) on a single graph with labels,
title and legend. Where f(x) sin(x),g(x)
cos(x)
23Where is this going?
- As a final assignment in the course, we will be
writing a Matlab script (or function) to solve a
beam problem