Presentation is loading. Please wait.

Presentation is loading. Please wait.

מטלב וניתוח נתונים ממערכות עצביות היכרות עם MATLAB

Similar presentations


Presentation on theme: "מטלב וניתוח נתונים ממערכות עצביות היכרות עם MATLAB"— Presentation transcript:

1 מטלב וניתוח נתונים ממערכות עצביות היכרות עם MATLAB
ד"ר אורן שריקי Based on: PowerPoint to accompany the book “Introduction to MATLAB for Engineers”, Third Edition by William J. Palm III, Copyright © The McGraw-Hill Companies, Inc.

2 Overview 2-2

3 The Default MATLAB Desktop:

4 Entering Commands and Expressions
• MATLAB retains your previous keystrokes. • Use the up-arrow key to scroll back through the commands. • Press the key once to see the previous entry, and so on. • Use the down-arrow key to scroll forward. Edit a line using the left- and right-arrow keys the Backspace key, and the Delete key. • Press the Enter key to execute the command.

5 Scalar arithmetic operations

6 An Example Session: >> 8/10 ans = 0.8000 >> 5*ans 4
>> r=8/10 r = >> r >> s=20*r s = 16

7 Commands for managing the work session

8 Special variables and constants

9 Numeric display formats:
The Desktop Menus and Toolbar:

10 Arrays • The numbers 0, 0.1, 0.2, …, 10 can be assigned to the variable u by typing u = 0:0.1:10. • To compute w = 5 sin u for u = 0, 0.1, 0.2, …, 10, the session is; >>u = 0:0.1:10; >>w = 5*sin(u); • The single line, w = 5*sin(u), computed the formula w = 5 sin u 101 times.

11 Array Index >>u(7) ans = 0.6000 >>w(7) 2.8232
• Use the length function to determine how many values are in an array. >>m = length(w) m = 101

12 To find the roots of x3 – 7x2 + 40x – 34 = 0, the session is
Polynomial Roots, Page 20 To find the roots of x3 – 7x2 + 40x – 34 = 0, the session is >>a = [1,-7,40,-34]; >>roots(a) ans = i i 1.0000 The roots are x = 1 and x = 3 ± 5i.

13 Some commonly used mathematical functions:

14 When you type problem1, 1. MATLAB first checks to see if problem1 is a variable and if so, displays its value. 2. If not, MATLAB then checks to see if problem1 is one of its own commands, and executes it if it is. 3. If not, MATLAB then looks in the current directory for a file named problem1.m and executes problem1 if it finds it. 4. If not, MATLAB then searches the directories in its search path, in order, for problem1.m and then executes it if found.

15 System, directory, and file commands:

16 A graphics window showing a plot:

17 Some MATLAB plotting commands:

18 You can perform operations in MATLAB in two ways:
1. In the interactive mode, in which all commands are entered directly in the Command window, or 2. By running a MATLAB program stored in script file. This type of file contains MATLAB commands, so running it is equivalent to typing all the commands—one at a time—at the Command window prompt. You can run the file by typing its name at the Command window prompt.

19 COMMENTS The comment symbol may be put anywhere in the line. MATLAB ignores everything to the right of the % symbol. For example, >>% This is a comment. >>x = 2+3 % So is this. x = 5 Note that the portion of the line before the % sign is executed to compute x.

20 The MATLAB Command window with the Editor/Debugger open:

21 Keep in mind when using script files:
1. The name of a script file must begin with a letter, and may include digits and the underscore character, up to 63 characters. 2. Do not give a script file the same name as a variable. 3. Do not give a script file the same name as a MATLAB command or function. You can check to see if a command, function or file name already exists by using the exist command.

22 Debugging Script Files
Program errors usually fall into one of the following categories. 1. Syntax errors such as omitting a parenthesis or comma, or spelling a command name incorrectly. MATLAB usually detects the more obvious errors and displays a message describing the error and its location. 2. Errors due to an incorrect mathematical procedure, called runtime errors. Their occurrence often depends on the particular input data. A common example is division by zero.

23 To locate program errors, try the following:
1. Test your program with a simple version of the problem which can be checked by hand. 2. Display any intermediate calculations by removing semicolons at the end of statements. 3. Use the debugging features of the Editor/Debugger.

24 Programming Style 1. Comments section a. The name of the program and any key words in the first line. b. The date created, and the creators' names in the second line. c. The definitions of the variable names for every input and output variable. Include definitions of variables used in the calculations and units of measurement for all input and all output variables! d. The name of every user-defined function called by the program.

