ece257 numerical methods and scientific computingchandy/courses/257f04/257ln02.pdf · ece 257...

50
ECE257 ECE257 Numerical Numerical Methods Methods and and Scientific Scientific Computing Computing Lecture 2 Lecture 2 Programming and Software Programming and Software Parts adapted from MIT 12.010 Lecture Notes, Thomas Herring

Upload: phamdien

Post on 05-Feb-2018

218 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE257ECE257 NumericalNumerical Methods Methods andandScientificScientific ComputingComputing

Lecture 2Lecture 2

Programming and SoftwareProgramming and Software

Parts adapted from MIT 12.010 Lecture Notes, Thomas Herring

Page 2: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Packages vs. ProgrammingPackages vs. Programming

•• PackagesPackages

–– MATLABMATLAB

–– MathematicaMathematica

–– MapleMaple

–– ExcelExcel

•• Packages do the work for youPackages do the work for you

•• Most offer an interactive environmentMost offer an interactive environment

Page 3: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

MATLABMATLAB

•• Software product from The MathWorks, Inc.Software product from The MathWorks, Inc.

•• Originally focused on matrix manipulationsOriginally focused on matrix manipulations

•• Interactive tool to do numerical functions, symbolicInteractive tool to do numerical functions, symboliccomputiations and visualizationcomputiations and visualization

•• Commands can be saved into user scripts call andCommands can be saved into user scripts call andm-filem-file

•• Graphics and Graphical user interfaces (GUI) areGraphics and Graphical user interfaces (GUI) arebuilt into programbuilt into program

Page 4: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

MATLABMATLAB

Page 5: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

MATLABMATLAB

•• Base product is extended with a variety ofBase product is extended with a variety of““toolboxestoolboxes””

–– OptimizationOptimization

–– StatisticsStatistics

–– Curve fittingCurve fitting

–– Image ProcessingImage Processing

Page 6: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

MATLABMATLAB

•• Example: Find roots ofExample: Find roots of

>> p = [1 -6 -72 -27]>> p = [1 -6 -72 -27]

>> r = roots(p)>> r = roots(p)

>> r =>> r =

12.122912.1229

-5.7345-5.7345

-0.3884-0.3884

x 3 − 6x 2 − 72x − 27

Page 7: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

MATLABMATLAB

•• Advantages:Advantages:

–– Large library of functions to evaluate a wide variety ofLarge library of functions to evaluate a wide variety ofnumerical problemsnumerical problems

–– Interactive tool allows immediate evaluation of resultsInteractive tool allows immediate evaluation of results

•• DisadvantagesDisadvantages

–– ExpensiveExpensive

–– Not as fast as C/C++/FORTRAN codeNot as fast as C/C++/FORTRAN code

Page 8: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Symbolic ComputationSymbolic Computation

•• Products include Mathematica and MapleProducts include Mathematica and Maple

•• These products attempt to do analyticalThese products attempt to do analyticalsolutions of equations instead of numericalsolutions of equations instead of numericalmethodsmethods

•• You get an equation back as the answerYou get an equation back as the answerrather than numbersrather than numbers

Page 9: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Programming language conceptsProgramming language concepts

•• Interpreted LanguagesInterpreted Languages

–– MATLAB, Java, BASIC, VBMATLAB, Java, BASIC, VB

–– Usually integrated with an interactive GUIUsually integrated with an interactive GUI

–– Scripting basedScripting based

–– Many interpreted languages do not have typesMany interpreted languages do not have types

–– Allows quick evaluation of algorithmsAllows quick evaluation of algorithms

–– Ideal for debugging and one-time analysis and inIdeal for debugging and one-time analysis and incases where high speed is not criticalcases where high speed is not critical

Page 10: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Programming language conceptsProgramming language concepts

•• Compiled LanguagesCompiled Languages

–– FORTRAN, C, C++FORTRAN, C, C++

–– Source code is created with an editor as a plain textSource code is created with an editor as a plain textfile.file.

–– Programs then needs to be Programs then needs to be ““compiledcompiled”” (converted (convertedfrom source code to machine instructions withfrom source code to machine instructions withrelative addresses and undefined external routinesrelative addresses and undefined external routinesstill needed).still needed).

