120 likes | 262 Views
Computer Simulation Lab. “Lecture 3”. Electrical and Computer Engineering Department SUNY – New Paltz. Developing algorithms. · structure plan (pseudo- code) · systematic procedure or algorithm. Structure plans. Solving Quadratic Equation ax 2 + bx + c = 0. Input data.
E N D
Computer Simulation Lab “Lecture 3” Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz
Developing algorithms · structure plan (pseudo- code) ·systematic procedureor 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 • Input data 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. • 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
MATLAB CODE(2) 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. 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 end end SUNY-New Paltz
START INPUT COEFFICIENTS b2 – 4ac>0 ? N a = 0 ? Y x1,2 =(±b+sqrt(b2 – 4ac))/2a b = 0 ? N x=-c/b Y c = 0 ? SOLUTION INDETERMINATE! COMPLEX SOLUTIONS! N THERE IS NO SOLUTION! Flow Chart SUNY-New Paltz
START INPUT COEFFICIENTS b2 – 4ac>0 ? a = 0 ? N Y x1,2 =(±b+sqrt(b2 – 4ac))/2a b = 0 ? N x=-c/b c = 0 ? SOLUTION INDETERMINATE! Y COMPLEX SOLUTIONS! N THERE IS NO SOLUTION! 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 end end N Y Y 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 ); SUNY-New Paltz
Trigonometric Functions 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. 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 ex rand 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 SUNY-New Paltz
Utility Functions clock time and date in a six-element vector, e.g. the statements Date 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