Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Tutorial matlab

318 views

Published on

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

Tutorial matlab

  1. 1. 1Introduction to MatlabIntroduction to Matlab1. Vectors, matrices, and arithmetic2. Plotting3. Flow Constructs4. Other Useful Things
  2. 2. 2Why use Matlab?• Drawbacks:Slow compared to C or Java• Advantages:Handles vector and matrices very nicelyQuick plotting and analysisEXTENSIVE documentation (type ‘help’)Lots of nice functions: FFT, fuzzy logic, neuralnets, numerical integration, OpenGL (!?)Vectors and Matrices• Can be run from command line or from *.m filescalar: x = 3vector: x = [1 0 0]2D matrix: x = [1 0 0; 0 1 0; 0 0 1]arbitrarily higher dimensions possible• Can also use matrices / vectors as elements:x = [1 2 3]y = [ x 4 5 6]
  3. 3. 3Some Standard matrices• ones(3,3) 3x3 of all ones• zeros(3,3) 3x3 of all zeros• eye(3,3) 3x3 identity• rand(3) 3x3 random elements• linspace(1,10,100)linear spacing from 1 to 10, with 100spacings (also logspace)• x = 1:10linear spacing from 1 to 10, counting by 1Accessing elements• MATLAB IS NOT ZERO INDEXED!• x retrieves entire matrix x• x(1,2) retrieves element at row 1, col 2• x(1, 5:10) retrieves row 1, columns 5 to 10• x(1,:) retrieves row 1, all columns• Useful functions:length(x) length of vector x (cols)size(x) rows, cols of x
  4. 4. 4Matrix Operations• For matrix operations– Dimensions must agree• Scalar operations– Same as usual• Scalar / matrix mixed– Scalar + matrix = [scalar + matrix(x, y)]– Scalar * matrix = [scalar * matrix(x, y)]More Matrix Operations• The ‘.’ operator– “element by element” access• Example:– x = [1 2 3]; y = [4; 5; 6];– x * y = 32– x .* y = [4 10 18]• For some functions :– x ^ 2 ERROR!– x . ^2 fine
  5. 5. 5More Matrix Operations• x=1:12• reshape(x, 3,4)• a=2*ones(3,4)• X.*a• b=[1:3;1:3;1:3;1:3]• X.*b• y=reshape(x, 4,3)• y.^bPlotting• 2D graphingplot(x,y)• Example:x = linspace(-10,10,100)y = x .^2plot(x,y)• Also:z = x .^3plot(x,z)
  6. 6. 6Plotting Examplebasis = [[1 0];[0 0];[0 1];];square = [[ 1 1][-1 1];[-1 -1];[ 1 -1];[ 1 1];];axis equal;plot( sqrt(3), sqrt(3), w.);plot( -sqrt(3), -sqrt(3), w.);plot( basis(1,:), basis(2,:), k);plot( square(1,:), square(2,:), b);pauseobj = S*square;plot( obj(1,:), obj(2,:), r);pausePlotting Examplebasis = [[1 0];[0 0];[0 1];[1 0];];square = [[ 1 1];[-1 1];[-1 -1];[ 1 -1];[ 1 1];
  7. 7. 7More Plotting• Graphics Window– To open a new graph, type ‘figure’• Multiple data sets:– Type ‘hold on’ to add new plot to current graph– Type ‘hold off’ to resume default mode• Make your graph beautiful:– title(‘apples over oranges’)– xtitle(‘apples’)– ytitle(‘oranges’)3D Plotting• 3D plots – plot an outer productx = 1:10y = 1:10z = x’ * ymesh(x,y,z)Single quote ‘ means transpose
  8. 8. 8Flow Control• IF blockif (<condition>)<body>elseif<body>end• WHILE blockwhile (<condition>)<body>endConditions same as C, ( ==, >=, <=) except != is ~=More Flow Control• FOR blockfor i = 1:10<body>end• SWITCH statementswitch <expression>case <condition>,<statement>otherwise <condition>,<statement>end
  9. 9. 9Other Language Features• Matlab language is pretty sophisticated– FunctionsStored in a *.m file of the same name:function <return variable> = <function name>(<args>)<function body>– Structs• point.x = 2; point.y = 3; point.z = 4;Useful Commands• Single quote is transpose• % same as // comment in C, JavaNo /* block comments */• ; suppresses printing• More:max(x) min(x)mean(x) median(x)abs(x) dot(x,y)cross(x,y) flops (flops in this session)
  10. 10. 10Useful Constants• Inf infinity• NaN Not a number (div by zero)• eps machine epsilon (precision)• ans most recent unassigned answer• pi 3.14159….• i and j Matlab supports imaginarynumbers!Programming• Wrong:for x = 1:10for y = 1:10foo(x,y) = 2 * bar(x,y)endend• Right:foo = 2 * bar;• Matlab is optimized for vectorization
  11. 11. 11Symbolic Maths• Symbolic mathematics can be done using Matalb:a = sqrt(sym(2))a =2^(1/2)th=sym(th);rt=sym([cos(th) sin(th) 0;-sin(th) cos(th) 0;0 0 1]);rt =[ cos(th), sin(th), 0][ -sin(th), cos(th), 0][ 0, 0, 1]Good luck

×