–– The compiled routines (called object modules) needThe compiled routines (called object modules) needthen to be linked with system libraries.then to be linked with system libraries.

–– Faster execution than interpreted languagesFaster execution than interpreted languages

–– Static type checkingStatic type checking

Page 11: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Programming conceptsProgramming concepts

•• Data representationData representation

–– Constants, variables, type declarationsConstants, variables, type declarationsint x;int x; // integer type declaration// integer type declarationconst g=9.8;const g=9.8; // constant// constant

•• Data organization and structuresData organization and structures

–– Arrays, lists, trees, etc.Arrays, lists, trees, etc.int matrix[10][10]; // 2-d integer arrayint matrix[10][10]; // 2-d integer arraystruct {struct { // linked list node // linked list nodestruct node *next;struct node *next;

... ...} node;} node;

Page 12: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Programming conceptsProgramming concepts

•• Mathematical formulasMathematical formulas

–– Assignment, precedence rulesAssignment, precedence rules

x = y+5;x = y+5;y = x * 4;y = x * 4;

•• Input/OutputInput/Output

–– Stdin/Stdout, files, GUIStdin/Stdout, files, GUI

printf( printf( ““Hello, worldHello, world”” ); );outf << outf << ““x =x =““ << x; << x;

Page 13: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Programming conceptsProgramming concepts

•• Structured programmingStructured programming

–– Set of rules that prescribe goodSet of rules that prescribe goodprogramming styleprogramming style

–– Single entry point and exit point to all controlSingle entry point and exit point to all controlstructuresstructures

–– Modular programmingModular programming

–– Introduced by ALGOL and continued on byIntroduced by ALGOL and continued on byPASCAL, C and their derivativesPASCAL, C and their derivatives

Page 14: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Structured programmingStructured programming

•• Control structuresControl structures

–– SequenceSequence

–– SelectionSelection

–– RepetitionRepetition

Page 15: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Algorithm expressionAlgorithm expression

•• FlowchartsFlowcharts

–– Visual representationVisual representation

–– Useful in planningUseful in planning

•• PseudocodePseudocode

–– Simple representation of an algorithmSimple representation of an algorithm

–– Basic program constructs without beingBasic program constructs without beinglanguage-specificlanguage-specific

Page 16: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

FlowchartsFlowcharts

From Numerical Methods for Engineers, Chapra and Canale, Copyright © The McGraw-Hill Companies, Inc.

Page 17: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Control structuresControl structures

•• SequenceSequence

From Numerical Methods for Engineers, Chapra and Canale, Copyright ©The McGraw-Hill Companies, Inc.

Page 18: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Control structuresControl structures

•• SelectionSelection

–– BranchingBranching

•• IF/THEN/ELSEIF/THEN/ELSE

•• CASE/ELSECASE/ELSE

From Numerical Methods for Engineers, Chapra and Canale, Copyright ©The McGraw-Hill Companies, Inc.

Page 19: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Control structuresControl structures

From Numerical Methods for Engineers, Chapra and Canale, Copyright © The McGraw-Hill Companies, Inc.

Page 20: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Control structuresControl structures

From Numerical Methods for Engineers, Chapra and Canale, Copyright © The McGraw-Hill Companies, Inc.

Page 21: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Control structuresControl structures

•• RepetitionRepetition

–– DOEXIT loops (break loop)DOEXIT loops (break loop)

•• Similar to C while loopSimilar to C while loop

–– DOFOR loop (count-controlled)DOFOR loop (count-controlled)

Page 22: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Control structuresControl structures

From Numerical Methods for Engineers, Chapra and Canale, Copyright © The McGraw-Hill Companies, Inc.

Page 23: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Control structuresControl structures

From Numerical Methods for Engineers, Chapra and Canale, Copyright © The McGraw-Hill Companies, Inc.

Page 24: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Modular programmingModular programming

•• Hierarchical approach to program structureHierarchical approach to program structure

•• Well-defined modules with a single entry point andWell-defined modules with a single entry point andsingle exit pointsingle exit point

