Presentation is loading. Please wait.

Presentation is loading. Please wait.

Matlab Fundamentals.

Similar presentations


Presentation on theme: "Matlab Fundamentals."— Presentation transcript:

1 Matlab Fundamentals

2 Overview (1) • Basic Matlab Operations ⊲ Starting Matlab
⊲ Using Matlab as a calculator ⊲ Introduction to variables and functions • Matrices and Vectors: All variables are matrices. ⊲ Creating matrices and vectors ⊲ Subscript notation ⊲ Colon notation

3 Overview (2) • Additional Types of Variables ⊲ Complex numbers
⊲ Strings ⊲ Polynomials • Working with Matrices and Vectors ⊲ Some basic linear algebra ⊲ Vectorized operations ⊲ Array operators

4 What is MATLAB? (cont.) MATLAB allows us to solve many technical computing problems in a fraction of the time it would take to write a program in a scalar non-interactive language such as C/C++ or Fortran. MATLAB is a standard instructional tool for introductory and advanced courses in mathematics, engineering, and science at the university environment. In industry, it is the tool of choice for high-productivity research, development, and analysis.

5 What is MATLAB? (cont.) MATLAB has a family of application-specific solutions called toolboxes, which allow us to learn and apply specialised technologies. Toolboxes are comprehensive collections of MATLAB functions (M-files). Areas in which toolboxes are available include Signal Processing, Control, Neural Networks, Fuzzy Logic, Wavelets, and many others.

6 What is MATLAB? (cont.) MATLAB consists of five main parts:
1. MATLAB language is a hight-level matrix/array language with control flow statements, functions, data structures, input/output, and object-oriented programming features. 2. MATLAB environment includes facilities for managing variables in the workspace and importing and exporting data. It also includes tools for developing, managing and debugging M-files (MATLAB applications).

7 What is MATLAB? (cont.) 3. MATLAB graphics includes high-level commands for 2D and 3D data visualisation, image processing and animation. It also includes low-level commands that allow us to fully customise the appearance of graphics as well as to build complete Graphical User Interfaces for MATLAB applications. 4. MATLAB mathematical function library. 5. MATLAB Application Program Interface (API) is a library that allows us to write C and Fortran programs that interact with MATLAB.

8 Matlab programming Fundamentals -----------------------------------------------------------------
How to start and quit Matlab? PC - a double click on the Matlab icon unix system - setup Matlab (return) Matlab On both system leave a Matlab session by typing : quit or by typing exit at the Matlab prompt.

9 MATLAB Desktop On starting MATLAB: * Matlab Command Prompt
The Command history maintains a record of Matlab functions you ran

10 Matlab Desktop The desktop provides different ways of interacting with Matlab ⊲ Entering commands in the command window ⊲ Viewing values stored in variables ⊲ Editing statements in Matlab functions and scripts ⊲ Creating and annotating plots Watch an animated demonstration of the Matlab desktop by typing playbackdemo(’desktop’) at the command prompt. Alt F4 to stop the demo

11 General Purpose Commands
Helpwin help window with hypertext navigation demo runs MATLAB demos from a MATLAB created Graphic User Interface (GUI) helpdesk troubleshooting with hypertext navigation ver tells you the version of MATLAB being used who lists all variables in the current workspace whos lists all variables in the workspace including array sizes clear clears all variables and functions from memory

12 pack consolidates workspace memory
General Purpose Commands (cont.) pack consolidates workspace memory load load workspace variables from disk save saves all variables and functions in the workspace to disk quit quits MATLAB session what lists MATLAB files in directory edit edits a MATLAB M-file diary save text of MATLAB session Save myfile Save myfile x y Save myfile x –ascii

13 Some Useful MATLAB commands
dir List all files in current directory ls Same as dir type test Display test.m in command window delete test Delete test.m cd a: Change directory to a: chdir a: Same as pwd Show current directory which test Display current cd directory path to test.m

14 Operating System Commands that Work in
MATLAB cd changes directory copy file copy a file delete delete a file mkdir make a directory dos execute DOS command and return result clc to clear the Command Window

15 Find your way around the windows
Run demos Click Help -> Demos Then in new window, select Matlab, then Desktop Environment Then select Desktop overview Run the demo (it will run in a browser)

16 Matlab as a Calculator (1)
Enter formulas at the command prompt >> (press return after ‘‘4’’) ans = 4 >> ans/2 2

17 Matlab as a Calculator (2)
Define and use variables >> a = 5 a = 5 >> b = 6 b = 6 >> c = b/a c = 1.2000

18 Built-in Variables pi (= π) and ans are a built-in variables
3.1416 >> sin(ans/4) 0.7071 Note: The argument of sin are measured in radians. Type lookfor sin in the command prompt to see more

19 Built-in Functions Many standard mathematical functions, such as sin, cos, log, and log10, are built-in. Example: log(x) computes the natural logarithm of x log10(x) is the base 10 logarithm log2(x) is the base 2 logarithm >> log(256) ans = 5.5452 >> log10(256) ans = 2.4082 >> log2(256) ans = 8

20 Ways to Get Help • Use on-line help to request info on a specific function >> help sqrt • In Matlab version 6 and later the doc function opens the on-line version of the manual. This is very helpful for more complex commands. >> doc plot • Use lookfor to find functions by keywords >> lookfor functionName

