Your SlideShare is downloading. ×
0
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
gmrit-cse
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

gmrit-cse

814

Published on

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
814
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
20
Comments
0
Likes
1
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. 01/12/2006 Department of Electrical & Electronics Engineering Introduction to MATLAB
  • 2. 01/12/2006 Department of Electrical & Electronics Engineering Outline <ul><ul><li>MATLAB Introduction </li></ul></ul><ul><ul><li>MATLAB elements </li></ul></ul><ul><ul><ul><ul><li>Types </li></ul></ul></ul></ul><ul><ul><ul><ul><li>variables </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Matrices </li></ul></ul></ul></ul><ul><ul><li>Loading ,Saving and Plotting </li></ul></ul><ul><ul><li>MATLAB Programming </li></ul></ul><ul><ul><li>Simulink </li></ul></ul>
  • 3. 01/12/2006 Department of Electrical & Electronics Engineering Welcome to MATLAB <ul><li>MATLAB stands for “Mat rix Lab oratory” </li></ul><ul><li>APPLICATIONS OF MATLAB: </li></ul><ul><ul><li>Mathematical Calculations </li></ul></ul><ul><ul><li>Data Analysis & Visualization </li></ul></ul><ul><ul><li>Software Development </li></ul></ul><ul><ul><li>Simulation </li></ul></ul>
  • 4. 01/12/2006 Department of Electrical & Electronics Engineering Getting Started Command-Window Workspace & Directory Command- History
  • 5. 01/12/2006 Department of Electrical & Electronics Engineering <ul><ul><li>Command Window </li></ul></ul><ul><ul><ul><li>type commands </li></ul></ul></ul><ul><ul><li>Current Directory </li></ul></ul><ul><ul><ul><li>View folders and m-files </li></ul></ul></ul><ul><ul><li>Workspace </li></ul></ul><ul><ul><ul><li>View program variables </li></ul></ul></ul><ul><ul><ul><li>Double click on a variable to see it in the Array Editor </li></ul></ul></ul><ul><ul><li>Command History </li></ul></ul><ul><ul><ul><li>view past commands </li></ul></ul></ul><ul><ul><ul><li>save a whole session using diary </li></ul></ul></ul>
  • 6. 01/12/2006 Department of Electrical & Electronics Engineering Basic MATLAB Commands clear all : clears workspace of all variables close all : closes all the figure windows plot (x,y) : plots vector “y” versus “x” % : used for Comments help : when used with command gives its syntax
  • 7. 01/12/2006 Department of Electrical & Electronics Engineering Basic Arithmetic Operators <ul><ul><li>+ Arithmetic addition </li></ul></ul><ul><ul><li>- Arithmetic subtraction </li></ul></ul><ul><ul><li>* Arithmetic multiplication </li></ul></ul><ul><ul><li>/ Arithmetic division </li></ul></ul><ul><ul><li>^ Exponent or power </li></ul></ul><ul><li>.* (element by element for arrays) </li></ul>
  • 8. 01/12/2006 Department of Electrical & Electronics Engineering Variables <ul><ul><li>No need for types. i.e., </li></ul></ul><ul><ul><li>All variables are created with double precision unless specified and they are matrices. </li></ul></ul><ul><ul><li>After these statements, the variables are 1x1 matrices with double precision </li></ul></ul>int a; double b; float c; Example: >>x=5; >>x1=2;
  • 9. 01/12/2006 Department of Electrical & Electronics Engineering <ul><ul><li>a vector x = [1 2 5 1] </li></ul></ul><ul><li>x = </li></ul><ul><li>1 2 5 1 </li></ul><ul><ul><li>a matrix x = [1 2 3; 5 1 4; 3 2 -1] </li></ul></ul><ul><li>x = </li></ul><ul><li>1 2 3 </li></ul><ul><li>5 1 4 </li></ul><ul><li>3 2 -1 </li></ul><ul><ul><li>transpose y = x’ y = </li></ul></ul><ul><li>1 </li></ul><ul><li>2 </li></ul><ul><li>5 </li></ul><ul><li>1 </li></ul>
  • 10. 01/12/2006 Department of Electrical & Electronics Engineering <ul><ul><li>a vector x = [1 2 5 1] </li></ul></ul><ul><li>x = </li></ul><ul><li>1 2 5 1 </li></ul><ul><ul><li>a matrix x = [1 2 3; 5 1 4; 3 2 -1] </li></ul></ul><ul><li>x = </li></ul><ul><li>1 2 3 </li></ul><ul><li>5 1 4 </li></ul><ul><li>3 2 -1 </li></ul><ul><ul><li>transpose y = x’ y = </li></ul></ul><ul><li>1 </li></ul><ul><li>2 </li></ul><ul><li>5 </li></ul><ul><li>1 </li></ul>Array, Matrix
  • 11. 01/12/2006 Department of Electrical & Electronics Engineering <ul><ul><li>t =1:10 </li></ul></ul><ul><li>t = </li></ul><ul><li>1 2 3 4 5 6 7 8 9 10 </li></ul><ul><ul><li>k =2:-0.5:-1 </li></ul></ul><ul><li>k = </li></ul><ul><li>2 1.5 1 0.5 0 -0.5 -1 </li></ul><ul><ul><li>B = [1:4; 5:8] </li></ul></ul><ul><li>x = </li></ul><ul><li>1 2 3 4 </li></ul><ul><li>5 6 7 8 </li></ul>Long Array, Matrix
  • 12. 01/12/2006 Department of Electrical & Electronics Engineering Generating Vectors from Functions <ul><ul><li>zeros(M,N) MxN matrix of zeros </li></ul></ul><ul><ul><li>ones(M,N) MxN matrix of ones </li></ul></ul><ul><ul><li>rand(M,N) MxN matrix of uniformly distributed random </li></ul></ul><ul><li>numbers on (0,1) </li></ul>x = zeros(1,3) x = 0 0 0 x = ones(1,3) x = 1 1 1 x = rand(1,3) x = 0.9501 0.2311 0.6068
  • 13. 01/12/2006 Department of Electrical & Electronics Engineering Matrix Index <ul><ul><li>The matrix indices begin from 1 (not 0 (as in C)) </li></ul></ul><ul><ul><li>The matrix indices must be positive integer </li></ul></ul>Given: A(-2), A(0) Error: ??? Subscript indices must either be real positive integers or logical. A(4,2) Error: ??? Index exceeds matrix dimensions.
  • 14. 01/12/2006 Department of Electrical & Electronics Engineering Concatenation of Matrices <ul><ul><li>x = [1 2], y = [4 5], z=[ 0 0] </li></ul></ul><ul><li>A = [ x y] </li></ul><ul><li>1 2 4 5 </li></ul><ul><li>B = [x ; y] </li></ul><ul><li>1 2 </li></ul><ul><li>4 5 </li></ul>C = [x y ;z] Error: ??? Error using ==> vertcat CAT arguments dimensions are not consistent.
  • 15. 01/12/2006 Department of Electrical & Electronics Engineering First, let's create a simple vector with 9 elements called a a = [1 2 3 4 6 4 3 4 5] a = 1 2 3 4 6 4 3 4 5 b = a + 2 b = 3 4 5 6 8 6 5 6 7 Notice how MATLAB requires no special handling of vector or matrix math. Basic matrix operation
  • 16. 01/12/2006 Department of Electrical & Electronics Engineering Creating a matrix Given A and B: Addition Subtraction Product Transpose
  • 17. 01/12/2006 Department of Electrical & Electronics Engineering Operators (Element by Element) .* element-by-element multiplication ./ element-by-element division .^ element-by-element power
  • 18. 01/12/2006 Department of Electrical & Electronics Engineering The Use of “ .” and “Element Operation” A = [1 2 3; 5 1 4; 3 2 1] A = 1 2 3 5 1 4 3 2 -1 x = A(1,:) x = 1 2 3 y = A(3 ,:) y= 3 4 -1 b = x .* y B=3 8 -3 c = x . / y c= 0.33 0.5 -3 d = x .^2 d= 1 4 9 K= x^2 Erorr: ??? Error using ==> mpower Matrix must be square. B=x*y Erorr: ??? Error using ==> mtimes Inner matrix dimensions must agree .
  • 19. 01/12/2006 Department of Electrical & Electronics Engineering eye identity matrix zeros matrix of zeros ones matrix of ones diag create or extract diagonals triu upper triangular part of a matrix tril lower triangular part of a matrix Rand randomly generated matrix hilb Hilbert matrix magic magic square Creating Matrices using in-built functions
  • 20. Basic Task: Plot the function sin(x) between 0≤x≤4π 01/12/2006 Department of Electrical & Electronics Engineering Scalar in-built functions >>x=linspace(0,4*pi,100); >>y=sin(x); >>plot(y)
  • 21. Plot the function e -x/3 sin(x) between 0≤x≤4π 01/12/2006 Department of Electrical & Electronics Engineering Scalar in-built functions Calculate e -x/3 of the x-array >>y1=exp(-x/3); Multiply the arrays y and y1 >>y2=y*y1; Multiply the arrays y and y1 correctly >>y2=y.*y1; >>plot(y2)
  • 22. 01/12/2006 Department of Electrical & Electronics Engineering Matrix in-built functions eig det size length rank Find Eigen values and eigenvectors determinant Size of an array Length of a vector rank fi nd indices of nonzero entries x = 2 * rand(1,5) y = x(find(x > 1)) Suppose we want vector that consists all values of x greater than 1
  • 23. 01/12/2006 Department of Electrical & Electronics Engineering plot (x ,y) plots vector y versus vector x Plotting in Matlab plot(y) plots the columns of y versus their index. plot(x ,y,s ) Where s is a character string made from one element from any or all the following 3 columns: b blue . point - solid g green o circle : dotted r red x x-mark -. dashdot c cyan + plus -- dashed Example : plot(x, y, ‘r+:') plots a red dotted line with a plus at each data point
  • 24. 01/12/2006 Department of Electrical & Electronics Engineering Plotting in Matlab - Examples x = -2.9:0.2:2.9; bar(x, exp(-x.*x)); x = -2.9:0.2:2.9; plot(x, exp(-x.*x));
  • 25. 01/12/2006 Department of Electrical & Electronics Engineering Plotting in Matlab x = -pi:pi/10:pi; y = tan(sin(x)) - sin(tan(x)); plot(x,y,'--rs','LineWidth',2,... 'MarkerEdgeColor','k',... 'MarkerFaceColor','g',... 'MarkerSize',10)
  • 26. 01/12/2006 Department of Electrical & Electronics Engineering x=0:0.25:10; stairs(x, sin(x)); x = 0:0.1:4; y = sin(x.^2).*exp(-x); stem (x,y) Plotting in Matlab - Examples
  • 27. 01/12/2006 Department of Electrical & Electronics Engineering More details of plot Plot (X1,Y1,S1,X2,Y2,S2,X3,Y3,S3,...) combines the plots defined by the (X,Y,S) triples, x = -pi:pi/10:pi; y = tan(sin(x)) - sin(tan(x)); y1=- sin(tan(x)) plot(x,y,'--rs',x,y1,'ob:')
  • 28. 01/12/2006 Department of Electrical & Electronics Engineering Programming in Matlab-Creating M-file Click to create a new M-File <ul><ul><li>Extension “.m” </li></ul></ul><ul><ul><li>A text file containing program to run </li></ul></ul>
  • 29. 01/12/2006 Department of Electrical & Electronics Engineering Flow control in Programming <ul><ul><li>if </li></ul></ul><ul><ul><li>for </li></ul></ul><ul><ul><li>while </li></ul></ul><ul><ul><li>break </li></ul></ul><ul><ul><li>… . </li></ul></ul>
  • 30. 01/12/2006 Department of Electrical & Electronics Engineering Control structure in Programming <ul><ul><li>If Statement Syntax </li></ul></ul><ul><li>if (Condition_1) </li></ul><ul><li>Matlab Commands </li></ul><ul><li>elseif (Condition_2) </li></ul><ul><li>Matlab Commands </li></ul><ul><li>elseif (Condition_3) </li></ul><ul><li>Matlab Commands </li></ul><ul><li>else </li></ul><ul><li>Matlab Commands </li></ul><ul><li>end </li></ul>Some Dummy Examples if ((a>3) & (b==5)) Some Matlab Commands; end ----------------------------------- if (a<3) Some Matlab Commands; elseif (b~=5) Some Matlab Commands; End
  • 31. 01/12/2006 Department of Electrical & Electronics Engineering While Loop Syntax while (condition) Matlab Commands end Dummy Example while ((a>3) & (b==5)) Some Matlab Commands; end Control structure
  • 32. 01/12/2006 Department of Electrical & Electronics Engineering <ul><ul><li>For loop syntax </li></ul></ul><ul><li>for i=Index_Array </li></ul><ul><li>Matlab Commands </li></ul><ul><li>end </li></ul>Some Dummy Examples for i=1:100 Some Matlab Commands; end -------------------------------------------- for j=1:3:200 Some Matlab Commands; end ------------------------------------------ for m=13:-0.2:-21 Some Matlab Commands; end Control structure
  • 33. 01/12/2006 Department of Electrical & Electronics Engineering
  • 34. 01/12/2006 Department of Electrical & Electronics Engineering Writing User defined functions <ul><ul><li>Functions are m-files which can be executed by specifying </li></ul></ul><ul><li>some inputs and supply some desired outputs. </li></ul><ul><ul><li>The code telling the Matlab that an m-file is actually a function is </li></ul></ul>function out1=functionname(in1) function out1=functionname(in1,in2,in3) function [out1,out2]=functionname(in1,in2) Note : You should write this command at the beginning of the m-file and you should save the m-file with a file name same as the function name

×