•• ProceduresProcedures and and subroutines subroutines do not return anydo not return anyresultsresults

•• FunctionsFunctions return a single result - some languages return a single result - some languageshave mechanisms to return multiple resultshave mechanisms to return multiple results

•• Always a good idea to return a value for status orAlways a good idea to return a value for status orerror codeerror code

Page 25: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Modular programmingModular programming

•• Makes logic easier to understand and digestMakes logic easier to understand and digest

•• Allows black-box development of modulesAllows black-box development of modules

•• Requires well-defined interfaces with no sideRequires well-defined interfaces with no sideeffects (i.e. unintended consequences)effects (i.e. unintended consequences)

•• Allows for reuse of modules and development ofAllows for reuse of modules and development oflibrarieslibraries

Page 26: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Object-oriented programmingObject-oriented programming

•• The next step in modular programmingThe next step in modular programming

•• Three key pointsThree key points

–– EncapsulationEncapsulation

–– InheritanceInheritance

–– PolymorphismPolymorphism

Page 27: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

EncapsulationEncapsulation•• Classes or InterfacesClasses or Interfaces

•• Apply the idea of modularity to data as well asApply the idea of modularity to data as well asfunctionalityfunctionality

•• Code is data oriented rather than procedureCode is data oriented rather than procedureorientedoriented

•• Every piece of data or object is associated withEvery piece of data or object is associated withfunctions (or methods) that operate on it.functions (or methods) that operate on it.

Page 28: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

EncapsulationEncapsulation

•• Objects are instances of classesObjects are instances of classes

•• An object is a container for other dataAn object is a container for other dataobjects which are called objects which are called propertiesproperties of the of theobjectobject

•• Example:Example:

–– Student transcript recordStudent transcript record

Page 29: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

EncapsulationEncapsulation

Class Transcript {Class Transcript {

Student &student;Student &student;

Course courses[];Course courses[];

void print();void print();

};};

Class Student {Class Student {

string name;string name;

int id;int id;

void print();void print();

};};

Page 30: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

InheritanceInheritance

•• Objects can inherit behaviors and dataObjects can inherit behaviors and datastructures from parent structuresstructures from parent structures

•• Allows for reuse of codeAllows for reuse of code

•• Can create new classes with lots ofCan create new classes with lots offunctionality and writing little codefunctionality and writing little code

Page 31: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

InheritanceInheritance

Class Person {Class Person {

string name;string name;

void print();void print();

};};

Class Student {Class Student {

int id;int id;

};};

Page 32: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

PolymorphismPolymorphism

•• Polymorphism is a way for inherited classesPolymorphism is a way for inherited classesto override methods in base classesto override methods in base classes

•• The inherited class still maintains typeThe inherited class still maintains typecompatibility with the base classcompatibility with the base class

Page 33: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

PolymorphismPolymorphism

