Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New...

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

Transcript of Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New...

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

Computer Simulation Lab

Electrical and Computer Engineering Department

SUNY – New Paltz

SUNY-New Paltz

“Lecture 2”

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

MATLAB fundamentals

       Desktop, Editing        Variables, Operators,

Expressions        Vectors         Input / Output        Repetition        Decision

SUNY-New Paltz

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

The MATLAB Desktop

SUNY-New Paltz

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

Integrated Development Environment (IDE)

       Command Window       Current Directory       Command History       Launch Pad       Workspace

SUNY-New Paltz

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

Programs

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

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

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

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

C Language

include <stdio.h>

include <interest.h>

define int interest, balance

main{balance = 1000;

rate = 0.09;

interest = rate * balance;

balance = balance + interest;

printf(“balance=%d”, balance);

}

SUNY-New Paltz

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

MATLAB Program

balance = 1000;

rate = 0.09;

interest = rate * balance;

balance = balance + interest;

disp( ’New balance:’ );

disp( balance );

SUNY-New Paltz

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

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_dayInvalid: pay-day, 2a name$, _2a

SUNY-New Paltz

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

Case sensitivity

• Different variables:– balance, BALANCE and BaLance

• Camel Caps:– camelCaps– milleniumBug– dayOfTheWeek

• Commands are all in lower case

SUNY-New Paltz

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

Examples of variables

• interest=.09;

• years=10;

• delta=1.e-3;

• email=‘[email protected]’;

• vect=[1 2 3 4 5];

• matx=[1 2 3; 4 5 6];

SUNY-New Paltz

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

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

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

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:10x = 1:0.5:4 x = 10:-1:1 x = 0:-2:-5

• linspace – linspace(0, pi/2, 10)

SUNY-New Paltz

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

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

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

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

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

Exercise

Construct the following vectors and matrices:1. A row vector of all odd numbers between –101

and 1012. A column vector of all numbers between –101

and 101 that are divisible by 33. 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

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

Capturing output

• diary filename

• diary off

SUNY-New Paltz

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

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 ab a ^ b

SUNY-New Paltz

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

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

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

Exercise

• Evaluate the following arithmetic expressions:

• A= 2/4^2*2+1\4

• B=1/2^2*2/4

SUNY-New Paltz

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

Arithmetic operations on arrays

Arithmetic operators that operate element-by-element on arraysOperator 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

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

Statements, Commands and Functions

• Statements: s = u * t - g / 2 * t .^ 2;

• Functions: sin(x), plot(x)

• Commands: load, save, clear

SUNY-New Paltz

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

Vectorization of Formulae1- 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

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

Vectorization of Formulae2- Case of Mutiple Investment

SUNY-New PaltzSUNY-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

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

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

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

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

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

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

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

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

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

Decisions if r > 0.5

disp( ’greater indeed’ ) end

Relational operatorsRelational operatorsRelational Operator Meaning < less than <= less than or equal == equal ~= not equal > greater than >= greater than or

equal

SUNY-New Paltz

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

Decisions

SUNY-New Paltz

if condition

statementsA

else

statementsB

end

if condition1

statementsA

elseif condition2

statementsB

elseif condition3

statementsC

...

else

statementsE

end

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

Logical operators

‘~’ ‘&’ ‘ |’

if bal >= 5000 & bal < 10000

SUNY-New Paltz

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

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

end

SUNY-New Paltz

02

4 22

2,1

cbxaxa

acbbx

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

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

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

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

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

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