1 / 14

Computer Simulation

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

fredgarcia
Download Presentation

Computer Simulation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Simulation “Lecture 4” Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz

  2. Developing algorithms · structure plan (pseudo- code) ·systematic procedureor algorithm SUNY-New Paltz

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

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

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

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

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

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

  9. Components of Flowchart SUNY-New Paltz

  10. Example of a Flowchart SUNY-New Paltz

  11. Example of a Flowchart SUNY-New Paltz

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

  13. Exercise SUNY-New Paltz

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

More Related