Class Person {Class Person {

string name;string name;

virtual void print() {printf(virtual void print() {printf(““%s%s””, name);, name);

};};

Class Student : Person {Class Student : Person {

int id;int id;

void print() {printf(void print() {printf(““%d: %s%d: %s””, id, name);, id, name);

};};

Page 34: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

PolymorphismPolymorphism

Student s(Student s(““JoeJoe””, 1);, 1);

Person p = s;Person p = s;

p.print();p.print();

Without polymorphism:Without polymorphism:

JoeJoeWith polymorphism:With polymorphism:

1: Joe1: Joe

Page 35: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Software engineering approachSoftware engineering approach

•• Specification: Specification: A clear statement of the problem, theA clear statement of the problem, therequirements, and any special parameters of operationrequirements, and any special parameters of operation

•• Algorithm/DesignAlgorithm/Design: A flow chart or pseudocode: A flow chart or pseudocoderepresentation of how exactly how will the problem berepresentation of how exactly how will the problem besolved.solved.

•• Implementation:Implementation: Breaking the algorithm into Breaking the algorithm intomanageable pieces that can be coded into the languagemanageable pieces that can be coded into the languageof choice, and putting all the pieces together to solve theof choice, and putting all the pieces together to solve theproblem.problem.

•• VerificationVerification: Checking that the implementation solves: Checking that the implementation solvesthe original specification. In numerical problems, this isthe original specification. In numerical problems, this isdifficult because most of the time you dondifficult because most of the time you don’’t know whatt know whatthe correct answer is.the correct answer is.

Page 36: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Example caseExample case

•• How do you solve the sum of integersHow do you solve the sum of integersproblem?problem?

S = ii= 0

n

Page 37: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

PossiblePossible solutionssolutions

(a) Simplest(a) Simplest

sum = 1 + 2 + 3 + 4 + 5 + .... + Nsum = 1 + 2 + 3 + 4 + 5 + .... + N

–– Coded specifically forCoded specifically for specific values of N. specific values of N.Not very flexible; could be many optionsNot very flexible; could be many optionsdepending on number of values of Ndepending on number of values of Npossible.possible.

Page 38: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

(b) Intermediate solution(b) Intermediate solution

•• Does not require much thought, takesDoes not require much thought, takesadvantage of looping ability of mostadvantage of looping ability of mostlanguages:languages:

–– C/C++C/C++::int sumint sum = = 0;0;

For ( int i=1; i<=n; i++ ) {For ( int i=1; i<=n; i++ ) {

sum sum = = sum + isum + i

}}

–– MATLABMATLAB::sumsum = 0; = 0;

for i = 1for i = 1:n:n ; ;

sumsum = = sumsum + i ; + i ;

end ;end ;

Page 39: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

(c) (c) AnalyticalAnalytical solution solution

•• (c) Requires some thought about the(c) Requires some thought about thesequence sequence - remember back to one of your- remember back to one of yourmath classes.math classes.sumsum = = n*(n+n*(n+1)/21)/2

Page 40: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Verification of algorithmVerification of algorithm

•• What can fail in the above algorithms. (To know allWhat can fail in the above algorithms. (To know allthe possible failure modes requires knowledge ofthe possible failure modes requires knowledge ofhow computers work). Some examples of failureshow computers work). Some examples of failuresare:are:

•• For (a):What happens if specific value of N neededFor (a):What happens if specific value of N neededis not coded?is not coded?

Need some exception handlingNeed some exception handling..

•• For (b): This algorithm is fairly robust. (See (c) forFor (b): This algorithm is fairly robust. (See (c) forsome possible problems).some possible problems).

When N is large execution will be slow compared toWhen N is large execution will be slow compared to(c).(c).

Page 41: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Algorithm (c)Algorithm (c)

•• This is most common algorithm for this typeThis is most common algorithm for this typeof problem, and it has many potential failureof problem, and it has many potential failuremodes. For example:modes. For example:

•• (c.1) What if N is less than zero?(c.1) What if N is less than zero?

Still returns an answer but not what would beStill returns an answer but not what would beexpected. (What happens in (b) if N isexpected. (What happens in (b) if N isnegative?).negative?).

Page 42: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Algorithm (cAlgorithm (c))

•• (c.2) In which order will the multiplication and(c.2) In which order will the multiplication anddivision be done.division be done.

For all values of N, either N or N+1 will be even andFor all values of N, either N or N+1 will be even andtherefore N*(N+1) will always be even but what iftherefore N*(N+1) will always be even but what ifthe division is done first? Algorithm will work half ofthe division is done first? Algorithm will work half ofthe time.the time.

If division is done first and N+1 is odd, the algorithmIf division is done first and N+1 is odd, the algorithmwill return a result but it will not be correct. This is awill return a result but it will not be correct. This is abug. For verification, it means the algorithm worksbug. For verification, it means the algorithm workssometimes but not always.)sometimes but not always.)

Page 43: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Algorithm (cAlgorithm (c))

•• (c.3) What if N is very large?(c.3) What if N is very large?

What is the maximum value N*(N+1) can have?What is the maximum value N*(N+1) can have?There are maximum values that integers can beThere are maximum values that integers can berepresented in a computer and we may overflow.represented in a computer and we may overflow.What happens then? Can we code this to handleWhat happens then? Can we code this to handlelarge values of N?large values of N?

•• We will return later to algorithm development whenWe will return later to algorithm development whenlanguages are discussed in more detaillanguages are discussed in more detail

Page 44: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Input/Output (IO)Input/Output (IO)

•• Important part of any program since this is whereImportant part of any program since this is wherehuman interaction occurs.human interaction occurs.

•• Generally programs start with some type of input toGenerally programs start with some type of input tobe supplied by human, and end with output ofbe supplied by human, and end with output ofresults that humans need to interpret.results that humans need to interpret.

•• IO comes in many forms.IO comes in many forms.

–– Maybe simple reading of keyboard for user reply orMaybe simple reading of keyboard for user reply orcould be interrupt driven reading from serial orcould be interrupt driven reading from serial ormodem port (real-time data transfer).modem port (real-time data transfer).

–– We will cover file and screen IO but not real-time IOWe will cover file and screen IO but not real-time IOoperations (the latter are very platform dependent).operations (the latter are very platform dependent).

Page 45: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Input/Output (IOInput/Output (IO))

•• IO operations typically involve human interactionIO operations typically involve human interactionand therefore the code has to be very robust sinceand therefore the code has to be very robust sincethe actions of humans are unpredictable.the actions of humans are unpredictable.

•• The role of IO is important to consider whenThe role of IO is important to consider whendesigning an algorithm (Think of programs youdesigning an algorithm (Think of programs youhave used where either the input or the output ishave used where either the input or the output isnot satisfactory.not satisfactory.

•• IO can be problematic especially when an expectedIO can be problematic especially when an expectedinput never arrives.input never arrives.

Page 46: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

VerificationVerification

•• Basically: How do you know that the resultsBasically: How do you know that the resultsof your program are correct?of your program are correct?

•• The overall program is so large andThe overall program is so large andcomplex, that human checking of the thecomplex, that human checking of the theresults is usually not possible.results is usually not possible.

•• What is the correct answer, anyway?What is the correct answer, anyway?

•• There are no definite rules for this operation.There are no definite rules for this operation.

Page 47: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

VerificationVerification

•• By breaking the program into small modules, eachBy breaking the program into small modules, eachof which can be checked, the sum of the parts isof which can be checked, the sum of the parts islikely to be correct but not always.likely to be correct but not always.

•• Note:Note: getting a program to compile and run is only getting a program to compile and run is onlythe first (small) step in programming.the first (small) step in programming.

•• Problems can be that the program only works someProblems can be that the program only works someof the time (sound familiar), or it may not work on allof the time (sound familiar), or it may not work on allplatforms.platforms.

•• The critical issue to realize all possible cases thatThe critical issue to realize all possible cases thatmight occur.might occur.

Page 48: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

VerificationVerification

•• This takes experience and the more youThis takes experience and the more youknow about how computers work, the moreknow about how computers work, the morelikely you are to realize the exceptions.likely you are to realize the exceptions.

•• When humans are involved or you areWhen humans are involved or you areworking with instrument data streams youworking with instrument data streams youneed to be particularly carefulneed to be particularly careful..

Page 49: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Programming interface to OSProgramming interface to OS

•• POSIX is the standardPOSIX is the standard

•• Depending on language used, OS interfaceDepending on language used, OS interfacemay or may not be importantmay or may not be important

•• For FORTRAN, C, C++ when program isFor FORTRAN, C, C++ when program islinked OS routines are neededlinked OS routines are needed

–– How to read from keyboard or file?How to read from keyboard or file?

–– How to write to screen or disk?How to write to screen or disk?

Page 50: ECE257 Numerical Methods and Scientific Computingchandy/courses/257f04/257ln02.pdf · ECE 257 Numerical Methods and Scientific Computing Fall 2004 Lecture 1 John A. Chandy Dept. of

ECE 257 Numerical Methods and Scientific Computing

Fall 2004

Lecture 1

John A. Chandy

Dept. of Electrical and Computer Engineering

University of Connecticut

Next ClassNext Class

•• HW1, due 9/9HW1, due 9/9

–– Chapra & CanaleChapra & Canale

•• 1.4, 1.5, 1.9, 2.5, 2.151.4, 1.5, 1.9, 2.5, 2.15

•• Next classNext class

–– Computation complexityComputation complexity

–– ITE 330ITE 330