1 / 30

Intro to Matlab

Can be found at: http://www.cs.unc.edu/~kim/matlab.ppt. Intro to Matlab. Using scalar variables Vectors, matrices, and arithmetic Plotting Solving Systems of Equations. New Class--for Engineers. ENGR196.3 SPECIAL TOPICS: INTRODUCTION TO MATLAB

kourtney
Download Presentation

Intro to Matlab

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. Can be found at: http://www.cs.unc.edu/~kim/matlab.ppt Intro to Matlab Using scalar variables Vectors, matrices, and arithmetic Plotting Solving Systems of Equations

  2. New Class--for Engineers • ENGR196.3 • SPECIAL TOPICS: INTRODUCTION TO MATLAB • Description:Fundamentals of MATLAB programming applied to problems in science and mathematics. Solving systems of equations, basic scripting, functions, vectors, data files, and graphics. (Credit course for grade or CR/NC)

  3. Drawbacks: Slow (execution) compared to C or Java Advantages: Handles vector and matrices very nice Quick plotting and analysis EXTENSIVE documentation (type ‘help’) Lots of nice functions: FFT, fuzzy logic, neural nets, numerical integration, OpenGL (!?) One of the major tools accelerating tech change Why use Matlab?

  4. Click on Help>Full Product Family Help: Check out Fuzzy Logic Genetic Algorithms Symbolic Math A tour of Matlab’s features

  5. Scalars • The First Time You Bring Up MATLAB • MATLAB as a Calculator for Scalars • Fetching and Setting Scalar Variables • MATLAB Built-in Functions, Operators, and Expressions • Problem Sets for Scalars

  6. 3-1 The First Time You Bring Up MATLAB Basic windows in MATLAB are: • Command - executes single-line commands • Workspace - keeps track of all defined variables • Command History - keeps a running record of all single line programs you have executed • Current Folder - lists all files that are directly available for MATLAB use • Array Editor - allows direct editing of MATLAB arrays • Preferences - for setting preferences for the display of results, fonts used, and many other aspects of how MATLAB looks to you

  7. 3-2 MATLAB as a Calculator for Scalars • A scalar is simply a number… • In science the term scalar is used as opposed to a vector, i.e. a magnitude having no direction. • In MATLAB, scalar is used as opposed to arrays, i.e. a single number. • Since we have not covered arrays (tables of numbers) yet, we will be dealing with scalars in MATLAB.

  8. 3-3 Fetching and Setting Scalar Variables • Think of computer variables as named containers. • We can perform 2 types of operations on variables: • we can set the value held in the container: x = 22 • we can look at the value held in the container: x

  9. The Assignment Operator (=) • The equal sign is the assignment operator in MATLAB. >> x = 22 places number 22 in container x • How about: >> x = x + 1 • Note the difference between the equal sign in mathematics and the assignment operator in MATLAB!

  10. Useful Constants • Inf infinity • NaN Not a number (div by zero) • eps machine epsilon • ans most recent unassigned answer • pi 3.14159…. • i and j Matlab supports imaginary numbers!

  11. Using the Command History Window

  12. 3-4 MATLAB Built-in Functions, Operators, and Expressions • MATLAB comes with a large number of built-in functions (e.g.. sin, cos, tan, log10, log, exp) • A special subclass of often-used MATLAB functions is called operators • Assignment operator (=) • Arithmetic operators (+, -, *, /, ^) • Relational operators (<, <=, = =, ~=, >=, >) • Logical operators (&, |, ~)

  13. Example – Arithmetic Operators Hint: the function exp(x) gives e raised to a power x

  14. Example – Relational and Logical Operators

  15. Vector Operations Chapter 5

  16. Vector Operations • Vector Creation • Accessing Vector Elements • Row Vectors and Column Vectors, and the Transpose Operator • Vector Built-in Functions, Operators, and Expressions

  17. Vectors and Matrices • Can be to command line or from *.m file scalar: x = 3 vector: x = [1 0 0] 2D matrix: x = [1 0 0; 0 1 0; 0 0 1] arbitrarily higher dimensions (don’t use much) • Can also use matrices / vectors as elements: • x = [1 2 3] • y = [ x 4 5 6]

  18. 2-D Plotting and Help in MATLAB Chapter 6

  19. 2-D Plotting and Help in MATLAB • Using Vectors to Plot Numerical Data • Other 2-D plot types in MATLAB • Problem Sets for 2-D Plotting

  20. 6-2 Using Vectors to Plot Numerical Data • Mostly from observed data - your goal is to understand the relationship between the variables of a system. • Determine the independent and dependent variables and plot: speed = 20:10:70; stopDis = [46,75,128,201,292,385]; plot(speed, stopDis, '-ro') % note the ‘-ro’ switch • Don’t forget to properly label your graphs: title('Stopping Distance versus Vehicle Speed', 'FontSize', 14) xlabel('vehicle speed (mi/hr)', 'FontSize', 12) ylabel('stopping distance (ft)', 'FontSize', 12) grid on

  21. Sample Problem – Plotting Numerical Data

  22. 3D Plotting • 3D plots – plot an outer product x = 1:10 y = 1:10 z = x’ * y mesh(x,y,z) Single quote ‘ means transpose (can’t cut and paste though…)

  23. IF block if (<condition>) <body> elseif <body> end WHILE block while (<condition>) <body> end Flow Constructs Conditions same as C, ( ==, >=, <=) except != is ~=

  24. FOR block for i = 1:10 <body> end SWITCH statement switch <expression> case <condition>, <statement> otherwise <condition>, <statement> end More Flow Constructs

  25. Other Language Features • Matlab language is pretty sophisticated • Functions Stored 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; • Even has try and catch (never used them)

  26. Solving Systems of Equations • Consider a system of simultaneous equations 3x + 4y + 5z = 32 21x + 5y + 2z = 20 x – 2y + 10z = 120 • A solution is a value of x, y, and z that satisfies all 3 equations • In general, these 3 equations could have 1 solution, many solutions, or NO solutions

  27. Using Matlab to Solve Simultaneous Equations • Set up the equation in matrix/vector form: A = [3 4 5; 21 5 2; 1 -2 10] u = [ x y z]’ b = [ 32 20 120]’ In other words, A u = b (this is linear algebra) = *

  28. The solution uses matrix inverse • If you multiply both sides by 1/A you get u = 1/A * b • In the case of matrices, order of operation is critical (WRONG: u = b/A ) • SO we have “Left division” u = A \ b (recommended approach) • OR use inv( ) function: u = inv(A) * b

  29. The solution >> u = A\b u = 1.4497 ( value of x) -6.3249 ( value of y) 10.5901 ( value of z) • You can plug these values in the original equation test = A * u and see if you get b

  30. Caution with Systems of Eqs • Sometimes, Matrix A does not have an inverse: • This means the 3 equations are not really independent and there is no single solution (there may be an infinite # of solns) • Take determinant det(A) if 0, it’s singular = *

More Related