Computer Simulation Lab

Download Report

Transcript Computer Simulation Lab

Computer Simulation Lab
“Lecture 2”
Electrical and Computer Engineering
Department
SUNY – New Paltz
SUNY-New Paltz
MATLAB fundamentals






Desktop, Editing
Variables, Operators, Expressions
Vectors
Input / Output
Repetition
Decision
SUNY-New Paltz
The MATLAB Desktop
SUNY-New Paltz
Integrated Development
Environment (IDE)
q
q
q
q
q
Command Window
Current Directory
Command History
Launch Pad
Workspace
SUNY-New Paltz
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
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?
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
SUNY-New Paltz
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
MATLAB Program
balance = 1000;
rate = 0.09;
interest = rate * balance;
balance = balance + interest;
disp( ’New balance:’ );
disp( balance );
SUNY-New Paltz
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$,
SUNY-New Paltz
_2a
Case sensitivity
• Different variables:
– balance, BALANCE and BaLance
• Camel Caps:
– camelCaps
– milleniumBug
– dayOfTheWeek
• Commands are all in lower
case
SUNY-New Paltz
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
workspace
• who:
• whos:
• ans:
lists the names of all the variables in your workspace
lists the size of each variable as well
returns the value of the last expression evaluated but
not assigned to a variable
• Workspace
–
–
–
–
Name
balance
interest
rate
Size
1x1
1x1
1x1
Bytes
8 double
8 double
8 double
SUNY-New Paltz
Class
array
array
array
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
x
x
x
=
=
=
=
1:10
1:0.5:4
10:-1:1
0:-2:-5
• linspace
– linspace(0, pi/2, 10)
SUNY-New Paltz
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
Matrices
•
a = [1 2 3; 4 5 6] = 1 2 3
456
• a’ = 1 4
25
36
• 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
Exercise
1.
2.
3.
4.
Construct the following vectors and matrices:
A row vector of all odd numbers between –101
and 101
A column vector of all numbers between –101
and 101 that are divisible by 3
A matrix of 16 equally spaced angles between 0
and 2*pi along with sin(x) and cos(x) and tan(x).
A matrix of size 5x5 having random numbers
between 0 and 1 for each element.
SUNY-New Paltz
Capturing output
• diary filename
• diary off
SUNY-New Paltz
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
Precedence of arithmetic
operations
Precedence
1
2
3
4
Operator
Parentheses
Power, left to right
Multiplication and division, left
to right
Addition and subtraction, left
to right
SUNY-New Paltz
Exercise
• Evaluate the following arithmetic
expressions:
• A= 2/4^2*2+1\4
• B=1/2^2*2/4
SUNY-New Paltz
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];
b = [3 2 2];
3 .* a = ?
a .^ 2 = ?
a .* b = ?
a ./ b = ?
SUNY-New Paltz
Statements, Commands and
Functions
• Statements:
/ 2 * t .^ 2;
• Functions:
s = u * t - g
sin(x), plot(x)
• Commands: load, save, clear
SUNY-New Paltz
Vectorization of Formulae
1- Case of Scalar
A = 750;
r = 0.09;
n = 10;
B = A * (1 + r) ^ n;
disp( [A B] )
750.00
SUNY-New Paltz
1775.52
Vectorization of Formulae
2- Case of Mutiple Investment
A = [750 1000 3000 5000 11999];
r = 0.09;
n = 10;
B = A * (1 + r) ^ n;
disp( [A’ B’] )
750.00
1000.00
3000.00
5000.00
11999.00
SUNY-New Paltz
1775.52
2367.36
7102.09
11836.82
28406.00
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
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
Repeating with for
for i = 1:5
disp(i)
end
for i=0:2:1000
disp([i i*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
Avoid Loops!
s = 0;
for n = 1:100000
s = s + n;
end;
n = 1:100000;
s = sum( n );
Find x=1+1/2+1/3+1/4…. + 1/1000
SUNY-New Paltz
Decisions
if r > 0.5
disp( ’greater indeed’ )
end
Relational Operator
Relational operators
Meaning
<
<=
==
~=
>
>=
less than
less than or equal
equal
not equal
greater than
greater than or equal
SUNY-New Paltz
Decisions
if condition1
statementsA
elseif condition2
statementsB
elseif condition3
statementsC
...
else
statementsE
end
if condition
statementsA
else
statementsB
end
SUNY-New Paltz
Logical operators
‘~’ ‘&’ ‘ |’
if bal >= 5000 & bal < 10000
SUNY-New Paltz
Nested if’s
x1, 2
 b  b 2  4ac

2a
ax2  bx  c  0
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
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
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
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
1.0000 + 1.0000i 2.0000 + 2.0000i
a = [1+i, 2+2i; 3+3i, 4+4i]
3.0000 + 3.0000i 4.0000 + 4.0000i
SUNY-New Paltz