Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”

Similar presentations


Presentation on theme: "Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”"— Presentation transcript:

1 Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”

2 MATLAB fundamentals  Desktop, Editing  V ariables, Operators, Expressions  V ectors  I nput / Output  R epetition  D ecision SUNY-New Paltz

3 The MATLAB Desktop SUNY-New Paltz

4 Integrated Development Environment (IDE)  Command Window  Current Directory  Command History  Launch Pad  Workspace SUNY-New Paltz

5 Program s A collection of statements to solve a problem is called a program. Different Types of Languages: –Machine Language (Executable File) – Low-Level Languages (Assembly) –High-Level Languages (C, C++, Java, Basic, Fortran, etc.) –Script Languages ( PERL, MATLAB, PHP, etc.) SUNY-New Paltz

6 A Simple Program: Suppose you have $1000 saved in the bank. Interest is compounded at the rate of 9% per year. What will your bank balance be after one year? SUNY-New Paltz 1.Get the data (initial balance and interest rate) into the program. 2.Calculate the interest (9 per cent of $1000, i.e. $90). 3.Add the interest to the balance ($90 + $1000, i.e. $1090) 4.Display the new balance

7 C Language include define int interest, balance main{ balance = 1000; rate = 0.09; interest = rate * balance; balance = balance + interest; printf(“balance=%d”, balance); } SUNY-New Paltz

8 MATLAB Program balance = 1000; rate = 0.09; interest = rate * balance; balance = balance + interest; disp( ’New balance:’ ); disp( balance ); SUNY-New Paltz

9 Variables and the workspace A variable name (like balance) must comply with the following two rules: 1. It may consist only of the letters a–z, the digits 0–9 and the underscore ( _ ). 2. It must start with a letter. Valid: r2d2, pay_day Invalid : pay-day, 2a name$, _2a SUNY-New Paltz

10 Case sensitivity Different variables: – balance, BALANCE and BaLance Camel Caps: – camelCaps – milleniumBug – dayOfTheWeek Commands are all in lower case SUNY-New Paltz

11 Examples of variables interest=.09; years=10; delta=1.e-3; email=‘doej@gmail.com’; vect=[1 2 3 4 5]; matx=[1 2 3; 4 5 6]; SUNY-New Paltz

12 workspace who: lists the names of all the variables in your workspace whos: lists the size of each variable as well ans: returns the value of the last expression evaluated but not assigned to a variable Workspace –Name Size Bytes Class –balance 1x1 8 double array –interest 1x1 8 double array –rate 1x1 8 double array SUNY-New Paltz

13 Arrays: vectors and matrices Initializing vectors: explicit lists x = [1 3 0 -1 5] use spaces a = [5,6,7] use commas a = [1 2 3], b = [4 5], c = [a -b] x = [ ] Initializing vectors: the colon operator x = 1:10 x = 1:0.5:4 x = 10:-1:1 x = 0:-2:-5 linspace –linspace(0, pi/2, 10) SUNY-New Paltz

14 Array Subscripts r = rand(1,7) This gives you a row vector of seven random numbers. r(3) This will display the third element of r. The number 3 is the subscript. r(2:4) This should give you the second, third and fourth elements. r(1:2:7) r([1 7 2 6]) r([1 7 2]) = [ ] will remove elements 1, 7 and 2. SUNY-New Paltz

15 Matrices a = [1 2 3; 4 5 6] = 1 2 3 4 5 6 a’ = 1 4 2 5 3 6 A matrix can be constructed from column vectors of the same lengths: x = 0:30:180; table = [x’ sin(x*pi/180)’] = 0.0000 0.0000 30.0000 0.5000 60.0000 0.8660 90.0000 1.0000 120.0000 0.8660 150.0000 0.5000 180.0000 0.0000 SUNY-New Paltz

16 Exercise Construct the following vectors and matrices: 1.A row vector of all odd numbers between –101 and 101 2.A column vector of all numbers between –101 and 101 that are divisible by 3 3.A matrix of 16 equally spaced angles between 0 and 2*pi along with sin(x) and cos(x) and tan(x). 4.A matrix of size 5x5 having random numbers between 0 and 1 for each element. SUNY-New Paltz

17 Capturing output diary filename diary off SUNY-New Paltz

