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

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

Embed Size (px)

Citation preview

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

SUNY-New Paltz

Computer Simulation Lab

Electrical and Computer Engineering Department

SUNY – New Paltz

“Lecture 3”

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

SUNY-New Paltz

Developing algorithms

       structure plan (pseudo- code)

        systematic procedure or algorithm

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

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)

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

SUNY-New Paltz

Pseudo CodeStart

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

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)

else

x1 = (−b + √b2 − 4ac)/(2a)

x2 = (−b − √b2 − 4ac)/(2a)

Display x1, x2

Stop.

•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)

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

SUNY-New Paltz

MATLAB CODE(2)a = input('Enter a :');b = input('Enter b :');c = input('Enter c :');if a == 0 if b == 0 if c == 0 display( 'Solution indeterminate!'); else display('There is no solution'); end else x==-c/b; display(['only one root: equation is linear, x=', num2str(x)]); endelse if b*b < 4*a*c display('Complex roots') else if b*b == 4*a*c x = -b/(2*a) display(['equal roots, x1=x2=',num2str(x)]); else x1 = (-b + sqrt(b*b - 4*a*c))/(2*a); x2 = (-b - sqrt(b*b - 4*a*c))/(2*a); display (['distinct roots x1=', num2str(x1),' x2=', num2str(x2)]); end endend

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

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)

else

x1 = (−b + √b2 − 4ac)/(2a)

x2 = (−b − √b2 − 4ac)/(2a)

Display x1, x2

Stop.

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

SUNY-New Paltz

Flow Chart

N

Y

N

N

Y

START

INPUT COEFFICIENTS

a = 0?

b = 0?

THERE IS NO SOLUTION!

x=-c/b

b2 – 4ac>0

?

x1,2 =(±b+sqrt(b2 – 4ac))/2a

COMPLEX SOLUTIONS!

c = 0?

SOLUTION INDETERMINATE!

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

Y

Y

N

SUNY-New Paltz

MATLAB CODE(3)

a = input('Enter a :');b = input('Enter b :');c = input('Enter c :');if a == 0 if b == 0 if c == 0 display( 'Solution indeterminate!'); else display('There is no solution'); end else x==-c/b; display(['only one root: equation is linear, x=', num2str(x)]); endelse if b*b < 4*a*c display('Complex roots') else if b*b == 4*a*c x = -b/(2*a) display(['equal roots, x1=x2=',num2str(x)]); else x1 = (-b + sqrt(b*b - 4*a*c))/(2*a); x2 = (-b - sqrt(b*b - 4*a*c))/(2*a); display (['distinct roots x1=', num2str(x1),' x2=',

num2str(x2)]); end endend

N

Y

N

N

Y

START

INPUT COEFFICIENTS

a = 0?

b = 0?

THERE IS NO SOLUTION!

x=-c/b

b2 – 4ac>0

?

x1,2 =(±b+sqrt(b2 – 4ac))/2a

COMPLEX SOLUTIONS!

c = 0?

SOLUTION INDETERMINATE

!

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

SUNY-New Paltz

MATLAB functions

x = ut cos(a), y = ut sin(a) − gt2/2

V = sqrt( (u * cos(a))^2 + (u * sin(a) - g * t)^2 );

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

Trigonometric Functions

SUNY-New Paltz

sin(x) sine of x

cos(x) cosine of x.

tan(x) tangent of x.

cot(x) cotangent of x.

asin(x) arc sine (inverse sine) of x between −π/2 and π/2.

acos(x) arc cosine (inverse cosine) of x between 0 and π.

atan(x) arc tangent of x between −π/2 and π/2.

atan2(y, x) arc tangent of y/x between −π and π.

sinh(x) hyperbolic sine of x

cosh(x) hyperbolic cosine of x,

tanh(x) hyperbolic tangent of x..

asinh(x) inverse hyperbolic sine of x, i.e. ln(x + √x2 + 1).

acosh(x) inverse hyperbolic cosine of x, i.e. ln(x + √x2 − 1)

atanh(x) inverse hyperbolic tangent of x, i.e.1/2 ln[(1 + x)/(1 − x)].

sec(x) secant of x.

csc(x) cosecant of x.

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

SUNY-New Paltz

Mathematical Functions

abs(x) absolute value of x.log(x) natural logarithm of x.log10(x) base 10 logarithm of x.pow2(x) 2x exp(x) value of the exponential function exrand pseudo-random number in the interval [0, 1). realmax largest positive floating point number on your computer.realmin smallest positive floating point number on your computer rem(x,y) remainder when x is divided by y, e.g. rem(19, 5) returns 4

(5 goes 3 times into 19, remainder 4). max(x) maximum element of vector x.mean(x) mean value of elements of vector x.min(x) minimum element of vector x.cumsum(x) cumulative sum of the elements of x, e.g. cumsum(1:4) returns [1 3 6 10]prod(x) product of the elements of x

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

SUNY-New Paltz

Utility Functions

clock time and date in a six-element vector, e.g. the statementsDate 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 3ceil(x) smallest integer which exceeds x, i.e. rounds up to nearest

integer, e.g. ceil(-3.9) returns -3, ceil(3.9) returns 4fix(x) rounds to the nearest integer towards zero, e.g. fix(-3.9) returns -

3, fix(3.9) returns 3round(x) rounds to the integer nearest to x, e.g. round(4.49) returns 4, round(4.5)

returns 5plot(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.

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

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)