Electrical and Computer Engineering Department SUNY – New Paltz Computer Simulation “Lecture 4” Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz
Developing algorithms · structure plan (pseudo- code) · systematic procedure or algorithm SUNY-New Paltz
What is an Algorithm? In mathematics and computer science, an algorithm is an unambiguous specification of how to solve a class of problems. Starting from an initial state and initial input, the instructions describe a computation that, when executed, proceeds through a finite number of well-defined successive states, eventually producing "output" SUNY-New Paltz
Solving Quadratic Equation 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 Second Order(a=0)? If not x=-c/b 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’ 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) 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) SUNY-New Paltz
MATLAB CODE(2) SUNY-New Paltz a = input('Enter a :'); 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’ 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) 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 x==-c/b; display(['only one root: equation is linear, x=', num2str(x)]); 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)]); 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)]); SUNY-New Paltz
SOLUTION INDETERMINATE! Flow Chart N Y START a = 0 ? b = 0 x=-c/b b2 – 4ac>0? x1,2 =(±b+sqrt(b2 – 4ac))/2a c = 0 Input coefficients Y Y SOLUTION INDETERMINATE! COMPLEX SOLUTIONS! THERE IS NO SOLUTION! SUNY-New Paltz
SOLUTION INDETERMINATE! MATLAB CODE(3) 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! 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 x==-c/b; display(['only one root: equation is linear, x=', num2str(x)]); 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)]); 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)]); N Y Y SUNY-New Paltz
Components of Flowchart SUNY-New Paltz
Example of a Flowchart SUNY-New Paltz
Example of a Flowchart SUNY-New Paltz
Exercise Write a flow chart that implements the following algorithm: A single die is rolled If it is even, print “Even Roll!” If it is odd, print “Odd Roll” Modify the process to roll indefinitely! SUNY-New Paltz
Exercise SUNY-New Paltz
Exercise Write a flow chart that implements the following algorithm: Set vector V=[ 2, 5, -1, 6, 0] Initialize variable maximum myMax = 2 Search through the vector and compare all the elements of the vector to find the maximum of the vector. SUNY-New Paltz