Uploaded on

 

More in: Education , Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,395
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
91
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Introduction to Matlab By Abd El Kareem Ahmed
  • 2. Contents
    • MATLAB Environment
      • Command window, Workspace, Path window, Editor window,and Figure window)
    • Basic Functions: clc, clear, save, load, who, whos …
    • Variables in MATLAB
      • Arrays
    • Control flow (for – end, while – end, if – else – elseif – end, switch
    • M-files
    • Plotting
    • Introduction to GUI
    • Introduction to Image processing Toolbox
  • 3. MATLAB Environment Command window Use Menus as an alternative to typing some commands Use of Toolbar for easy access to popular operations Status bar Command window Status of Caps, Num, and Scroll locks New M-file Open file Undo New Simulink Model Help Cut Paste Copy Workspace Browser Path Browser
  • 4. MATLAB Environment Workspace Open the selected matrix Delete the selected matrix from the workspace Displays the total number of elements in the workspace and total size of them. Close the workspace browser size of the matrix Number of bytes in the matrix Name of the matrix Type of the matrix
  • 5. MATLAB Environment Workspace Name of the matrix Current dimension of the matrix: User can change the dimensions of the matrix by simply type the dimensions he/she want Current Cell Current values: User can change the values o the matrix’s element by editing in the cell.
  • 6. MATLAB Environment Path browser The current Directory The directories in the path The Files in the selected path. Add a new path Remove the selected path
  • 7. MATLAB Environment Editor window Current line number Automatic Indenting Automatic Color Highlighting to Distinguish Different Elements The toolbar Names of the opened files in the editor New file Save file Copy Print Set/Clear break point Step in Continue List if functions in call stack Open file Cut Paste Help Clear all break points Single step Quit debugging
  • 8. MATLAB Environment Figure window New figure Open an exciting figure Save the current figure Print Zoom the plot Rotate the plot Start / End the plot editor mode Draw an arrow in the plot Type a text in the plot Draw a line in the plot
  • 9. Basic Functions load save lookfor help whos who clear clc Function name Loading saved workspace Saving workspace Search for a word in MATLAB files Show information about any function Show workspace in details Show workspace Clear workspace Clear the command window Function mean
  • 10. Variables
    • MATLAB variables are arrays
    • Variable must be one word
      • myvariable accepted
      • my variable not accepted
    • Variable length is up to 31 character
    • Variables must begin by a character
    • MATLAB variables are case sensitive
    • There are preserved variable names:
  • 11. Variables The number of output parameters nargout The number of input parameters nargin Maximum real number = 10 308 × 1.7977 realmax Minimum real number = 10 -308 × 2.2251 realmin When the result = 0/0 NaN Very large value = ∞ as the result of (1/0) inf Very small value eps Pi = 3.14 pi Default function output ans
  • 12. Arrays : review
    • Vector Array : (1 × n), (m ×1)
    • Matrix : (m × n)
    • Identity array
    5 4 0 7 0 5 7 1 4 4 5 12 8 9 15 1 0 0 0 1 0 0 0 1
    • 5
    • 3 4
    • 0 0
    (2×4) -> (3×2) -> (1×4) -> (3×1) ->
  • 13. Arrays : review
    • Assume You have A = B=
    • A+B= A’ =
    • 2A = =
    1 3 0 2 5 4 0 7 0 1 4 3 7 4 9 8 1 4 4 5 12 8 9 15 2 6 0 4 10 8 0 14
    • 5
    • 3 4
    • 0 0
    • 2 7
    ax+by cx+dy ex+fy (m×k)(k×n)=(m×n) a b c d e f x y
  • 14. Arrays : Marlab vision 2 3 5 9 1 0 3 6 4 5 1 10 8 6 4 M=
    • M=[2 3 5 9 1 ; 0 3 6 4 5 ; 1 10 8 6 4]
      • M(1)  2
      • M(6)  10
      • M(3, 4)  6
      • M(3, :)  1 10 8 6 4
      • M(:, 2)  3
      • 3
      • 10
      • M(1, 2:4)  3 5 9
      • M(2, 1:2:5)  0 6 5
    a=[1 4 6 8 9] a=[1,4,6,8,9] m=[18] a=3*6 m=[4] m=4
  • 15. 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 0 0 0 1 1 0 0 0 1 0 8 1 6 3 5 7 4 9 2 0.1389 0.6038 0.0153 0.9318 0.2028 0.2722 0.7468 0.4660 0.1987 0.1988 0.4451 0.4186 magic(3) = magic logspace (0,4,5)= [1 10 100 1000 10000] logspace (1,2,3)= [10 31.6228 100] logspace linspace ( 0,4,5 )= [0 1 2 3 4] linspace ( 1,2,3 )= [1 1.5 2] linspace rand ( 3,4) = rand eye(3)= eye(2,3)= eye ones(3)= ones(2,3)= ones zeros(3)= zeros(2,3)= zeros Standard arrays
  • 16. N= 1 0 3 4 7 5 9 2 8 1 7 8 9 2 8 4 7 5 1 0 3 3 0 1 5 7 4 8 2 9 3 5 8 0 7 2 1 4 9 -0.3866 -0.0504 0.1765 -0.1092 0.1597 -0.0588 0.4622 0.0168 -0.0588 1 4 9 0 7 2 3 5 8 find (N ==7 ) = 5 find (N == 10 ) = [] find rot90(N)= fliplr ( N)= flipud ( N)= min(N) = [1 0 3] max(N) = [9 7 8] sum(N) = [14 9 16] rot90 fliplr flipud min max sum transpose ( ´ ) inv diag det length size transpose ( N)= diag(N)= det(N)=-119 length(N)=[3] size(N)=[3 3] Array functions
  • 17. Relational and Logical Operators logical exclusive or xor logical not ~ not logical or | or logical and & and Logical Operators greater than or equal >= ge less than or equal <= le greater than > gt less than < lt not equal ~= ne equal = = eq Comment Symbol Operator name Relational Operators
  • 18. Control Flow
    • for – end
          • for variable= expression
          • statements
          • end
    • example:
          • for i=1:10 for j=1:2:10
          • x(i) = i*2; y=j
          • end end
  • 19. Control Flow
    • while – end
          • x=0;
          • while (x<5)
          • x=x+s;
          • disp (x);
          • end
  • 20. Control Flow
    • if – else – elseif – end
          • a=6; % a=7; % a=10 ;
          • if (rem(a,3)==0)
          • a=a*3;
          • elseif (rem(a,2)==0)
          • a=a*2;
          • else
          • a=a*10;
          • end
          • disp (a);
  • 21. Control Flow
    • switch – case – otherwise – end
          • x= input (‘The value of x:’);
          • units= input (“Enter the unit of x: (Please Enter the unit between ‘ ‘) ’);
          • switch units
          • case (‘inch’,’in’)
          • y=x*2.54 ;
          • case (‘feet’,’ft’)
          • y=x*2.54*12 ;
          • case (‘meter’,’m’)
          • y=x*100 ;
          • case (‘millimeter’,’mm’)
          • y=x/10 ;
          • otherwise
          • disp (‘Unknown unit’);
          • end
  • 22. Control Flow
    • break
        • for i=1:10
        • if (i>5)
        • break ;
        • else
        • a=a*4.5;
        • end
        • end
        • disp (a);
  • 23. M-files
    • Scripts : Deal with the work space directly without inputs or outputs
    • Functions: Needs inputs and only the outputs are saved in the workspace.
    • examples:
      • function [output1,output2…]=function_name(input1,input2,…)
      • function function_name(input1,input2,…)
  • 24. M-files
    • Function or script calling is done by typing the file name.
    • The function file name must be the same as function name
    • Function name must be one word
    • Has a maximum of 31character size
    • Starting by a character
  • 25. M-files
    • Assume a function:
        • function [a,b,c]=my_function(x,y)
    • is called as
        • [b]=my_function(x,y,z);
        • [a,b,c]=my_function(u);
    • then
        • b = a value
        • x= u value
  • 26. Math functions rem ceil floor round fix Approximation functions sph2cart deg2rad rad2deg base2dec pol2cart cart2pol cart2sph bin2dec hex2dec dec2base dec2bin dec2hex conversion functions tanh sinh cosh acot asec rem ceil floor real imag conj angle abs cos abs complex functions acsc sort cot min sec max csc exp atan log10 asin log acos power tan mean sin sqrt triangular functions Basic functions
  • 27. Exercise
    • write a matlab program that sum a series begins by 2 and ends by 100
        • Use both for and while loops in separated m-files
    • write a switch – case program such that:
        • if 35>=S>20 display ‘ Hot Day’
        • if 20>=S>10 display ‘ Nice Day’
        • if 10>=S>=0 display ‘ Cold Day ‘
        • otherwise display ‘ Out of Range’
      • That S is the sum of the array M, and M is:
        • Random matrix of size 3×6 and has values between 0 and 2
        • A square matrix of size 3 and values between 1 and 9
        • A vector of equal elements =5 and size of 7
        • Identity matrix of size 10×10