Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 2 MATLAB as Calculator Scalar Variables Vectors and Matrices

Similar presentations


Presentation on theme: "Lecture 2 MATLAB as Calculator Scalar Variables Vectors and Matrices"— Presentation transcript:

1 Lecture 2 MATLAB as Calculator Scalar Variables Vectors and Matrices
Basic Matrix Operations

2 Useful Functions and Operations MATLAB
MATLAB as Calculator (Revisited) >> 2+2 100-43 3*3 5/2 2^8 log(2)*sin(pi/2) acos(-1)/exp(4) MATLAB prompt

3 Arithmetic Operations
MATLAB Command + (addition) + - (subtraction) - × (multiplication) * / (division) / (exponentiation) ^ (caret)

4 Special Values: Normally, mathematicians use i to represent
Description MATLAB Command Example Pi pi x=pi^2 = square root of -1 i or j i=sqrt(-1) infinity Inf z=pi/0 The last computed unassigned result to an expression typed in the Command window. ans Normally, mathematicians use i to represent whereas engineers use j to represent

5 Trigonometric Functions
Description MATLAB Command Example sine sin sin(pi/6) cosine cos cos(pi/3) tangent tan tan(pi/4) inverse sine (arcsine) asin asin(1/2) inverse cosine (arccos) acos acos(1/2) inverse tangent (arctan) atan atan(1)

6 Hyperbolic Functions Description MATLAB Command Example
hyperbolic sine sinh sinh(pi/6) hyperbolic cosine cosh cosh(1) hyperbolic tangent tanh tanh(pi/4) inverse hyperbolic sine asinh asinh(0.5) inverse hyperbolic cosine acosh acosh(0.5) inverse hyperbolic tangent atanh atanh(0.5)

7 Exponential, Square Root, Error Functions
Description MATLAB Command Example exponential exp exp(1) natural logarithm log log(10) Common (base 10) logarithm log10 log10(10) square root sqrt sqrt(25) error function erf

8 Complex Numbers: i=sqrt(-1) Description MATLAB Command Example
absolute value (magnitude) abs z1=1+j; y=abs(z1) phase angle (in radians) angle phi=angle(z1) complex conjugate conj z2=4+5*j; conj(z2) real(z2) imag(z2) complex imaginary part imag v=imag(z1) complex real part real z3=2*exp(j*pi/6) w=real(z3)

9 Rounding Functions Description MATLAB Command Example round round(2.5) round towards negative infinity floor floor(-2.6) round towards positive infinity ceil ceil(-2.6) round towards zero fix fix(-1.9) remainder after division mod mod(7,3) The ceiling of x is the smallest integer which is greater than or equal to x, while the floor of x is the largest integer less than or equal to x.

10 Rounding Functions Description MATLAB Command Example
factorial n or n! factorial factorial(4) binomial coefficient Cnk nchoosek nchoosek(4,2) prime factors factor factor(12)

11 Example 1: Question: Compute . In MATLAB this is done by simply typing
at the prompt. Be careful with parentheses and do not forget to type * whenever you multiply! >>

12 Useful Functions and Operations MATLAB
Match regular expression (case insensitive): This means that MATLAB knows a difference between letters written as lower and upper case letters, e.g., a and A.

13 Example 2: >> close all; clear all; clc;
>> balance = 1; >> Balance ??? Undefined function or variable 'Balance'. >> BALANCE ??? Undefined function or variable 'BALANCE'. Upper and lower case characters are not equivalent (MATLAB is case sensitive). Do you mean: >> balance

14 Example 3: >> close all; clear all; clc; >> balance = 1; >> balance balance = 1 Typing the name of a variable will cause MATLAB to display its current value. MATLAB uses parenthesis ( ), square bracket [ ], and curly braces { }, and most standard characters, such as a to z, A to Z, %, ^, +, -, *, /.