25 Programming Style (continued)
Input section Include input data and/or the input functions and comments for documentation. Calculation section 4. Output section This section might contain functions for displaying the output on the screen.

26 Some Input/output commands:

27 Example of a Script File:
Problem: The speed v of a falling object dropped with no initial velocity is given as a function of time t by v = gt. Plot v as a function of t for 0 < t < tfinal, where tfinal is the final time entered by the user.

28 Example of a Script File (continued)
% Program falling_speed.m: % Plots speed of a falling object. % Created on March 1, 2009 by W. Palm % % Input Variable: % tfinal = final time (in seconds) % Output Variables: % t = array of times at which speed is % computed (in seconds) % v = array of speeds (meters/second)

29 Example of a Script File (continued)
% Parameter Value: g = 9.81; % Acceleration in SI units % % Input section: tfinal = input(’Enter final time in seconds:’);

30 Example of a Script File (continued)
% Calculation section: dt = tfinal/500; % Create an array of 501 time values. t = 0:dt:tfinal; % Compute speed values. v = g*t; % % Output section: Plot(t,v),xlabel(’t (s)’),ylabel(’v m/s)’)

31 Getting Help From MATLAB: The Function Browser after plot has been selected

32 The MATLAB Help Browser

33 MATLAB Help Functions, help funcname: Displays in the Command window a description of the specified function funcname. lookfor topic: Looks for the string topic in the first comment line (the H1 line) of the HELP text of all M-files found on MATLABPATH (including private directories), and displays the H1 line for all files in which a match occurs. doc funcname: Opens the Help Browser to the reference page for the specified function funcname, providing a description, additional remarks, and examples.

34 Arrays

