Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.

Similar presentations


Presentation on theme: "Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting."— Presentation transcript:

1 Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting

2 Basic functions MATLAB as a calculator » (3+5)/7 ans = 1.1429 » x = (3+5)/7 x = 1.1429 » x x = 1.1429 + addition -subtraction /division *multiplication ^power Sum of (3+5) divided by 7 define a variable

3 Basic function (continued) If you want to see how many variables you have defined so far, you can use the following command: » who Your variables are: ans x » »whos Name Size Bytes Class ans 1x1 8 double array x 1x1 8 double array Grand total is 2 elements using 16 bytes List all the variables in the current MATLAB session List all the variables with details

4 Basic function (continued) Once you have defined variables: » x = -10 x = -10 » y = 5*x y = -50 » x = (3+5)/7 x = 1.1429 *Once you have defined a value for a variable, you can use it to perform algebraic operation *By default, MATLAB displays 4 decimal digit if the value is not an integer

5 Special Variables In MATLAB, there are some special variables: » pi ans = 3.1416 » i, j, ans = 0+1.0000i ans = 0+1.0000i Value for  Imaginary number by default unless you change them

6 Format By default, the answer only displays 4 decimal places if it is not an integer If you want to change the format of the output: » format long » pi ans = 3.14159265358979 Other format for the output: format short (4 decimal places) format short e (scientific notation) format long (more than 12) format bank (2 decimal place)

7 Built-in functions In MATLAB, there are many standard built-in functions » z = sin (pi/4) z = 0.7071 » q = log (4) q = 1.3863 Trigonometric function: sin ( ) asin ( ) cos ( ) acos ( ) tan ( ) atan ( ) ** Angle is in radian Elementary function: sqrt ( )  exp ( )e log ( )log log10 ( )log 10

8 Defining a vector (Row vector) MATLAB is a tool for doing computations with vectors & matrices Row Vectors: » v1 = [2 3, 3] v1 = 2 3 3 » length (v1) ans = 3 Name of vector You can use either a space or comma to separate the elements in vector definition Square bracket Reports the number of elements in a vector

9 Defining a vector (Row vector) You can perform standard linear algebra operations on these vectors » v2 = 3*v1 v2 = 6 9 9 » v3 = [2 2] v3 = 2 2 » v4 = v3+v1 ??? Undefined function or variable ‘v1’ » Multiplication by a scalar quantity Other vector operations: - Addition/subtraction of a vector -dot product …etc **Row vectors must have the same length

10 Defining a vector (Column vector) Similar you can also define a column vector » c1 = [1;3;2] c1 = 1 3 2 » c2 = [2 2 4] c2 = 2 4 Use semicolon or enter the value at the next line for next row elements Square bracket

11 Defining a vector (Column vector) We can convert a row vector into a column vector (or vice versa) by taking the transpose which can be done by using a single quote ’ » v4 = c1’ v4 = 1 3 2 » Transpose operator

12 Matrices In a similar fashion, you can define a matrix in MATLAB by doing the following: » a =[5 7 9 1 -3 -7] a = 5 7 9 1 -3 -7 » a = [ 5 7 9; 1 -3 -7] a = 5 7 9 1 -3 -7 Element in first row Use a semi-colon for next row elements Similarly, for matrix operation, it must follow the basic rule of linear algebra, eg. dimensions must agree!

13 Matrix Math Matrix math –Dimensions must agree Scalar math –Same as usual Scalar / matrix math –Scalar + matrix = [scalar + matrix(x, y)] –Scalar * matrix = [scalar * matrix(x, y)]

14 More Matrix Math The ‘.’ operator –“element by element” access Example: –x = [1 2 3];y = [4; 5; 6]; –x * y = 32 –x * y’ ERROR! –x.* y’ = [4 10 18] For some functions (x same as above): –x ^ 2ERROR! –x. ^2fine

15 Creating Vectors with Constant Spacing vname = [m:q:n] m is first element q is spacing (if omitted 1 is assumed) n is last element >> z = [1:2:7] z = 1 3 5 7 >> x = [1:7] x = 1 2 3 4 5 6 7