15 Obtaining Help on MATLAB commands
Type help topic to access online help on a command, function or symbol topic (e.g. help cos). Hyperlinks, indicated by underlines, are provided that will take you to related help items and the Help browser. Use lookfor topic (e.g. lookfor cosine) if you are not certain what the name of the topic is. If you press the tab key after partially typing a function or variable name, MATLAB will attempt to complete it, offering you a selection of choices if there is more than one possible completion.

16 Scalar Variables in MATLAB
x = 2 x y=4 x+y clear x MATLAB prompt This assigns the value 2 to the variable x. Hit return/enter key and what will you see? Clear a user – definded variable x. Hit return/enter key and what will you see?

17 Scalar Variables in MATLAB
z = 3; MATLAB prompt This assigns the value 3 to the variable z without printing it. You can have variable name like: X, A2, x_y. Cases are sensitive: x is not the same as X.

18 Variable identifier (name):
Variable Assignment >> max_grade = 100; Assignment operator Variable identifier (name): Letters Digits Underscore Can’t start with a number or underscore Case sensitive!!! Can’t be a keyword Value clear max_grade; clear; clear all;

19 Example 4: >> r3d3 = 10; Here are examples of valid variables.
>> pay_cheque = 10000; Here are examples of valid variables.

20 Example 5: Here are examples of invalid names are:
>> pay-cheque = 10000; >> 1a = 2; >> Name$ = 300; >> _4b = 45; pay-cheque = 10000; | Error: The expression to the left of the equals sign is not a valid target for an assignment. 1a = 2; | Error: Unexpected MATLAB expression. Name$ = 300; | Error: The input character is not valid in MATLAB statements or expressions. _4b = 45; | Error: The input character is not valid in MATLAB statements or expressions.

21 Example 6: Don’t override built-in functions!!! >>
disp = 100; disp(x); builtin('disp', x); clear disp; The disp variable overrides the built-in disp function. Don’t override built-in functions!!!

22 Built-in Variables Three built-in variables are: pi, i, j, where
Not good to re-assign pi as a variable. To reset i: >> clear i or >> i = sqrt(-1)

23 Everything in MATLAB is an array!
Data Types (Variable types) A scalar is also an array of size 1x1 Next lectures… Everything in MATLAB is an array!

24 Data Types (Variable types)
Integer Default

25 Data Types (Variable types)
Integer: a = int16(100); Be careful of memory overflow b = int8(5000); Real(/Complex): x = double(235.5); x = single(235.5); x = 235;

26 Numeric Data Functions
A summery of numeric functions: Help -> contents -> MATLAB -> Functions -> Mathematics -> Array and Matrices (and also the rest of the items…) Notice: isinf, isnan, floor, ceil Infinite Is Not A Number

27 Who’s Who in My Memory To know what you have assigned or computed:
>> close all; clear all; clc; b = 2 c = [2,3,5] d = [4;1;3;-1] e = 'words, words, words' f = [7,9;8,5] who whos List variables currently in the workspace List variables currently in the workspace with their size

28 Vectors and Matrices How to input: rvec = [1 2 3] cvec = [4 3 4 2]
A MATLAB variable is an array. A one dimensional array is named vector. A two dimensional array is named matrix. How to input: rvec = [1 2 3] cvec = [4 3 4 2] mat = [3 3 4 4 5 6 9 9 0]

29 Faster way of generating vectors and matrices:
Or simply: rvec = [1, 2 3] cvec = [4; 3; 4; 2] mat1 = [3 3, 4; 4 5 6; 9, 9 0] Faster way of generating vectors and matrices: zeros(4,3) = 4-by-3 zero matrix ones(1,4) =[1, 1, 1, 1] eye(4,4) =4-by-4 identity matrix rand(4,1) =4-by-1 random matrix [1:4] =[1, 2, 3, 4] [1:4]’ = ? [1:0.5:3] =[1, 1.5, 2, 2.5, 3] [3:-0.5:1] =[3, 2.5, 2, 1.5, 1] Space / comma – New element in same line New line / Semicolon – new line Colon operator – start:increment:end. Default step is one. ‘ operator- transpose (near the enter key)

