Tutoriala MATLAB
30 pages
English

Tutoriala MATLAB

Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres
30 pages
English
Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres

Description

MATLAB Tutoriala whirlwind tourHinke Osingah.m.osinga@bris.ac.ukEngineering MathematicsQueen’s Building 2.12MATLAB Tutorial – p.1/30The BasicsMATLAB can do everything a calculator does>>(1+4)*3ans =15As is standard:+ and- are addition,/ is division and* is multiplication,ˆ is an exponentMATLAB Tutorial – p.2/30Everything is a Matrix!In MATLAB, a scalar is actually a 1×1 matrixa vector is an n×1 or 1× n matrix>>a = 3a =3a is the number 3, but also the 1×1 matrix [3]Here is how to create a row vector>>v = [1 2 3]v =1 2 3MATLAB Tutorial – p.3/30Everything is a Matrix!A semicolon tells MATLAB to start a new row, so>>w = [4; 5; 6]w =456is a column vector.With a’ you turn a column vector into a row vector>>w’ans =4 5 6MATLAB Tutorial – p.4/30Multiplication of matricesYou can multiply the vectorsv andw>>v*wans =32Recall: 1×3 times 3×1 gives 1×1Similarly, 3×1 times 1×3 gives 3×3>>A = w*vA =4 8 125 10 156 12 18MATLAB Tutorial – p.5/30Matrix operationsStandard multiplication is really matrix multiplicationDimensions must match!Try typing>>v*vHowever, MATLAB can do 1×1 times a matrix:>>3*ACheck what MATLAB does when adding a scalar anda matrix>>A + 2MATLAB Tutorial – p.6/30Elementwise operationsElementwise operations are done using a. before theoperator. For example, each element is squared using>>sqv = v.ˆ2sqv =1 4 9If two vectors (or matrices) have the same dimensionsyou can perform an elementwise ...

Informations

Publié par
Nombre de lectures 27
Langue English

Extrait

MATLAB Tutorial
a
whirlwind tour
Hinke Osinga
h.m.osinga@bris.ac.uk
Engineering Mathematics
Queen's Building 2.12
MATLAB Tutorial – p.1/30
The Basics
MATLAB can do everything a calculator does
>>(1+4)*3 ans =
15 As is standard:
+and-are addition, /is division and*is multiplication, ˆis an exponent
MATLAB Tutorial – p.2/30
Everything is a Matrix!
In MATLAB, a scalar is actually a1×1matrix a vector is ann×1or1×nmatrix
>>a = 3 a = 3 ais the number 3, but also the1×1matrix[3] Here is how to create a row vector
>>v = [1 2 3] v = 1
2
3
MATLAB Tutorial – p.3/30
Everything is a Matrix!
A semicolon tells MATLAB to start a new row, so
>>w = [4; 5; 6] w =
4 5 6
is a column vector. With a'you turn a column vector into a row vector >>w' ans =
4
5
6
MATLAB Tutorial – p.4/30
Multiplication of matrices
You can multiply the vectorsvandw
>>v*w ans = 32
Recall:1×3times3×1gives1×1 Similarly,3×1times1×3gives3×3
>>A = w*v A =
4 8 12 5 10 15 6 12 18
MATLAB Tutorial – p.5/30
Matrix operations
Standard multiplication is really matrix multiplication
Dimensions must match!
Try typing
>>v*v However, MATLAB can do1×1times a matrix:
>>3*A Check what MATLAB does when adding a scalar and a matrix
>>A + 2
MATLAB Tutorial – p.6/30
Elementwise operations
lEementwiseoperationsareodnesuignabeforeteh
. operator. For example, each element is squared using
>>sqv = v.ˆ2 sqv = 1 4 9 If two vectors (or matrices) have the same dimensions you can perform an elementwise product
>>v.*sqv ans = 1
8
27
MATLAB Tutorial – p.7/30
Functions operate elementwise
Functions operate on each element in a matrix:
>>exp(v) ans = 2.7183
>>log(ans) ans = 1 2
>>sqrt(v) ans = 1.0000
3
7.3891
1.4142
20.0855
1.7321
MATLAB Tutorial – p.8/30
huonewdtituupityf
>>ans ans = 3.1416
; MATLAB saves the last output in the variableans.
>>y = tan(pi/6);
Special constants
>>pi ans = 3.1416
The variablepi
MATLAB Tutorial – p.9/30
sirepaenamavtnlewiriablueπthvaerppusBAoehtsessTLMA
Dealing with Matrices
To create a matrix, you could do something like:
>>M = [-3 0 1; 2 5 -7; -1 4 8] The semicolons indicate the end of a row. All rows have to be the same length.
Theelementinthetihdror,wrstcloumn,is
M(3,1) You get the elements in rows 2 through 3 and columns 1 through 2, by typing
>>M(2:3,1:2) ans = 2 5 1 4
MATLAB Tutorial – p.10/30
Extract a submatrix
So, to get the entire second column, you type
>>M(1:3,2) which is the same as
>>M(:,2) which is literally telling MATLAB to use all rows second column
You get a whole row of a matrix with
>>M(1,:)
in
the
MATLAB Tutorial – p.11/30
  • Univers Univers
  • Ebooks Ebooks
  • Livres audio Livres audio
  • Presse Presse
  • Podcasts Podcasts
  • BD BD
  • Documents Documents