MATLAB Jirawat Kanjanapitak (Tae) What is MATLAB A computer program for doing numerical computation including; Arithmetic, Polynomials, Graphics, 2-D Plots, Matrices, Systems of Equations etc. Getting Started Open Program Click “MATLAB R2006a” on Desktop OR Go to “Start” All Program > MATLAB > MATLAB R2006a MATLAB Desktop Default layout Arranging the Desktop including resizing, moving, and closing tools. Taken from MATLAB Help MATLAB for Problem Solving Arithmetic Calculator Type in any basic arithmetic as shown Click “Enter” >> 1+1 ans = 2 >> (7*5)+2 ans = 37 >> [(8+2^2)/(1*3)] ans = 4 Basic Operations Operation x+y x-y xy x/y xy ex |x| π MATLAB x+y x-y x*y x/y x^y exp(x) abs(x) pi sqrt(x) Vectors To increment using colon >> v = [1:5] v=1 2 3 4 5 To increment other than1 To transpose a column vector to a row vector, use an apostrophe “‘“ >>v = [5:0.5:7] v = 5 5.5 6 6.5 7 >>v = [1 2 3]’ To view individual entries in this vector v(2) Ans = 5.5 To enter a vector >> v = [1 2 3] 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 3 Use command >> a = [ 1 2; 3 4 ] 2 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. y m c r g b yellow magenta cyan red green blue k black * star . dotted -x dashed x-mark Plotting Examples x = 0:0.1:100; y = 2*x; plot (x,y,'--') 200 180 160 140 120 100 80 60 40 20 0 0 10 20 30 40 50 60 70 80 90 100 Plotting Examples 2 x = 0:0.1:5; y = sin (x); 1 0.8 0.6 0.4 plot (x,y,'x') 0.2 0 -0.2 -0.4 -0.6 -0.8 -1 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 Plotting Examples 3 x = linspace(0,2*pi,50); y = sin (x); z = cos (x); plot (x,y, x,z) 1 0.8 0.6 0.4 0.2 0 -0.2 -0.4 -0.6 -0.8 -1 0 1 2 3 4 5 6 7 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) 1 1 0.8 0.8 0.6 0.6 0.4 0.4 0.2 0.2 0 0 -0.2 -0.2 -0.4 -0.4 -0.6 -0.6 -0.8 -0.8 -1 0 2 4 6 8 -1 0 2 4 6 8 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 title name 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') 180 put text in the middle of plot 160 140 label of the y-axis 200 120 100 80 60 40 20 0 0 10 20 30 40 50 60 label of the x-axis 70 80 90 100 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 multiply two polynomial x = [1 2 3]; y = [2 4 6 8]; z = conv(x,y) [xx, w] = deconv(z,y) xx = 1 2 3 w= z= 2 8 20 32 34 To devide two polynomial 24 0 0 0 0 0 0 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.ht ml http://physics.gac.edu/~huber/matlab/mtlabsym.htm