30 Controlling Output >> x= ; >> format long; x >> format long e; x e+000 >> y= ; >> format short e; y 2.3909e+002 >> format short; y Scientific or Exponential notation

31 Controlling Output 7 5 6 7 8 3 6 7 8 >> A=[1,2,3,4;5,6,7,8];
>> A(:,3) 3 >>A(2,2:4)

32 Controlling Output >>A=[1 2 3 4; 11 12 13 14; 21 22 23 24] A =
>> A([1 2], [2 3 4]) ans = >> A([1 3], [1 4]) >> A([1 3], [1 4]) = [-5 -6; -7 -8]

33 Assignment into Array >> A = [1, 2, 3; 4, 5, 6; 7, 8, 9] A = >> A(3,4) ??? Index exceeds matrix dimensions. >> A(4,4) = 10 1 2 3 4 5 6 7 8 9 ? 1 2 3 4 5 6 7 8 9 10 Notice the difference!

34 Subscripted assignment dimension must match!
Assignment into Array >> A = [1, 2, 3; 4, 5, 6; 7, 8, 9] A = >> A(:) = 21:30 ??? In an assignment A(:) = B, the number of elements in A and B must be the same. >> B = [21, 22, 23; 24, 25, 26] B = >> A(1:2,1:3) = B 1 2 3 4 5 6 7 8 9 Notice the difference! >> A(1:2,1:3) = 10 A = 26 25 24 23 22 21 Subscripted assignment dimension must match! Scalar expansion

35 Some Useful Commands MATLAB Command Explanatory Note quit, exit
quit MATLAB diary log the session clear clear all variables in the memory help XX help on the command XX who what variables in memory whos size of the variables lookfor XY search command related to XY clc clear command window ; suppress printing % comments (MATLAB will not read)

36 Entry-wise Operations
MATLAB Command Explanatory Note C = [3 1 1; ; ] % C is a 3-by-3 matrix d = [2 3 4] % d is a row vector e = d' % e is transpose of d size(C) % size of C length(d) % length of d 3*C % scalar multiplication C*e % matrix-vector product C*C % matrix-matrix product C\e % solution to C x = e d/C % solution to d = x C C^2 % square of C = C*C

37 Basic Matrix Operations
MATLAB Command Explanatory Note 3.*A = 3*A A + B = A+B A – B = A-B A.*B = ai,j * bi,j A./B = ai,j / bi,j A.^2 = ai,j * ai,j A+3 = ai,j + 3

38 Mathematical equivalent
Use / and \operations: MATLAB notation Mathematical equivalent Right division: a/b Left division a\b b/a Elementary matrix and array operations: Arithmetic Operation Matrix sense Entry-wise sense Addition + Subtraction - Multiplication * .* Left division \ .\ Right division / ./ Exponentiation ^ .^

39 Scalar Functions for Matrices
MATLAB Command Explanatory Note sin(C) % sine of each entry of matrix C exp(C) % exponential of each entry of C real(C) % real part of each entry of C ceil(C) % round each C’s entry up floor(C) % round each C’s entry down sqrt(C) % square root of each entry of C C^(1/2) % matrix square root of C

40 Scalar Functions as Matrix Functions
Polynomial Evaluation: Consider p(x)=2x3+4x-7, to compute p(3), use: p=[ ]; x=3; y=polyval(p,x) To compute p(x) for x in between 0 and 2, use: y=polyval(p,[0:0.01:2]) If A is a matrix, polyval(p,A) gives 2*A.^3+4*A-7 polyval(p,A)


Download ppt "Lecture 2 MATLAB as Calculator Scalar Variables Vectors and Matrices"

Similar presentations


Ads by Google