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 >> 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 Operations OperationMATLAB x + y x - y x yx*y x / y xyxy x^y exex exp(x) | x |abs(x) πpi sqrt(x)
Vectors To enter a vector >> v = [1 2 3] v = To transpose a column vector to a row vector, use an apostrophe “‘“ >>v = [1 2 3] ’ v = To increment using colon >> v = [1:5] v = To increment other than1 >>v = [5:0.5:7] v = To view individual entries in this vector v(2) Ans = 5.5
Vector Examples >> v = [1:5]; >> u = [0:-1:4]; u+v Ans: v^2 Ans: Note: Error message will appear from adding two vectors whose dimensions are different.
Matrices To enter a matrix Use command >> a = [ 1 2; 3 4 ]
Matrix Examples >> a = [ 1 2; 3 4 ]; >> b = [ 0 1; 2 3 ]; >> a+b ans = >>a*b ans = T = a+b; >> inv(t) ans = >> inv(t)/t ans =
Plotting Use “plot” command Format: plot(x,y, ‘m’) The third input is a characteristic of the graph. yyellow mmagenta ccyan rred ggreen bblue kblack *star.dotted --dashed xx-mark
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; t 3 +4t 2 -3t+1 x = [ ] or x 5 +3x 2 -6 x = [ ]
Polynomial Examples To multiply two polynomial x = [1 2 3]; y = [ ]; z = conv(x,y) z = To devide two polynomial [xx, w] = deconv(z,y) xx = w = 0 0 0
Getting HELP! Use “help” command help commandname