29
MATLAB Jirawat Kanjanapitak (Tae)

MATLAB Jirawat Kanjanapitak (Tae). What is MATLAB A computer program for doing numerical computation including; Arithmetic, Polynomials, Graphics, 2-D

Embed Size (px)

Citation preview

MATLAB

Jirawat Kanjanapitak (Tae)

What is MATLABWhat is MATLAB

A computer program for doing numerical computation including; Arithmetic, Polynomials, Graphics, 2-D Plots, Matrices, Systems of Equations etc.

Getting StartedGetting Started

Open ProgramOpen Program Click “MATLAB R2006a” on Desktop

OR

Go to “Start” All Program > MATLAB > MATLAB R2006a

MATLAB DesktopMATLAB Desktop Default layout Arranging the

Desktop including resizing, moving, and closing tools.

Taken from MATLAB Help

MATLAB for Problem SolvingMATLAB for Problem Solving

Arithmetic CalculatorArithmetic Calculator >> 1+1

ans =

2

>> (7*5)+2

ans =

37

>> [(8+2^2)/(1*3)]

ans =

4

Type in any basic arithmetic as shown

Click “Enter”

Basic OperationsBasic Operations Operation MATLAB

x + y x+y

x - y x-y

x y x*y

x / y x/y

xy x^y

ex exp(x)

| x | abs(x)

π pi

sqrt(x)

VectorsVectors To enter a vector >> v = [1 2 3]

v = 1 2 3

To transpose a column vector to a row vector, use an apostrophe “‘“

>>v = [1 2 3]’

v = 1

2

3

To increment using colon

>> v = [1:5]v = 1 2 3 4 5

To increment other than1

>>v = [5:0.5:7]v = 5 5.5 6 6.5 7

To view individual entries in this vector

v(2)Ans = 5.5

Vector ExamplesVector Examples

>> v = [1:5]; >> u = [0:-1:4]; u+v Ans: 1 1 1 1 1 v^2 Ans: 1 4 9 16 25

Note: Error message will appear from adding two vectors whose dimensions are different.

MatricesMatrices

To enter a matrix

1 2

3 4 Use command

>> a = [ 1 2; 3 4 ]

Matrix ExamplesMatrix Examples

>> a = [ 1 2; 3 4 ]; >> b = [ 0 1; 2 3 ]; >> a+b ans = 1 3 5 7 >>a*b ans = 4 7 8 15

T = a+b; >> inv(t) ans =

-0.8750 0.3750

0.6250 -0.1250 >> inv(t)/t ans =

1.0000 -0.3750

-0.6250 0.2500

PlottingPlotting

Use “plot” command

Format: plot(x,y, ‘m’)

The third input is a characteristic of the graph.

y yellow

m magenta

c cyan

r red

g green

b blue

k black

* star

. dotted

-- dashed

x x-mark

Plotting ExamplesPlotting Examples

x = 0:0.1:100; y = 2*x; plot (x,y,'--')

0 10 20 30 40 50 60 70 80 90 1000

20

40

60

80

100

120

140

160

180

200

Plotting Examples Plotting Examples 22

x = 0:0.1:5; y = sin (x); plot (x,y,'x')

0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Plotting Examples Plotting Examples 33

x = linspace(0,2*pi,50); y = sin (x); z = cos (x); plot (x,y, x,z)

0 1 2 3 4 5 6 7-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Sub-PlottingSub-Plotting

Use “subplot” command to put more than one plot in the same figure

subplot (m,n,p)

where; m = number of rows

n = number of column

p = plot number

Sub-Plotting ExampleSub-Plotting Example

x = linspace(0,2*pi,50);

y = sin (x); z = cos (x); subplot (1,2,1) plot (x,y) subplot (1,2,2) plot (x,z)

0 2 4 6 8-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

0 2 4 6 8-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

Adding TextAdding Text

Below are the text adding related commands

title (‘title name’)

xlabel (‘label of the x-axis’)

ylabel (‘label of the y-axis’)

gtext (‘put text in the middle of plot’)

Adding Text ExampleAdding Text Example

x = 0:0.1:100; y = 2*x; plot (x,y,'--') title ('title name') xlabel ('label of the x-axis') ylabel ('label of the y-axis') gtext ('put text in the middle of plot')

0 10 20 30 40 50 60 70 80 90 1000

20

40

60

80

100

120

140

160

180

200title name

label of the x-axis

labe

l of

the

y-ax

is

put text in the middle of plot

PolynomialsPolynomials Vector is used to

represented polynomial

in MATLAB.

For example;

t3+4t2-3t+1

t = [1 4 -3 1]

orx5+3x2-6x = [1 0 0 3 0 -6]

Polynomial ExamplesPolynomial Examples To multiply two

polynomial

x = [1 2 3];

y = [2 4 6 8];

z = conv(x,y)

z =

2 8 20 32 34 24

To devide two polynomial

[xx, w] = deconv(z,y) xx =

1 2 3 w =0 0 0 0 0 0

Symbolic MathSymbolic Math

Use for factoring solving, root finding, differentiating, and integrating.

Use sym command to create a symbolic object.

Example:sqrt(2)ans

1.4142

With sym commanda = sqrt(sym(2))ans

a = 2^(1/2)

Symbolic MathSymbolic Math Cont. Cont.

Use double command to get the value of the symbolic object.

double(a)

ans =

1.4142

Use sym command to find common denominator.

sym(2)/sym(5) + sym(1)/sym(3)

ans =

11/15

Solve Command ExampleSolve Command Example

Eqn = ax^2 + bx + c = 0 To solve symbolically for the variable x use

solve(Eqn,'x') To find the value of b which makes this true

solve(QuadEqn,'a=3','c=-1','x=-1') A more complicated example

solve(QuadEqn,'a=sqrt(c)','b=2','x=-1')

Getting HELP!Getting HELP!

Use “help” command

help commandname

ReferencesReferences

MATLAB Help MATLAB Tutorials

http://www.cyclismo.org/tutorial/matlab/ http://www.math.utah.edu/lab/ms/matlab/matlab.html http://www.engin.umich.edu/group/ctm/basic/basic.ht

ml http://physics.gac.edu/~huber/matlab/mtlabsym.htm