140 likes | 178 Views
Computer Simulation. “Lecture 4”. Electrical and Computer Engineering Department SUNY – New Paltz. Developing algorithms. · structure plan (pseudo- code) · systematic procedure or algorithm. What is an Algorithm?.
E N D
Computer Simulation “Lecture 4” Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz
Developing algorithms · structure plan (pseudo- code) ·systematic procedureor 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
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 b2 – 4ac>0? N a = 0 ? Y x1,2 =(±b+sqrt(b2 – 4ac))/2a b = 0 ? N x=-c/b c = 0 ? Y N Flow Chart Input coefficients Y Y SOLUTION INDETERMINATE! COMPLEX SOLUTIONS! THERE IS NO SOLUTION! 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
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