18 Operators, Expressions, Statements Arithmetic operations between two scalars Operation Algebraic form MATLAB Addition a + b a + b Subtraction a - b a - b Multiplication a × b a * b Right division a/b a / b Left division b/a a \ b Power a b a ^ b SUNY-New Paltz

19 Precedence of arithmetic operations Precedence Operator 1 Parentheses 2 Power, left to right 3 Multiplication and division, left to right 4 Addition and subtraction, left to right SUNY-New Paltz

20 Exercise Evaluate the following arithmetic expressions: A= 2/4^2*2+1\4 B=1/2^2*2/4 SUNY-New Paltz

21 Arithmetic operations on arrays Arithmetic operators that operate element-by-element on arrays Operator Description.* Multiplication./ Right division.\ Left division.^ Power Examples: a = [2 4 8]; 3.* a = ? b = [3 2 2];a.^ 2 = ? a.* b = ? a./ b = ? SUNY-New Paltz

22 Statements, Commands and Functions Statements: s = u * t - g / 2 * t.^ 2; Functions: sin(x), plot (x) Commands: load, save, clear SUNY-New Paltz

23 Vectorization of Formulae 1- Case of Scalar A = 750; r = 0.09; n = 10; B = A * (1 + r) ^ n; disp( [A B] ) SUNY-New Paltz 750.00 1775.52

24 Vectorization of Formulae 2- Case of Mutiple Investment SUNY-New Paltz A = [750 1000 3000 5000 11999]; r = 0.09; n = 10; B = A * (1 + r) ^ n; disp( [A’ B’] ) 750.00 1775.52 1000.00 2367.36 3000.00 7102.09 5000.00 11836.82 11999.00 28406.00

25 Output disp( variable ) {variable could be numerical or textual} disp( ’Pilate said, ’’What is truth?’’’ ); disp( [x y z] ) disp( [’The answer is ’, num2str(x)] ); SUNY-New Paltz

26 format 1234567890 is displayed as 1.2346e+009 mantissa is between 1 and 9.9999 format short, format short e format long, format long e format long g, format short g format compact format hex format rat format bank SUNY-New Paltz

27 Repeating with for for i = 1:5 disp(i) end for index = j:m:k for index = v (where v is any vector such as [2 3 8]) Show the squares of all even numbers between 0 and 1000 SUNY-New Paltz for i=0:2:1000 disp([i i*i]) end

28 Avoid Loops! s = 0; for n = 1:100000 s = s + n; end; n = 1:100000; s = sum( n ); SUNY-New Paltz Find x=1+1/2+1/3+1/4…. + 1/1000

29 Decisions if r > 0.5 disp( ’greater indeed’ ) end Relational operators Relational Operator Meaning < less than <= less than or equal == equal ~= not equal > greater than >= greater than or equal SUNY-New Paltz

30 Decisions SUNY-New Paltz if condition statementsA else statementsB end if condition1 statementsA elseif condition2 statementsB elseif condition3 statementsC... else statementsE end

31 Logical operators ‘~’ ‘&’ ‘ |’ if bal >= 5000 & bal < 10000 SUNY-New Paltz

32 Nested if’s d = b^2 - 4*a*c; if a ~= 0 if d < 0 disp( ’Complex roots’ ) else x1 = (-b + sqrt( d )) / (2*a); x2 = (-b - sqrt( d )) / (2*a); end SUNY-New Paltz

33 Switch/Case d = floor(3*rand) + 1 switch d case 1 disp( ’That’’s a 1!’ ); case 2 disp( ’That’’s a 2!’ ); otherwise disp( ’Must be 3!’ ); end SUNY-New Paltz

34 Switch/Case d = floor(10*rand); switch d case {2, 4, 6, 8} disp( ’Even’ ); case {1, 3, 5, 7, 9} disp( ’Odd’ ); otherwise disp( ’Zero’ ); end SUNY-New Paltz

35 Complex numbers z = 2 + 3*i sqrt(2 + 3*i) exp(i*pi) circle = exp( 2*i*[1:360]*pi/360 ); % select all points around a circle plot(circle) % is equivalent to: plot(real(y), imag(y)) axis equal a = [1+i, 2+2i; 3+3i, 4+4i] SUNY-New Paltz 1.0000 + 1.0000i 2.0000 + 2.0000i 3.0000 + 3.0000i 4.0000 + 4.0000i


Download ppt "Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”"

Similar presentations


Ads by Google