Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 SUNY-New Paltz Developing algorithms  structure plan (pseudo- code)  systematic procedure or algorithm

3 SUNY-New Paltz Structure plans Solving Quadratic Equation ax 2 + bx + c = 0 Input data Second Order(a=0)? If not x=-c/b Is (b 2 -4ca)<0 If yes then complex roots Otherwise x 1,2 = (± b + √b2 − 4ac)/(2a)

4 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 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 (b 2 -4ca)<0 If yes then complex roots Otherwise x 1,2 = (± b + √b2 − 4ac)/(2a)

5 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)]); end else 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 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.

6 SUNY-New Paltz Flow Chart N Y N N Y START INPUT COEFFICIENTS a = 0 ? b = 0 ? THERE IS NO SOLUTION! x=-c/b b 2 – 4ac>0 ? x 1,2 =( ±b+sqrt(b 2 – 4ac))/2a COMPLEX SOLUTIONS! c = 0 ? SOLUTION INDETERMINATE!

7 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)]); end else 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 N Y N N Y START INPUT COEFFICIENTS a = 0 ? b = 0 ? THERE IS NO SOLUTION! x=-c/b b 2 – 4ac>0 ? x 1,2 =( ±b+sqrt(b 2 – 4ac))/2a COMPLEX SOLUTIONS! c = 0 ? SOLUTION INDETERMINATE !

8 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 );

9 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.

10 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)2 x exp(x)value of the exponential function ex randpseudo-random number in the interval [0, 1). realmaxlargest 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

11 SUNY-New Paltz Utility Functions clocktime and date in a six-element vector, e.g. the statements Datedate in a string in dd-mmm-yyyy format, e.g. 02-Feb-2001, which is thankfully Y2K compliant! tic, tocexecution 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.

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


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

Similar presentations


Ads by Google