16 Creating Vectors with Constant Spacing and Known Element Count vname = linspace( xi, xf, n ) xi is first element xf is last element n is number of elements in vector omitted n gives 100 elements >> z = linspace( 1, 5, 9 ) z = 1 1.5 2 2.5 3 3.5 4 4.5 5

17 More Vector Math Examine values of f(x) for −5 ≤ x ≤ 5

18 Element access xretrieves entire matrix x x(1,2)retrieves element at row 1, col 2 x(1, 5:10)retrieves row 1, columns 5 to 10 (use colon) x(1,:)retrieves row 1, all columns Useful functions: size(x) rows, cols of x

19 Plotting » x=[4 8 11] x = 4 8 11 » y=[4 5 9] y = 4 5 9 » plot (x,y) » plot(x,y,’o’) » axis([0 10 0 10]) » title(‘experimental data’) » xlabel(‘x’) » ylabel(‘y’) » grid on Plot the discrete data point indicated by o Change axis [xmin xmax ymin ymax] Labeling Turn on the gird on the graph Plot the data point and connected them by straight line

20 Plotting (continued) » z z = 4 4 8 5 11 9 » plot (z(:,1),z(:,2)) All row elements in first column All row elements in second column » plot (x1, y1, x2, y2, x3, y3 ) Multiple plot on the same graph (3 set of data points) If the data is stored in a matrix

21 Plotting (continued) » t = 0:.3:10; » y = sin(t); » plot(t,y,’r’) ; To make a graph of y = sin(t) on the interval t = 0 to t = 10 We can also use the commant fplot: » fplot (‘ff’, [ 0 10]) Function defined by a m-file (make sure you put the quotation) Limit for x value Note the use of the colon as a ‘range’ operator

22 Some housekeeping commands The command save can store all the variables defined in the workspace in a binary file matlab.mat (default name) The command load can restore the workspace from the file matlab.mat You can also store individual variable by writing: save filename X Y Z You specify a filename The command clear all remove all variables in the current workspace

23 Making a M-file Filename has an extension.m You can use M-file to do 2 things: - script file (when you want to do many MATLAB operations at the same time, consisting of a sequence of normal MATLAB statement) - function file (to create a function) To create a M-file, go to the toolbar of the MATLAB command window and from: file  new  M-file

24 Script file %filename= method1.m clear all x1= (2+4)/5; x2= x1*7; y1= sqrt(15) y2= exp(5) v1 = [x1 x2] c1 = [y1;y2]; dot_product = v1*c1 display('SAY HI!!!!') » method1 y1 = 3.8730 y2 = 148.4132 v1 = 1.2000 8.4000 dot_product = 1.2513e+003 ans = SAY HI!!!! Comment (is not a MATLAB statement) Erase all previously defined variables MATLAB statement The filename is called method1.m Use the command ls to check if this file is in your currently working directory Output To inhibit echo (operation not shown)

25 Function file function [out] = mean (x) % To calculate the mean or average of % a list of number % X is an array of number m = length(x) out = sum(x)/m output » v = [ 67 89 52 98 30] v = 67 89 52 98 30 » avg = mean(v) m = 5 mean = 67.2000 avg = 67.2000 Indicate that this file is a function M-file Return value Name of your function * *The name of your function should be the same as your m-file name Input argument

26 Flow Constructs IF block if ( ) elseif end Conditions <less than <=less and equal then ==equal >greater than <greater and equal then ~=not equal As you learned from programming, there are times when you want your code to make a decision:

27 If statement % filename method2.m if (grade< 55) display('fail') else if(grade>85) display('grade A') else display('pass') end Output » grade=88 grade = 88 » method2 ans = grade A » # of “if” must be consistent with # of “end”

28 Flow Constructs For loop for i = 1:10 end For loop for i = 1:2:10 end Initial value Final value (where you stop the loop) Initial value Final value (where you stop the loop) Specify the increment

29 Flow Constructs While loop n=0 while (n <8) n=n+1 end condition


Download ppt "Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting."

Similar presentations


Ads by Google