1 / 39

Matlab Programming for Engineers

Matlab Programming for Engineers. Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data Types Input/Output Functions Simulink Toolbox Important Toolboxes (if time is available). Dr. Bashir NOURI. Objectives:.

harriett
Download Presentation

Matlab Programming for Engineers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Matlab Programming for Engineers Introduction to MatlabMatlab Basics Branching Statements Loops User Defined Functions Additional Data Types Input/Output Functions Simulink Toolbox Important Toolboxes (if time is available) Dr. Bashir NOURI

  2. Objectives: • Variables and Arrays • Initializing Variables in MATLAB • Multidimensional Arrays • Subarrays • Special Values • Displaying Output Data • Data Files • Scalar and Array Operations • Hierarchy of Operations • Built-in MATLAB Functions • Introduction to Plotting • Debugging MATLAB Programs

  3. Variables and Arrays • The fundamental unit of data in any Matlab program is the Array • Any element of the array can be retrieved by its indexes • Arrays could be matrices or vectors (row or column) • Matlab variable is the region of memory containing an array • Matlab variable letter + (letter, number, _) • Variable type: • double precession (real, complex) • Character 'How are you?' a(3,2)

  4. Creating and initializing variables in Matlab • Initializing variables in assignment statements var = expression; var = 40i; var2 = var/5; var = [1 2 3 4];

  5. Creating and initializing variables in Matlab • Initializing variables in assignment statements a = [0 1+7];  a = [0 8] b = [ a(2) 7 a];  b = [8 7 0 8] c(2,3) = 5;  c = 0 0 0 0 0 5

  6. Creating and initializing variables in Matlab • Initializing variables with Shortcut Expression colon operator (:) first : incr : last Ex: x = 1:2:10;  x = [1 3 5 7 9] angles = (0.01:0.01:1)*pi; f = [1:4]';  if no incr is used  default incr=1 (') is the transpose operator. g = [1:4]; h = [g' g'];

  7. Creating and initializing variables in Matlab • Initializing variables with Built-in Function

  8. Creating and initializing variables in Matlab • Initializing variables with Keyboard Input my_val = input('Enter an input value: '); v1 = input('Enter data: '); v2 = input('Enter data: ', 's');

  9. Multidimensional Arrays

  10. Multidimensional Arrays • Storing Multidimensional Array in Memory • Accessing this array with one dimension index Array as introduced How the array is stored in computer memory

  11. Subarrays

  12. Subarrays • The end Function (highest value of a given subscript)

  13. Subarrays Left-hand side Subarrays assignment statement • Assigning a scalar to a subarray

  14. Special Values

  15. Displaying Output Data Default Matlab Format == Four Digits After the Decimal Point

  16. Displaying Output Data (NUMBER FORMAT)

  17. Displaying Output Data (disp and fprintf functions)

  18. Data Files (save) save filename var1 var2 var3 *.mat save filename var1 var2 var3 -ascii text format (ascii)

  19. Data Files (save) save filename var1 var2 var3 *.mat save filename var1 var2 var3 -ascii text format (ascii)

  20. Data Files (load) load filename.mat var1 var2 (*.mat file format, loads specific variables) load filename.* (any other format, loads all the file data in the file must have array structure)

  21. Scalar and Array Operations

  22. Scalar and Array Operations Very important

  23. Scalar and Array Operations Matrix left division (a\b)

  24. Hierarchy of Operations Not the same! Distance = 0.5 * acceleration* (time^2) Distance = (0.5 * acceleration* time)^2

  25. Class Work

  26. Built-in Matlab Functions 1 . Optional results 2. Most of functions accept array inputs Optional

  27. Built-in Matlab Functions (Common Functions)

  28. Built-in Matlab Functions (Common Functions)

  29. Plotting in Matlab

  30. Printing a Plot / Exporting a Plot as a Graphical Image >> print <options> <filename> >> help print (to see print options) If no filename is specified, plot is printed on the printer

  31. Printing a Plot / Exporting a Plot as a Graphical Image

  32. Multiple plots Plot the following functions on the same figure f(x) = sin(2x) and its first derivative over x = 0:pi/100:2*pi

  33. Multiple plots Plot the following functions on the same figure f(x) = sin(2x) and its first derivative over x = 0:pi/100:2*pi

  34. Line Color, Line Style, Marker Style, and Legends

  35. Line Color, Line Style, Marker Style, and Legends Line Color, Line Style, and Marker Style:

  36. Line Color, Line Style, Marker Style, and Legends Legends: >> legend off (removes the legend from the plot) >> legend('string1' , 'string1' , … ,'location', 'value from table') Be aware of the version of Matlab you are using, see help legend Limits of plot axes

  37. Logarithmic Scale >> plot >> semilogx >> semilogy >> loglog

  38. Example

  39. Home Work Solve the following problems 2. [9, 10 , 11, 16]

More Related