Introduction to MATLAB

6,614 views

Published on

Please Download and view it. Presentation on MATLAB

Published in: Education, Technology
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
6,614
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
859
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

Introduction to MATLAB

  1. 1. Do you speak MATLAB!
  2. 2. MATLAB ® R2012a (7.14)MathWorks®
  3. 3. MATLAB Is an integrated technical computingenvironment that combines numeric computation, advancedgraphics and visualization, and a high-level programming language. The MathWorks, Inc.High-level language of technical computingDevelopment environment for engineers, scientistsInteractive tools for design, problem solvingMathematical function librariesGraphics and data visualization toolsCustom GUIsExternal Interfaces: C, C++, Fortran, Java, COM, Excel, .NET Introducing MATLAB – Language of technical computing 2
  4. 4. MATLAB product family MATLAB MATLAB Programming Language User-written Functions Built-in FunctionsGraphics Computations External Interfaces2D Graphics Linear Algebra C3D Graphics Data Analysis C++Color & Lighting Polynomials JAVAAnimation Interpolations .netImage, Audio & Video Solutions of ODE Micorosoft ExcelToolboxes: Collections of specialized FunctionsParallel Computing Parallel Computing Toolbox ToolboxParallel Computing Spreadsheet Link EX (for Microsoft Excel) Symbolic Math Toolbox MATLAB Compiler Computational Finance Financial Optimization Computational Finance ToolboxMath, Statistics, and OptimizationDistributed Toolbox Toolbox Bioinformatics ToolboxMath, Data Acquisition Computing Server Statistics, and Optimization MATLAB MATLAB Builder NE (for Equation Toolbox Partial System Toolbox Econometrics Global Optimization Toolbox Biology Fixed-Income Toolbox Computational InstrumentControlDifferential Microsoft .NETRobustFixed-Point Toolbox and Verification Framework) Image Processing Toolbox Datafeed Toolbox Toolbox Signal Control Toolbox Control MATLAB Coder SimBiology Generation CodeControl System Design and Analysis StatisticsMATLAB Builder JA (for Java language) Network Toolbox Financial Derivatives Toolbox Toolbox System Identification System Coder Neural Image Acquisition ToolboxVision ToolboxToolbox RF ToolboxControl Toolbox Model Predictive Application Deployment Computer Toolbox HDLDatabase Toolbox DSP SystemSignal Processing and Communications Curve Fitting Toolbox EX Toolbox Database (for Aerospace Toolbox Calibration Toolbox Model-Based MATLAB Builder HDL Microsoft Excel) Array System OPC VisionFuzzy Logic Toolbox ToolboxVerifier Database Connectivity and ReportingImage Processing and Computer Toolbox Image AcquisitionSystem Toolbox Database ConnectivityToolbox Communications Phased and ReportingTest and MeasurementTest and Measurement Wavelet Toolbox Generator MATLAB Report Vehicle Network Spreadsheet Link EX (for Microsoft Excel) ToolboxFilter Design HDL Coder Mapping Toolbox Introducing MATLAB – Language of technical computing 3
  5. 5. Agenda Introducing MATLAB – Language of technical computing Minimal MATLAB 4
  6. 6. Agenda Introducing MATLAB – Language of technical computing Minimal MATLAB MATLAB Desktop Computing with MATLAB Array – creation & working Plotting – creating & visualizing Automating commands – creating, saving & executing script file Conditional control statements Loop control statements Functions – creating & executing a function file 5
  7. 7. Agenda Introducing MATLAB – Language of technical computing Minimal MATLAB MATLAB Desktop Computing with MATLAB Array – creation & working Plotting – creating & visualizing Automating commands – creating, saving & executing script file Conditional control statements Loop control statements Functions – creating & executing a function file 6
  8. 8. MATLAB - Desktop CurrentWindow Editor Directory Command Window Command History WorkspaceAll commandsall youron edit, create, and save your own scriptfar along within files typeThe is where you characterizedthe current directory are listed. programs there called typed files from that you have command promptThis main windowwrite, variablesby the MATLABcommand window get recorded, even subwindow lists all MATLAB prompt in the generated so or (>>)m-files.across multiple session in this window.and size. Minimal MATLAB 7
  9. 9. MATLAB - Desktop Help WindowThe main window characterized by the MATLAB command prompt (>>) Minimal MATLAB 8
  10. 10. Agenda Introducing MATLAB – Language of technical computing Minimal MATLAB MATLAB Desktop Computing with MATLAB Array – creation & working Plotting – creating & visualizing Automating commands – creating, saving & executing script file Conditional control statements Loop control statements Functions – creating & executing a function file 9
  11. 11. Computing with MATLABBasic Data and Variables Variable Value Size Class Minimal MATLAB 10
  12. 12. Computing with MATLAB Basic Data and Variables Multidimensional Array Scalar [full or sparse] functionlogical char numeric cell struct handle (@)  ab { }int8, uint8, single doubleint16, unit16,int32, unit32,int64, unit64 Minimal MATLAB 11
  13. 13. Computing with MATLABArithmetic Operator &Their Precedence Operations Operators Examples Addition Subtraction Multiplication Right Division Left Division Exponentiation Precedence Order Operators 1 Parentheses ( ). For nested parentheses, the innermost are executed first. 2 Exponentiation, ^ 3 Multiplication, *; Division, /, 4 Addition, +; Subtraction, - help precedence Minimal MATLAB 12
  14. 14. Computing with MATLABMathematical Functions Trigonometry Radian Degree Basic Hyperbolic Inverse Basic sin sinh asin sind cos cosh acos cosd tan tanh atan tand cot coth acot cotd sec sech asec secd csc csch acsc cscd Exponential Complex Discrete Math Matrix exp abs factorial det log conj gcd trace log10 imag lcm rank log2 real primes inv sqrt angle expm sqrtm help <type function name> Minimal MATLAB 13
  15. 15. Computing with MATLABPredefined Variables andKeywordsPredefined Variablesanspiepsinfi,jNaNKeywordsbreak case catch classdef continue else elseifend for function global if otherwise parforpersistent return spmd switch try while help <Predefined Variable Name> iskeyword Minimal MATLAB 14
  16. 16. Computing with MATLABManaging VariablesCommand Outcomeclear Clears the workspace, all variables are removed from the memoryclear x y z Clears only variables x, y, and zclear all Clears all variables and functions from workspacewho Lists variables currently in the workspacewhos Lists variables currently in the workspace with their sizes together with information about their bytes and class Minimal MATLAB 15
  17. 17. Agenda Introducing MATLAB – Language of technical computing Minimal MATLAB MATLAB Desktop Computing with MATLAB Array – creation & working Plotting – creating & visualizing Automating commands – creating, saving & executing script file Conditional control statements Loop control statements Functions – creating & executing a function file 16
  18. 18. ArrayOverview 5 7 5 5 7 4 4 Scalar of 1 X 1 Row Vector of 1 X 3 Column vector of 3 X 1 5 14 12 10 56 5 14 12 10 56 5 10 56 6 10 504 4 6 5 14 12 10 56 6 6 4 6 5 10 56 85 23 89 0 5 10 56 85 23 89 0 7 78 86 6 4 6 23 2 6 4 6 23 2 0 7 78 86 53 6 7 78 86 53 6 One Dimensional Matrix of 3 X 3 Two Dimensional Three Dimensional Matrix of 3 X 5 Matrix of 3 X 5 Minimal MATLAB 17
  19. 19. Array Creation from a known list of numbers with constant spacing by specifying the first term, the spacing, and the last termKnown lists of numbersOne Dimensional Array variable_name = [ type vector elements ]Two Dimensional Array variable_name = [1st row elements; 2nd row elements; 3rd row elements; ... ; last row elements] Minimal MATLAB 18
  20. 20. Array CreationConstant spacing : (colon operator)Creating a vector with constant spacing by specifying the m = first termfirst term, the spacing, and the last term: q = spacing n = last term variable_name = [m:q:n] or variable_name = m:q:nConstant spacing linspaceCreating a vector with linear (equal) spacing by specifying xi = first elementthe first and last terms, and the number of terms: xf = Last element variable_name = linspace(xi, xf, n) n = number of elements Minimal MATLAB 19
  21. 21. ArrayArray Operators Operations Operation + Addition - Subtraction .* Element-by-element multiplication ./ Element-by-element division . Element-by-element left division .^ Element-by-element power ‘ Transpose Minimal MATLAB 20
  22. 22. ArrayData ManipulationAddressing - Extraction (Subset)Addressing an element(s) individually or in subgroups: To redefine only some of the elements, When specific elements are to be used in calculations, When a subgroup of the elements is used to define a new variable. Minimal MATLAB 21
  23. 23. Array Data Manipulation Address vector>> ve = [35 46 78 23 5 14 81 3 55]ve = 35 46 78 23 5 14 81 3 55 1 2 3 4 5 6 7 8 9 position or index of element Minimal MATLAB 22
  24. 24. ArrayData Manipulation Address matrix ) (1,1) (1,2) (1,3) (1,4) 3 11 6 5 3 11 6 5 (2,1) (2,2) (2,3) (2,4) MAT MAT = = 4 7 10 2 4 13 79 0 10 8 2 (3,1) (3,2) (3,3) (3,4) 13 9 0 8 Minimal MATLAB 23
  25. 25. ArrayData Manipulation Addressing vector : (colon operator) Addressing matrix : (colon operator)A(:,n) Refers to the elements in all the rows of column n of the matrix A.A(n,:) Refers to the elements in all the columns of row n of the matrix A.A(:,m:n) Refers to the elements in all the rows between columns m and n of the matrix A. Refers to the elements in all the columns between rows m and nA(m:n,:) of the matrix A. Refers to the elements in rows m through n and columns pA(m:n,p:q) through q of the matrix A. Minimal MATLAB 24
  26. 26. Agenda Introducing MATLAB – Language of technical computing Minimal MATLAB MATLAB Desktop Computing with MATLAB Array – creation & working Plotting – creating & visualizing Automating commands – creating, saving & executing script file Conditional control statements Loop control statements Functions – creating & executing a function file 25
  27. 27. PlottingCreationExample Damped Oscillation equation: Minimal MATLAB 26
  28. 28. PlottingCreationExample Damped Oscillation equation: 1- a = 0.1; b = 1; 2- t = 0:0.1:20; 3- y = exp(-a*t).*cos(b*t); 4- plot(t,y) 5- grid on; 6- xlabel(Time in second); 7- ylabel(Response in mm); 8- title(Damped Oscillation); 9- legend(a = 0.1,b = 1)); Minimal MATLAB 27
  29. 29. PlottingCreationOverview title legend ylabel xlabel Minimal MATLAB 28
  30. 30. PlottingGraphingPlot function plot(x,y)where the variables x and y are vector and must be of same size i.e the number ofelements should be equal. x=[1 2 3 5 7 7.5 8 10]; y=[2 6.5 7 7 5.5 4 6 8]; plot(x,y) Minimal MATLAB 29
  31. 31. Plotting Graphing Plot functionplot(x,y,‘line specifiers’,‘PropertyName’,PropertyValue) Line specifiers Line specifiers are optional and can be used to define the style and color of the line and the type of markers (if markers are desired). Line Style Specifier solid (default) - dashed -- dotted : dash-dot -. Line color Specifier Line color Specifier red r magenta m green g yellow y blue b black k cyan c white w Minimal MATLAB 30
  32. 32. Plotting Graphing Plot functionplot(x,y,‘line specifiers’,‘PropertyName’,PropertyValue) Line specifiers Line specifiers are optional and can be used to define the style and color of the line and the type of markers (if markers are desired). Marker type Specifier Marker type Specifier plus sign square s circle diamond d asterisk five pointed star p point six pointed star h cross triangle (pointed left) < triangle (pointed up) triangle (pointed right) > triangle (pointed down) Minimal MATLAB 31
  33. 33. Plotting Graphing Plot functionplot(x,y,‘line specifiers’,‘PropertyName’,PropertyValue) Property Name To specify the thickness of the line, the size of the marker, and the colors of the marker’s edge line and fill Minimal MATLAB 32
  34. 34. Agenda Introducing MATLAB – Language of technical computing Minimal MATLAB MATLAB Desktop Computing with MATLAB Array – creation & working Plotting – creating & visualizing Automating commands – creating, saving & executing script file Conditional control statements Loop control statements Functions – creating & executing a function file 33
  35. 35. ScriptCreationOverview Script Sequence of MATLAB statements in fileA script file is an external file that contains a sequence of MATLAB statements. Scriptfiles have a filename extension of .m. Minimal MATLAB 34
  36. 36. ScriptBasicsCreating script fileSaving script fileExecuting script fileAccepting input from user variable_name = input(‘prompt message’)Displaying output to user disp(name of a variable) disp(‘text as string’) Minimal MATLAB 35
  37. 37. Agenda Introducing MATLAB – Language of technical computing Minimal MATLAB MATLAB Desktop Computing with MATLAB Array – creation & working Plotting – creating & visualizing Automating commands – creating, saving & executing script file Conditional control statements Loop control statements Functions – creating & executing a function file 36
  38. 38. ScriptCreationConditional StatementConditional expression consisting of relational and/or logical operators. Relational Operators Logical OperatorsOperations Operators Operations OperatorsLess than AndGreater than OrLess than or equal to NotGreater than or equal toEqual toNot Equal to Minimal MATLAB 37
  39. 39. ScriptCreationExample g1 Plot g2 g31- t = linspace(-5,10);2- g1 = -4 - 2*t; g1 g2 = -4 + 3*t; g23- g3 = 16 - 2*t; g34- g = g1.*(-2<t & t<=0)...5- + g2.*(0<t & t<=4)...6- + g3.*(4<t & t<=8);7- plot(t,g) Minimal MATLAB 38
  40. 40. Scriptif/elseif/else Statement if expression False if statements statement elseif expression statements else True statements end Statement Statement Group 2 Group 1 End Minimal MATLAB 39
  41. 41. Script Creation Switch Statement Flowchart switch switch_expression case case_expression statements True match False case case_expression case, k statements : otherwise statements case case False endstatement, k end True Minimal MATLAB 40
  42. 42. Agenda Introducing MATLAB – Language of technical computing Minimal MATLAB MATLAB Desktop Computing with MATLAB Array – creation & working Plotting – creating & visualizing Automating commands – creating, saving & executing script file Conditional control statements Loop control statements Functions – creating & executing a function file 41
  43. 43. ScriptLoop control statementsfor (Iterative Loop) for index = start:increment:end statements endwhile (Conditional Loop) while expression statements endcontinue, break Minimal MATLAB 42
  44. 44. Agenda Introducing MATLAB – Language of technical computing Minimal MATLAB MATLAB Desktop Computing with MATLAB Array – creation & working Plotting – creating & visualizing Automating commands – creating, saving & executing script file Conditional control statements Loop control statements Functions – creating & executing a function file 43
  45. 45. Function Creation Overview Set of inputs Set of outputs 1 SYSTEM 1 2 [Mathematical model of 2 3 3 Physical Phenomenon] m (function file) n[Input variables] [System] [Output variables]current, voltage, Electrical Networks current, voltage,power, energy, Communication System power, energy,temperature, Control System temperature,data Thermodynamic System Processed data Computer system, etc function [out1, out2, ...] = func_name(in1, in2, ...) declares the function func_name, and its inputs and outputs Minimal MATLAB 44
  46. 46. Function Creation Example X Black Box Y 1- function H = lp(R,C) 2- w = logspace(-2,6); H = 1./sqrt(1 + (w*R*C).^2); 3- semilogx(w,H) 4- xlabel(Angular frquency, w (rad/s)); 5- ylabel(System Response); 6- title(Low-Pass frequency response);Source: MATLAB An Introduction with Applications, Amos Gilat, 4e Minimal MATLAB 45
  47. 47. FunctionCreationExample Minimal MATLAB 46
  48. 48. Function Creation Example Black BoxSource: MATLAB An Introduction with Applications, Amos Gilat, 4e Minimal MATLAB 47
  49. 49. Function Creation Example1- function [Td, RH] = DewptRhum(T,Tw,BP)2- es = 6.112*exp((17.67*T)/(T + 243.5)); ew = 6.112*exp((17.67*Tw)/(Tw + 243.5));3- e = ew - BP*(T - Tw)*0.00066*(1 + 0.00115*Tw);4- RH = 100*(e/es);5- Td = (243.5*log(e/6.112))/(17.67 - log(e/6.112)); Minimal MATLAB 48
  50. 50. BEFORELEAVING
  51. 51. More on MATLAB1. MATLAB Tutorials2. Demo and Webinars3. Documentations4. MATLAB Central (User Community) www.mathworks.com
  52. 52. Reference1. Getting started with MATLAB, A quick introduction for scientists and engineers, Rudra Pratap, Indian edition2. MATLAB An Introduction with Applications, Amos Gilat, 4e3. Matlab - A Practical Introduction to Programming and Problem Solving, Stormy Attaway, 2e4. Essential MATLAB for Engineers and Scientists, Brian D. Hahn & Daniel T. Valentine5. A Guide to MATLAB for Beginners and Experienced Users, Hunt Lipsman & Rosenberg
  53. 53. THANKS We speak MATLAB!ASHISH MESHRAMmeetashish85@gmail.comwww.facebook.com/ashishmeet

×