1Introduction to MatlabTodd WittmanJune 16, 2008What is Matlab? Matlab (Matrix Laboratory) is a programming language optimized for linear algebra operations. It is very useful for numerical computation and is commonly used by mathematicians. After you program in Matlab, you probably won't want to go back to C++.2Basic Arithmetic If we type a calculation, when we press ENTER the result appears as ans.2+3ans =5 Pressing the up arrow repeats the last input. In addition to basic + - * / there are basic mathematical functionsexp(2) sin(pi) asin(2) There may be round-off errors. Watch out for the values Inf and NaN.Variables To assign a value to a variable, just type it. We don't need to declare variables first: x=2 Matlab is weakly typed, which means the variable type is flexible.x=2 x=2.3 x=2+4i x='hello' To see what variables are available type: who To delete a variable x: clear x If you type just clear, all variables will be deleted.3Vectors & Matrices We enclose vector values in square brackets.v = [2 3 4 5 6] We can look up a value at a position: v(2) The colon operator takes on a range of valuesv = 2:6 More generally we set start:step:end (default step=1) Ex Make a vector of even integers 2 through 100evens = 2:2:100Matrices To make a 2D matrix, the semi-colon skips to the next row:A = [2 3 4; 5 6 7] We look up values by row,column: A(2,3) = 7; You can look up sections with the colon: A(1:2,2:3) Special matricesrand(10,20) eye(3) zeros(4,5) ones(3,2) As matrices get large, you can supress output with a semi-colon at the end.R = rand(20,20); We can display matrix values graphically with imagesc.4Matrix Operations Matrix multiplication: A*B A^3 Component-wise operations: A.*B A.^3 Inverse: inv(A) Transpose: A' Look up matrix size: size(A) Eignenvalues: [v,d] = eig(A); Linear solver Ax=b: x = linsolve(A,b); Vectorize a matrix: A(:) Change matrix size: reshape(A,[r,c]);Basic Flow Controlwhile i > 0...endfor i = 1:10...endif x >0...elseif x<0...else...end5Basic Plotting The plot function takes two vectors as input. The first goes on the horizontal axis (x) and the second is the vertical (y). Ex Plot a sine wave.x=0:0.1:2*pi;y=sin(x);plot(x,y,'r')axis([0,2*pi,-1,1])title('A sine wave') You can reset the axis and tick marks manually too. You can add text to the plot:xlabel ylabel title legend gtextLoading & Saving Data You can navigate directories by pressing the ... button at top. Check current position: pwd ls imread - Read an image into a matrix. You may have to convert between uint8 and double formats. textread - Read a text file. xlsread - Read an Excel file. We can save data to a .mat filesave my_data x y zload my_data6m Files We can create programs with extension .m We essentially define new functions for Matlab to use. The first line is:[out1, out2] = function_name (in1, in2) Comments are denoted by the % sign. Ex Write a function that computes the average band of a 3-band color image.Symbolic Variables To do symbolic computation, we create symbolic variables with syms. Then we can do symbolic integration and differentiation.syms x;diff(3*x^2+6*x-4)ans =6*x+6int(exp(2*x))ans =1/2*exp(2*x)7The Matlab Help Matlab has awesome Help documentation. It usually provides examples of how to use functions. If you need to do a standard math calculation, there is probably a Matlab calculation that will do it for you.Watch the Video For a summary of what we've covered, watch the Youtube video by Stuart McGarrity.http://www.youtube.com/watch?v=MdrShPzHeYg Mathworks also has some video demos
or
We will never post anything without your permission.
Don't have an account? Sign up