Download Computer Simulation Lab

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Computer Simulation Lab
“Lecture 3”
Electrical and Computer Engineering
Department
SUNY – New Paltz
SUNY-New Paltz
Developing algorithms


structure plan (pseudo- code)
systematic procedure or algorithm
SUNY-New Paltz
Structure plans
Solving Quadratic
Equation
ax2 + bx + c = 0
•Input data
•Second Order(a=0)?
•If not x=-c/b
•Is (b2-4ca)<0
•If yes then complex roots
•Otherwise x1,2 = (± b + √b2 − 4ac)/(2a)
SUNY-New Paltz
Pseudo Code
Start
Input data (a, b, c)
If a = 0 then
If b = 0 then
If c = 0 then
Display ‘Solution indeterminate’
else
Display ‘There is no solution’
else
•Input data
•Second Order(a=0)?
•If not x=-c/b
x = −c/b
Display x (only one root: equation is linear)
else if b2 < 4ac then
Display ‘Complex roots’
else if b2 = 4ac then
x = −b/(2a)
Display x (equal roots)
•Is (b2-4ca)<0
•If yes then complex roots
•Otherwise x1,2 = (± b + √b2 − 4ac)/(2a)
else
x1 = (−b + √b2 − 4ac)/(2a)
x2 = (−b − √b2 − 4ac)/(2a)
Display x1, x2
Stop.
SUNY-New Paltz
MATLAB
(2)
CODE
Start
a = input('Enter a :');
Input data (a, b, c)
b = input('Enter b :');
If a = 0 then
c = input('Enter c :');
If b = 0 then
if a == 0
If c = 0 then
if b == 0
Display ‘Solution indeterminate’
if c == 0
else
display( 'Solution indeterminate!');
Display ‘There is no solution’
else
else
display('There is no solution');
end
x = −c/b
else
Display x (only one root: equation is linear)
x==-c/b;
else if b2 < 4ac then
display(['only one root: equation is linear, x=', num2str(x)]);
Display ‘Complex roots’
end
else if b2 = 4ac then
else if b*b < 4*a*c
x = −b/(2a)
display('Complex roots')
Display x (equal roots)
else if b*b == 4*a*c
else
x = -b/(2*a)
x1 = (−b + √b2 − 4ac)/(2a)
display(['equal roots, x1=x2=',num2str(x)]);
x2 = (−b − √b2 − 4ac)/(2a)
else
Display x1, x2
x1 = (-b + sqrt(b*b - 4*a*c))/(2*a);
Stop.
x2 = (-b - sqrt(b*b - 4*a*c))/(2*a);
display (['distinct roots x1=', num2str(x1),' x2=', num2str(x2)]);
end
end
end
SUNY-New Paltz
Flow Chart
START
INPUT
COEFFICIENTS
N
b2 –
4ac>0
?
a = 0
?
Y
x1,2 =(±b+sqrt(b2 – 4ac))/2a
b = 0
?
N
x=-c/b
Y
COMPLEX
SOLUTIONS!
c = 0
?
N
THERE IS NO
SOLUTION!
SUNY-New
Paltz
SOLUTION
INDETERMINATE!
MATLAB
(3)
CODE START
INPUT
a = input('Enter a :');
COEFFICIENTS
b = input('Enter b :');
c = input('Enter c :');
if a == 0
if b == 0
N
N
b2 –
a = 0
if c == 0
4ac>0
?
display( 'Solution indeterminate!');
?
else
Y
display('There is no solution');
Y
end
x1,2 =(±b+sqrt(b2 –
N
else
4ac))/2a
b = 0
x==-c/b;
?
display(['only one root: equation is linear, x=', num2str(x)]);
Y
end
else if b*b < 4*a*c
Y
display('Complex roots')
c = 0
?
else if b*b == 4*a*c
COMPLEX
x = -b/(2*a)
N
SOLUTIONS!
display(['equal roots, x1=x2=',num2str(x)]);
else
THERE IS NO
x1 = (-b + sqrt(b*b - 4*a*c))/(2*a);
SOLUTION!
x2 = (-b - sqrt(b*b - 4*a*c))/(2*a);
display (['distinct roots x1=', num2str(x1),' x2=', num2str(x2)]);
end
end
SUNY-New Paltz
end
x=-c/b
SOLUTION
INDETERMINATE
!
MATLAB functions
x = ut cos(a), y = ut sin(a) − gt2/2
V = sqrt( (u * cos(a))^2 + (u *
sin(a) - g * t)^2 );
SUNY-New Paltz
Trigonometric Functions
sin(x)
cos(x)
tan(x)
cot(x)
asin(x)
acos(x)
atan(x)
atan2(y, x)
sinh(x)
cosh(x)
tanh(x)
asinh(x)
acosh(x)
atanh(x)
sec(x)
csc(x)
sine of x
cosine of x.
tangent of x.
cotangent of x.
arc sine (inverse sine) of x between −π/2 and π/2.
arc cosine (inverse cosine) of x between 0 and π.
arc tangent of x between −π/2 and π/2.
arc tangent of y/x between −π and π.
hyperbolic sine of x
hyperbolic cosine of x,
hyperbolic tangent of x..
inverse hyperbolic sine of x, i.e. ln(x + √x2 + 1).
inverse hyperbolic cosine of x, i.e. ln(x + √x2 − 1)
inverse hyperbolic tangent of x, i.e.1/2 ln[(1 + x)/(1 − x)].
secant of x.
cosecant of x.
SUNY-New Paltz
Mathematical Functions
abs(x)
log(x)
log10(x)
pow2(x)
exp(x)
rand
realmax
realmin
rem(x,y)
max(x)
mean(x)
min(x)
cumsum(x)
prod(x)
absolute value of x.
natural logarithm of x.
base 10 logarithm of x.
2x
value of the exponential function ex
pseudo-random number in the interval [0, 1).
largest positive floating point number on your computer.
smallest positive floating point number on your computer
remainder when x is divided by y, e.g. rem(19, 5) returns 4
(5 goes 3 times into 19, remainder 4).
maximum element of vector x.
mean value of elements of vector x.
minimum element of vector x.
cumulative sum of the elements of x, e.g. cumsum(1:4) returns [1 3 6 10]
product of the elements of x
SUNY-New Paltz
Utility Functions
clock
Date
time and date in a six-element vector, e.g. the statements
date in a string in dd-mmm-yyyy format, e.g. 02-Feb-2001, which
is thankfully Y2K compliant!
tic, toc
execution time of code between these two functions is displayed.
floor(x) largest integer not exceeding x, i.e. rounds down to nearest
integer, e.g. floor(-3.9) returns -4, floor(3.9) returns 3
ceil(x)
smallest integer which exceeds x, i.e. rounds up to nearest
integer, e.g. ceil(-3.9) returns -3, ceil(3.9) returns 4
fix(x)
rounds to the nearest integer towards zero, e.g. fix(-3.9) returns 3, fix(3.9) returns 3
round(x) rounds to the integer nearest to x, e.g. round(4.49) returns 4, round(4.5)
returns 5
plot(x,y) plot y versus x
length(x) number of elements of vector x
size(a)
number of rows and columns of matrix a.
sort(x)
sorts elements of vector x into ascending order (by columns if x is a matrix).
sign(x)
returns -1, 0 or 1 depending on whether x is negative, zero or positive.
SUNY-New Paltz
Exercises
• Plot sin(x) and cos(x) on the same axis
•Find the integers between 0 and 10000 that
are divisible by 7
• Find a random number between 1 and 10.
Then repeat the process for N random
numbers. Find the frequency of occurrence
of the numbers ( 1 thru 10)
SUNY-New Paltz
Related documents