21 On-line Help (1) Syntax: help functionName Example: >> help log
produces LOG Natural logarithm. LOG(X) is the natural logarithm of the elements of X. Complex results are produced if X is not positive. See also LOG2, LOG10, EXP, LOGM. The help function provides a compact summary of how to use a command. Use the doc function to get more in-depth information.

22 On-line Help (2) The help browser opens when you type a doc command:
>> doc plot

23 Looking for Functions Syntax: lookfor string
searches first line of function descriptions for “string”. Example: >> lookfor cosine produces ACOS Inverse cosine. ACOSH Inverse hyperbolic cosine. COS Cosine. COSH Hyperbolic cosine.

24 Suppress Output with Semicolon (1)
Results of intermediate steps can be suppressed with semicolons. Example: Assign values to x, y, and z, but only display the value of z in the command window: >> x = 5; >> y = sqrt(59); >> z = log(y) + x^0.25 z = 3.5341

25 Suppress Output with Semicolon (2)
Type variable name and omit the semicolon to print the value of a variable (that is already defined) >> x = 5; >> y = sqrt(59); >> z = log(y) + x^0.25 z = 3.5341 >> y y = ( = log(sqrt(59)) + 5^0.25 )

26 Multiple Statements per Line
Use commas or semicolons to enter more than one statement at once. Commas allow multiple statements per line without suppressing output. >> a = 5; b = sin(a), c = cosh(a) b = c =

27 Matlab Variables Names
Legal variable names: • Begin with one of a–z or A–Z • Have remaining characters chosen from a–z, A–Z, 0–9, or • Have a maximum length of 31 characters • Should not be the name of a built-in variable, built-in function, or user-defined function Examples: xxxxxxxxx pipeRadius widgets_per_box mySum mysum Note: mySum and mysum are different variables. Matlab is case sensitive.

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50 Example i Determine the current i in the resistor R.
The mesh equations for the circuit above is: 12 = 28i1 – 10i2 – 8i3 12 = -10i1 + 28i2 – 8i3 0 = -8i1 – 8i2 +(16+R)i3 The current i in the resistor R is equal to the mesh current in the third mesh, that is: i = i3

51 Example The mesh equations can be written using matrices such as:
A.X = b X = A-1*b

52 Example Open a new editor and enter the code above, save your file as exam_01.m

53

54

55

56

57

58

59

60

61

62

63

64

65 Exercise: Generating complex functions
Generate a complex function f(t) = 3ej3t for t ranging from 0 to 1 in increments. Step1: create a time variable, t = 0:0.001:1; Step2: construct the vector, f = 3*exp(j*3*pi*t); Step3: plot (t,f)

66

67

68

69

70

71

72 Exercise 1: Evaluating complex variables and Expressions
Express the following complex numbers in Cartesian form. Plot it in the complex plane y = jej11/4 y= (1-j)10 plot(y,’x’)

73 Marker for plot(function)
b blue point solid g green o circle : dotted r red x x-mark dashdot c cyan plus dashed m magenta * star (none) no line y yellow s square k black d diamond v triangle (down) ^ triangle (up) < triangle (left) > triangle (right) p pentagram h hexagram

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91 Matrix Manipulations X = zeros(5,5) Y = ones(6,6)
Sum(A) = sums of the columns of A A’ = Transpose of A Diag(A) = gives diagonal elements of A Min(A) = minimum element column wise of A. Max(A) = maximum element column wise of A. Mean(A) = Average element column wise of A. Std(A) = Standard deviation column wise of A. X = zeros(5,5) Y = ones(6,6) M=rand(5,5) N = randn(6,6) A=[1:50] A=[0:pi/4:pi] A =[ ]

92 Matrix Manipulations (contd..)
A = magic(4) = [ Dürer's magic square ] All numbers are stored internally using the long format specified by the IEEE floating-point standard. Floating-point numbers have finite precision 16 significant decimal digits and finite range  to

93 Matrix Manipulations (contd..)
A(:,2) = [ ]  deletes 2nd column. A(2:2:12) = [ ] results in row vector of remaining elements. A = A.*5  Element-by-element multiplication. [indices] = find(isprime(A)) format  this command can be used for formatting the displayed numeric output (either in short or long format).

94 Indexing Matrices Indexing using parentheses >> A(2,3)
Index submatrices using vectors of row and column indices >> A([2 3],[1 2]) Ordering of indices is important! >> B=A([3 2],[2 1]) >> B=[A(3,2),A(3,1);A(2,2),A(2,1)]

95 Indexing Matrices Index complete row or column using the colon operator >> A(1,:) Can also add limit index range >> A(1:2,:) >> A([1 2],:) %row 1 and row2 General notation for colon operator >> v=1:5 >> w=1:2:5

96 Matrix Functions Many elementary matrices predefined
>> help elmat; >> I=eye(3) Elementary functions are often overloaded >> help elmat >> sin(A) Specialized matrix functions and operators >> As=sqrtm(A) >> As^2 >> A.*A Note: in general, ”.<operator>” is elementwise operation

97 Numerical Linear Algebra
Basic numerical linear algebra >> z=[1;2;3]; x=inv(A)*z >> x=A\z Many standard functions predefined >> det(A) >> rank(A) >> eig(A) The number of input/output arguments can often be varied >> [V,D]=eig(A)


Download ppt "Matlab Fundamentals."

Similar presentations


Ads by Google