340 likes | 638 Views
MATLAB. Jirawat Kanjanapitak (Tae). A computer program for doing numerical computation including; Arithmetic, Polynomials, Graphics, 2-D Plots, Matrices, Systems of Equations etc. What is MATLAB. Getting Started. Open Program. Click “MATLAB R2006a” on Desktop OR Go to “Start”
E N D
MATLAB Jirawat Kanjanapitak (Tae)
A computer program for doing numerical computation including; Arithmetic, Polynomials, Graphics, 2-D Plots, Matrices, Systems of Equations etc. What is MATLAB
Open Program • Click “MATLAB R2006a” on Desktop OR • Go to “Start” • All Program > MATLAB > MATLAB R2006a
Default layout Arranging the Desktop including resizing, moving, and closing tools. MATLAB Desktop
>> 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” Arithmetic Calculator
Vectors • 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 • 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
Vector 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.
Matrices • To enter a matrix 1 2 3 4 • Use command >> a = [ 1 2; 3 4 ]
Matrix 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
Plotting • Use “plot” command Format: plot(x,y, ‘m’) • The third input is a characteristic of the graph.
Plotting Examples • x = 0:0.1:100; • y = 2*x; • plot (x,y,'--')
Plotting Examples 2 • x = 0:0.1:5; • y = sin (x); • plot (x,y,'x')
Plotting Examples 3 • x = linspace(0,2*pi,50); • y = sin (x); • z = cos (x); • plot (x,y, x,z)
Sub-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 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)
Adding 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 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')
Polynomials • Vector is used to represented polynomial in MATLAB. For example; t3+4t2-3t+1 t = [1 4 -3 1] or x5+3x2-6 x = [1 0 0 3 0 -6]
Polynomial Examples • To devide two polynomial [xx, w] = deconv(z,y) • xx = 1 2 3 • w = 0 0 0 0 0 0 • To multiply two polynomial x = [1 2 3]; y = [2 4 6 8]; z = conv(x,y) • z = 2 8 20 32 34 24
Symbolic 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 command a = sqrt(sym(2)) ans a = 2^(1/2)
Symbolic Math 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 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! • Use “help” command help commandname
References • 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.html • http://physics.gac.edu/~huber/matlab/mtlabsym.htm