1 / 79

Introduction to MATLAB

Introduction to MATLAB. Zongqiang Liao Research Computing Group UNC-Chapel Hill. Course outline. Introduction Getting started Mathematical functions Matrix generation Matrix and array operations Reading and writing data files Basic plotting Basic programming. Introduction.

kennan
Télécharger la présentation

Introduction 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. Introduction to MATLAB Zongqiang Liao Research Computing Group UNC-Chapel Hill

  2. Course outline • Introduction • Getting started • Mathematical functions • Matrix generation • Matrix and array operations • Reading and writing data files • Basic plotting • Basic programming

  3. Introduction

  4. Introduction • The name MATLAB stands for MATrix LABoratory • It is good at dealing with matrices • Vendor’s website: http//:www.mathworks.com • Advantages of MATLAB • Easiness of use • Powerful build-in routines and toolboxes • Good visualization of results • Disadvantage of MATLAB • Can be slow

  5. Getting started

  6. Getting started • MATLAB desktop • The Command Window • The Command History • The Workspace • The Current Directory • The Help Browser • The Start Button

  7. Getting started • Keeping track of your work session • diary command >> diary or >> diary FileName • Stop the recording >> diary off • Start the recording again >>diary on

  8. Getting started • Using MATLAB as a calculator >> 1+2*3 ans = 7 • You may assign the value to a output variable >> x=1+2*3 x= 7 • x can be used in the some calculation later >> 4*x ans = 28

  9. Getting started • Suppressing output • You can suppress the numerical output by putting a semicolon (;) at the end of the line >> t=-13; • We can place several statements on one line, separated by commas (,) or semicolons(;) >> t=-13; u=5*t, v=t^2+u u= -65 v= 104

  10. Getting started • Managing the workspace • The results of one problem may have an effect on the next one • Issue a clear command at the start of each new independent calculation >> clear or >> clear all

  11. Getting started • Miscellaneous commands • To clear the Command Window >> clc • To abort a MATLAB computation ctrl-C • To continue a line …

  12. Getting started • Getting help • Use help to request info on a specific function >> help sqrt • Use doc function to open the on-line version of the help menu >> doc plot • Use lookfor to find function by keywords >> lookfor functionkeyword

  13. Mathematical functions

  14. Mathematical functions • Lists of build-in mathematical functions • Elementary functions >> help elfun • Special functions >> help specfun • Such as sin(x), cos(x), tan(x), ex, ln(x)

  15. Mathematical functions • Example 1 Calculate z=e-asin(x)+10 for a=5, x=2, y=8 >> a=5; x=2; y=8; >> z=exp(-a)*sin(x)+10*sqrt(y) z= 28.2904

  16. Mathematical functions • Example 2 Calculate the roots of a equation ax2+bx+c=0, for a=2, b=1, and c=-4 >> a=2; b=1; c=-4; >> x1=(-b+sqrt(b^2-4*a*c))/(2*a) x1= 1.1861 >> x2=(-b-sqrt(b^2-4*a*c))/(2*a) x2= -1.1861

  17. Mathematical functions • Example 3 >> log(142) ans= 4.9558 >> log10(142) ans= 2.1523

  18. Mathematical functions • Example 4 Calculate sin( /4) >> sin(pi/4) ans = 0.7071 Calculate e10 >> exp(10) ans = 2.2026e+004

  19. Matrix generation

  20. Matrix generation • The name MATLAB is taken from ”MATrix LABoratory.” It is good at dealing with matrices. • Actually all variables in MATLAB are matrices. • Scalars are 1-by-1 matrices • vectors are N-by-1 (or 1-by-N) matrices. • You can see this by executing >> size(x)

  21. Matrix generation • Entering a matrix • Begin with a square bracket, [ • Separate elements in a row with spaces or commas (,) • Use a semicolon (;) to separate rows • End the matrix with another square bracket, ]

  22. Matrix generation • Entering a matrix: A typical example >> A=[1 2 3; 4 5 6; 7 8 9] >> A= 1 2 3 4 5 6 7 8 9

  23. Matrix generation • Matrix indexing • View a particular element in a matrix • For example, A(1,3) is an element of first row and third column >>A(1,3) >>ans = 3

  24. Matrix generation • Colon operator in a matrix • Colon operator is very useful in the usage of MATLAB • For example, A(m:n,k:l) specifies portions of a matrix A: rows m to n and column k to l.

  25. Matrix generation • Colon operator in a matrix • Example 1 Rows 2 and 3 and columns 2 and 3 of matrix A >>A(2:3, 2:3) ans = 5 6 8 9

  26. Matrix generation • Colon operator in a matrix • Example 2 Second row element of matrix A >>A(2, :) ans = 4 5 6

  27. Matrix generation • Colon operator in a matrix • Example 3 Last two columns of matrix A >>A(:, 2:3) ans = 2 3 5 6 8 9

  28. Matrix generation • Colon operator in a matrix • Example 4 Last rows of matrix A >>A(2:end, :) ans = 4 5 6 7 8 9 The end here denotes the last index in the specified dimension

  29. Matrix generation • Transposing a matrix • The transposing operation is a single quote (’) >>A’ ans = 1 4 7 2 5 8 3 6 9

  30. Matrix generation • Concatenating matrices • Matrices can be made up of sub-matrices >>B= [A 10*A; -A [1 0 0; 0 1 0; 0 0 1]] B = 1 2 3 10 20 30 4 5 6 40 50 60 7 8 9 70 80 90 -1 -2 -3 1 0 0 -4 -5 -6 0 1 0 -7 -8 -9 0 0 1

  31. Matrix generation • Generating vectors: colon operator • Suppose we want to enter a vector x consisting of points (0, 0.1, 0.2, 0.3,…,5) >>x=0:0.1:5; • All the elements in between 0 and 5 increase by one-tenth

  32. Matrix generation • Generating vectors: linear spacing • Suppose we want to have direct control over the number of points. >>y=linspace(a, b, n) For example, >>theta=linspace(0, 2*pi, 101) Creates a vector of 101 elements in the interval

  33. Matrix generation • Elementary matrix generators • eye(m,n) • eye(n) • zeros(m,n) • ones(m,n) • diag(A) • rand(m,n) • randn(m,n) • logspace(a,b,n) • For a complete list of elementary matrices >>help elmat >>doc elmat

  34. Matrix arithmetic operation

  35. Matrix arithmetic operation • Arithmetic operations • A+B or B+A • A*B • A^2 or A*A • a*A or A*a

  36. Matrix arithmetic operation • Matrix functions • det • diag • eig • inv • norm • rank

  37. Matrix arithmetic operation • Matrix functions • For example >> A=[1 2 3; 4 5 6; 7 8 0]; >>inv(A) ans = -1.7778 0.8889 -0.1111 1.5556 -0.7778 0.2222 -0.1111 0.2222 -0.1111 >>det(A) ans = 27

  38. Matrix arithmetic operation • More matrix operations • Calculate the sum of elements in the second row of matrix A >> sum(A(2,:)) • Calculates the sum of the last column of A >>sum(A(:,end))

  39. Array arithmetic operation

  40. Array arithmetic operation • Array operations • Array operations are done element-by-element • The period character (.) is used in array operations • The matrix and array operations are the same for addition (+) and subtraction (-)

  41. Array arithmetic operation • Array operations If A and B are two matrices of the same size with elements A=[aij] and B=[bij] • C=A.*B produces a matrix C of the same size with elements cij= aijbij • C=A./B produces a matrix C of the same size with elements cij= aij/bij • C=A.^2 produces a matrix C of the same size with elements cij= aij2

  42. Array arithmetic operation • Array operations • Example 1 A= B= >>C=A.*B C= 10 40 90 160 250 360 490 640 810

  43. Array arithmetic operation • Array operations • Example 2 >>C=A.^2 C= 1 4 9 16 25 36 49 64 81

  44. Reading and writing data files

  45. Reading and writing data files • Save and load data file • Use command save to save the variable in the workspace • For example, use command save: >> x = [1 3 -4]; >> y = [2 -1 7]; >> z = [3 2 3]; >> save Filename.mat The command saves all variables in the workspace into a binary file Filename.mat

  46. Reading and writing data files • Save and load data file • Save only certain variables by specifying the variable names after the file name >> save Filename.mat x y • Save variables into ASCII data file >> save Filename.dat x y –ascii

  47. Reading and writing data files • Save and load data file • The data can be read back with the load command >> load Filename.mat • Load only some of the variables into memory >> load Filename.mat x • Load the ASCII data file back into memory >> load Filename.dat -ascii

  48. Reading and writing data files • The textread function • The load command assumes all of data is of a single type • The textread function is more flexible, it is designed to read ASCII files where each column can be of a different type • The command is: >> [A,B,C,...] = textread(filename, format, n);

  49. Reading and writing data files • The textread function • For example, if a text file “mydata.dat” contains the following lines: tommy 32 male 78.8 sandy 3 female 88.2 alex 27 male 44.4 saul 11 male 99.6 • The command is: >> [name,age,gender,score] = textread(‘mydata.dat’, ‘%s %d %s %f’, 4);

  50. Reading and writing data files • C style read/write • MATLAB allows C style file access. It is crucially important that a correct data format is used. • The steps are: • Open a file for reading or writing. A unique file identifier is assigned. • Read the data to a vector • Close the file with file identifier

More Related