35 Vectors: To create a row vector, separate the elements by commas or spaces. Use square brackets. For example, >>p = [3,7,9] p = You can create a column vector by using the transpose notation ('). >>p = [3,7,9]' 3 7 9

36 You can also create a column vector by separating the elements by semicolons. For example,
3 7 9

37 You can create vectors by ''appending'' one vector to another.
For example, to create the row vector u whose first three columns contain the values of r = [2,4,20] and whose fourth, fifth, and sixth columns contain the values of w = [9,-6,3], you type u = [r,w]. The result is the vector u = [2,4,20,9,-6,3].

38 The colon operator (:) easily generates a large vector of regularly spaced elements. Parentheses are not needed but can be used for clarity. Do not use square brackets. Typing >>x = m:q:n or >>x = (m:q:n) creates a vector x of values with a spacing q. The first value is m. The last value is n if m - n is an integer multiple of q. If not, the last value is less than n.

39 For example, typing x = 0:2:8 creates the vector x = [0,2,4,6,8], whereas typing x = 0:2:7 creates the vector x = [0,2,4,6]. To create a row vector z consisting of the values from 5 to 8 in steps of 0.1, type z = 5:0.1:8. If the increment q is omitted, it is presumed to be 1. Thus typing y = -3:2 produces the vector y = [-3,-2,-1,0,1,2].

40 The linspace command also creates a linearly spaced row vector, but instead you specify the number of values rather than the increment. The syntax is linspace(x1,x2,n), where x1 and x2 are the lower and upper limits and n is the number of points. For example, linspace(5,8,31) is equivalent to 5:0.1:8. If n is omitted, the spacing is 1.

41 The logspace command creates an array of logarithmically spaced elements.
Its syntax is logspace(a,b,n), where n is the number of points between 10a and 10b. For example, x = logspace(-1,1,4) produces the vector x = [0.1000, , , ]. If n is omitted, the number of points defaults to 50.

42 A matrix has multiple rows and columns. For example, the matrix
Matrices A matrix has multiple rows and columns. For example, the matrix has four rows and three columns. Vectors are special cases of matrices having one row or one column. M =

43 creates the following matrix: 2 4 10 16 3 7
Creating Matrices If the matrix is small you can type it row by row, separating the elements in a given row with spaces or commas and separating the rows with semicolons. For example, typing >>A = [2,4,10;16,3,7]; creates the following matrix: Remember, spaces or commas separate elements in different columns, whereas semicolons separate elements in different rows. A =

44 Creating Matrices from Vectors
Suppose a = [1,3,5] and b = [7,9,11] (row vectors). Note the difference between the results given by [a b] and [a;b] in the following session: >>c = [a b]; c = >>D = [a;b] D =

45 You need not use symbols to create a new array
You need not use symbols to create a new array. For example, you can type >> D = [[1,3,5];[7,9,11]]; Array Addressing The colon operator selects individual elements, rows, columns, or ''subarrays'' of arrays. Here are some examples:  ■ v(:) represents all the row or column elements of the vector v. ■ v(2:5) represents the second through fifth elements; that is v(2), v(3), v(4), v(5).

46 Array Addressing, continued
A(:,3) denotes all the elements in the third column of the matrix A. A(:,2:5) denotes all the elements in the second through fifth columns of A. A(2:3,1:3) denotes all the elements in the second and third rows that are also in the first through third columns.  v = A(:) creates a vector v consisting of all the columns of A stacked from first to last. A(end,:) denotes the last row in A, and A(:,end) denotes the last column.

47 then type C = B(2:3,1:3), you can produce the following array:
You can use array indices to extract a smaller array from another array. For example, if you first create the array B B = then type C = B(2:3,1:3), you can produce the following array: C =

48 Additional Array Functions
Computes the arrays u and v, containing the row and column indices of the nonzero elements of the matrix A, and the array w, containing the values of the nonzero elements. The array w may be omitted. Computes either the number of elements of A if A is a vector or the largest value of m or n if A is an m × n matrix. [u,v,w] = find(A) length(A)

49 Element-by-element operations:
Symbol + - .* ./ .\ .^ Operation Scalar-array addition Scalar-array subtraction Array addition Array subtraction Array multiplication Array right division Array left division Array exponentiation Form A + b A – b A + B A – B A.*B A./B A.\B A.^B Examples [6,3]+2=[8,5] [8,3]-5=[3,-2] [6,5]+[4,8]=[10,13] [6,5]-[4,8]=[2,-3] [3,5].*[4,8]=[12,40] [2,5]./[4,8]=[2/4,5/8] [2,5].\[4,8]=[2\4,5\8] [3,5].^2=[3^2,5^2] 2.^[3,5]=[2^3,2^5] [3,5].^[2,4]=[3^2,5^4]

50 If

51 The if Statement The if statement’s basic form is if logical expression statements end Every if statement must have an accompanying end statement. The end statement marks the end of the statements that are to be executed if the logical expression is true.

52 The else Statement The basic structure for the use of the else statement is if logical expression statement group 1 else statement group 2 end

53 Flowchart of the else structure.
Figure 4.4–2, page 167

54 The statements if logical expression 1 if logical expression 2 statements end can be replaced with the more concise program if logical expression 1 & logical expression 2

55 The elseif Statement The general form of the if statement is if logical expression 1 statement group 1 elseif logical expression 2 statement group 2 else statement group 3 end The else and elseif statements may be omitted if not required. However, if both are used, the else statement must come after the elseif statement to take care of all conditions that might be unaccounted for.

56 Loops

57 for Loops A simple example of a for loop is for k = 5:10:35 x = k^2 end The loop variable k is initially assigned the value 5, and x is calculated from x = k^2. Each successive pass through the loop increments k by 10 and calculates x until k exceeds 35. Thus k takes on the values 5, 15, 25, and 35, and x takes on the values 25, 225, 625, and The program then continues to execute any statements following the end statement.

58 Flowchart of a for Loop.

59 Note the following rules when using for loops with the loop variable expression k = m:s:n:
· The step value s may be negative. Example: k = 10:-2:4 produces k = 10, 8, 6, 4. · If s is omitted, the step value defaults to one. · If s is positive, the loop will not be executed if m is greater than n. · If s is negative, the loop will not be executed if m is less than n. · If m equals n, the loop will be executed only once. · If the step value s is not an integer, round-off errors can cause the loop to execute a different number of passes than intended.

60 For example, the following code uses a continue statement to avoid computing the logarithm of a negative number. x = [10,1000,-10,100]; y = NaN*x; for k = 1:length(x) if x(k) < 0 continue end y(k) = log10(x(k)); y The result is y = 1, 3, NaN, 2.

61 while Loops The while loop is used when the looping process terminates because a specified condition is satisfied, and thus the number of passes is not known in advance. A simple example of a while loop is x = 5; while x < 25 disp(x) x = 2*x - 1; end The results displayed by the disp statement are 5, 9, and 17.

62 while logical expression statements end
The typical structure of a while loop follows. while logical expression statements end For the while loop to function properly, the following two conditions must occur: The loop variable must have a value before the while statement is executed. 2. The loop variable must be changed somehow by the statements.

63 Flowchart of the while loop.


Download ppt "מטלב וניתוח נתונים ממערכות עצביות היכרות עם MATLAB"

Similar presentations


Ads by Google