INF1100 Lectures, Chapter 9: Object-Oriented Programming

Transcription

INF1100 Lectures, Chapter 9:Object-Oriented ProgrammingHans Petter LangtangenSimula Research LaboratoryUniversity of Oslo, Dept. of InformaticsNovember 05, 2009

Before we start: define the heading of this chapterObject-oriented programming (OO) means different things todifferent people:programming with classes (better: object-based programming)programming with class hierarchies (class families)The 2nd def. is most widely accepted and used here

New concept: collect classes in families (hierarchies)What is a class hierarchy?A family of closely related classesA key concept is inheritance: child classes can inheritattributes and methods from parent class(es) – this savesmuch typing and code duplicationAs usual, we shall learn through examplesOO is a Norwegian invention – one of the most importantinventions in computer science, because OO is used in all bigcomputer systems today

Warnings: OO is difficult and takes time to masterThe OO concept might be difficult to understandLet ideas mature with time and try to work with itOO is less important in Python than in C , Java and C#,so the benefits of OO are less obvious in PythonOur examples here on OO employ numerical methods fordifferentiation, integration og ODEs – make sure youunderstand the simplest of these numerical methods beforeyou study the combination of OO and numericsAmbitions: write simple OO code and understand how tomake use of ready-made OO modules

A class for straight linesLet us make a class for evaluating lines y c0 c1 xclass Line:def init (self, c0, c1):self.c0, self.c1 c0, c1def call (self, x):return self.c0 self.c1*xdef table(self, L, R, n):"""Return a table with n points for L x R."""s ’’for x in linspace(L, R, n):y self(x)s ’%12g %12g\n’ % (x, y)return s

A class for parabolasLet us make a class for evaluating parabolas y c0 c1 x c2 x 2class Parabola:def init (self, c0, c1, c2):self.c0, self.c1, self.c2 c0, c1, c2def call (self, x):return self.c2*x**2 self.c1*x self.c0def table(self, L, R, n):"""Return a table with n points for L x R."""s ’’for x in linspace(L, R, n):y self(x)s ’%12g %12g\n’ % (x, y)return sThis is almost the same code as class Line, except for the thingswith c2

Class Parabola as a subclass of Line; principlesParabolacode Line code a little extra with the c2 termCan we utilize class Line code in class Parabola?This is what inheritance is about!Writingclass Parabola(Line):passmakes Parabola inherit all methods and attributes from Line,so Parabola has attributes c0 and c1 and three methodsLine is a superclass, Parabola is a subclass(parent class, base class; child class, derived class)Class Parabola must add code to Line’s constructor (an extrac2 attribute), call (an extra term), but table can be usedunalteredThe principle is to reuse as much code in Line as possible andavoid duplicated code

Class Parabola as a subclass of Line; principlesParabolacode Line code a little extra with the c2 termCan we utilize class Line code in class Parabola?This is what inheritance is about!Writingclass Parabola(Line):passmakes Parabola inherit all methods and attributes from Line,so Parabola has attributes c0 and c1 and three methodsLine is a superclass, Parabola is a subclass(parent class, base class; child class, derived class)Class Parabola must add code to Line’s constructor (an extrac2 attribute), call (an extra term), but table can be usedunalteredThe principle is to reuse as much code in Line as possible andavoid duplicated code

Class Parabola as a subclass of Line; principlesParabolacode Line code a little extra with the c2 termCan we utilize class Line code in class Parabola?This is what inheritance is about!Writingclass Parabola(Line):passmakes Parabola inherit all methods and attributes from Line,so Parabola has attributes c0 and c1 and three methodsLine is a superclass, Parabola is a subclass(parent class, base class; child class, derived class)Class Parabola must add code to Line’s constructor (an extrac2 attribute), call (an extra term), but table can be usedunalteredThe principle is to reuse as much code in Line as possible andavoid duplicated code

Class Parabola as a subclass of Line; principlesParabolacode Line code a little extra with the c2 termCan we utilize class Line code in class Parabola?This is what inheritance is about!Writingclass Parabola(Line):passmakes Parabola inherit all methods and attributes from Line,so Parabola has attributes c0 and c1 and three methodsLine is a superclass, Parabola is a subclass(parent class, base class; child class, derived class)Class Parabola must add code to Line’s constructor (an extrac2 attribute), call (an extra term), but table can be usedunalteredThe principle is to reuse as much code in Line as possible andavoid duplicated code

Class Parabola as a subclass of Line; principlesParabolacode Line code a little extra with the c2 termCan we utilize class Line code in class Parabola?This is what inheritance is about!Writingclass Parabola(Line):passmakes Parabola inherit all methods and attributes from Line,so Parabola has attributes c0 and c1 and three methodsLine is a superclass, Parabola is a subclass(parent class, base class; child class, derived class)Class Parabola must add code to Line’s constructor (an extrac2 attribute), call (an extra term), but table can be usedunalteredThe principle is to reuse as much code in Line as possible andavoid duplicated code

Class Parabola as a subclass of Line; principlesParabolacode Line code a little extra with the c2 termCan we utilize class Line code in class Parabola?This is what inheritance is about!Writingclass Parabola(Line):passmakes Parabola inherit all methods and attributes from Line,so Parabola has attributes c0 and c1 and three methodsLine is a superclass, Parabola is a subclass(parent class, base class; child class, derived class)Class Parabola must add code to Line’s constructor (an extrac2 attribute), call (an extra term), but table can be usedunalteredThe principle is to reuse as much code in Line as possible andavoid duplicated code

Class Parabola as a subclass of Line; principlesParabolacode Line code a little extra with the c2 termCan we utilize class Line code in class Parabola?This is what inheritance is about!Writingclass Parabola(Line):passmakes Parabola inherit all methods and attributes from Line,so Parabola has attributes c0 and c1 and three methodsLine is a superclass, Parabola is a subclass(parent class, base class; child class, derived class)Class Parabola must add code to Line’s constructor (an extrac2 attribute), call (an extra term), but table can be usedunalteredThe principle is to reuse as much code in Line as possible andavoid duplicated code

Class Parabola as a subclass of Line; principlesParabolacode Line code a little extra with the c2 termCan we utilize class Line code in class Parabola?This is what inheritance is about!Writingclass Parabola(Line):passmakes Parabola inherit all methods and attributes from Line,so Parabola has attributes c0 and c1 and three methodsLine is a superclass, Parabola is a subclass(parent class, base class; child class, derived class)Class Parabola must add code to Line’s constructor (an extrac2 attribute), call (an extra term), but table can be usedunalteredThe principle is to reuse as much code in Line as possible andavoid duplicated code

Class Parabola as a subclass of Line; codeA subclass method can call a superclass method in this way:superclass name.method(self, arg1, arg2, .)Class Parabola as a subclass of Line:class Parabola(Line):def init (self, c0, c1, c2):Line. init (self, c0, c1)self.c2 c2# Line stores c0, c1def call (self, x):return Line. call (self, x) self.c2*x**2What is gained? class Parabola just adds code to the alreadyexisting code in class Line – no duplication of storing c0 andc1, and computing c0 c1 xClass Parabola also has a table method – it is inheritedThe constructor and call are overrided, meaning that thesubclass redefines these methods (here: redefined method call superclass method something extra)

Demo of class ParabolaMain program:p Parabola(1, -2, 2)p1 p(2.5)print p1print p.table(0, 1, 3)Output:8.500.5110.51Follow the program flow of p(2.5)!

Checking class types and class relations l Line(-1, 1) isinstance(l, Line)True isinstance(l, Parabola)False p Parabola(-1, 0, 10) isinstance(p, Parabola)True isinstance(p, Line)True issubclass(Parabola, Line)True issubclass(Line, Parabola)False p. class ParabolaTrue p. class . name# string version of the class name’Parabola’

Next example: class for functions (part 1)Suppose we want to implement mathematical functionsf (x; p1 , . . . , pn ) with parameters as classes. These classesshould also support computation of df /dx and d 2 f /dx 2Example on such a class:class SomeFunc:def init (self, parameter1, parameter2, .)# store parametersdef call (self, x):# evaluate functiondef df(self, x):# evaluate the first derivativedef ddf(self, x):# evaluate the second derivativeIf we do not bother to derive the analytical derivatives, we canuse numerical differentation formulas in df and ddf

Next example: class for functions (part 2)Observation: with numerical differentiation, all such classescontain the same df and ddf methodsSuch duplication of code is considered a bad thingBetter: collect df and ddf in a superclass and let subclassesautomatically inherit these methods

Super- and subclass for functionsclass FuncWithDerivatives:def init (self, h 1.0E-9):self.h h # spacing for numerical derivativesdef call (self, x):raise NotImplementedError\(’ call missing in class %s’ % \self. class . name )def df(self, x):# compute 1st derivative by a finite difference:h float(self.h) # short formreturn (self(x h) - self(x-h))/(2*h)def ddf(self, x):# compute 2nd derivative by a finite difference:h float(self.h) # short formreturn (self(x h) - 2*self(x) self(x-h))/h**2The superclass is not useful in itself – a subclass is needed toimplement a specific mathematical function

How to implement a specific function as a subclassInherit from superclass FuncWithDerivativesStore parameters in constructorImplement function formula in callRely on inherited df and ddf for numerical derivatives, orreimplement df and ddf with exact expressions

A subclass for a function; no direct use of superclassSay we want to implement f (x) cos(ax) x 3We do this in a subclass:class MyFunc(FuncWithDerivatives):def init (self, a):self.a adef call (self, x):return cos(self.a*x) x**3def df(self, x):a self.areturn -a*sin(a*x) 3*x**2def ddf(self, x):a self.areturn -a*a*cos(a*x) 6*xThis subclass inherits from the superclass, but does not makeuse of anything from the superclass – no practical use of thesuperclass in this example

A subclass for a function; use of superclassSay we want to implement f (x) ln p tanh(qx cos rx) We are lazy and want to avoid hand derivation of longexpressions for f ′ (x) and f ′′ (x) – use finite differences insteadImplementation as a subclass:class MyComplicatedFunc(FuncWithDerivatives):def init (self, p, q, r, h 1.0E-9):FuncWithDerivatives. init (self, h)self.p, self.q, self.r p, q, rdef call (self, x):p, q, r self.p, self.q, self.rreturn log(abs(p*tanh(q*x*cos(r*x))))This time we inherit df and ddfNote also that we must pass h on to the superclass constructorIt is always a good habit to call the superclass constructor

Interactive examplef (x) ln p tanh(qx cos rx) compute f (x), f ′ (x), f ′′ (x) for x π/2 from math import * f MyComplicatedFunc(1, 1, 1) x pi/2 f(x)-36.880306514638988 f.df(x)-60.593693618216086 f.ddf(x)3.3217246931444789e 19

Recall the class for numerical differentiation (Ch. 9)Simplest numerical differentiation formula:f ′ (x) f (x h) f (x)hfor a small h, say h 10 9Class Derivative stores f and h as attributes and applies thedifferentation formula in the call special methodclass Derivative:def init (self, f, h 1E-9):self.f fself.h float(h)def call (self, x):f, h self.f, self.hreturn (f(x h) - f(x))/h# make short forms

There are numerous formulas numerical differentiationf ′ (x) f ′ (x) f ′ (x) f ′ (x) f ′ (x) f ′ (x) f (x h) f (x) O(h)hf (x) f (x h) O(h)hf (x h) f (x h) O(h2 )2h4 f (x h) f (x h) 1 f (x 2h) f (x 2h) O(h4 )32h34h3 f (x h) f (x h) 3 f (x 2h) f (x 2h) 22h54h1 f (x 3h) f (x 3h) O(h6 )10 6h 1111 f (x 2h) f (x h) f (x) f (x h) O(h3 )h623

Why should these formulas be implemented in a classhierarchy?A numerical differentiation formula can be implemented as aclass: h and f are attributes and a call special methodevaluates the formulaThe Derivative class appears as a plain function in useAll classes for different differentiation formulas are similar: theconstructor is the same, only call differsCode duplication (of the constructors) is a bad thing ( rule!)The general OO idea: place code common to many classes ina superclass and inherit that code – here we let a superclassimplement the common constructorSubclasses extend the superclass with code specific to a mathformula

Why should these formulas be implemented in a classhierarchy?A numerical differentiation formula can be implemented as aclass: h and f are attributes and a call special methodevaluates the formulaThe Derivative class appears as a plain function in useAll classes for different differentiation formulas are similar: theconstructor is the same, only call differsCode duplication (of the constructors) is a bad thing ( rule!)The general OO idea: place code common to many classes ina superclass and inherit that code – here we let a superclassimplement the common constructorSubclasses extend the superclass with code specific to a mathformula

Why should these formulas be implemented in a classhierarchy?A numerical differentiation formula can be implemented as aclass: h and f are attributes and a call special methodevaluates the formulaThe Derivative class appears as a plain function in useAll classes for different differentiation formulas are similar: theconstructor is the same, only call differsCode duplication (of the constructors) is a bad thing ( rule!)The general OO idea: place code common to many classes ina superclass and inherit that code – here we let a superclassimplement the common constructorSubclasses extend the superclass with code specific to a mathformula

Why should these formulas be implemented in a classhierarchy?A numerical differentiation formula can be implemented as aclass: h and f are attributes and a call special methodevaluates the formulaThe Derivative class appears as a plain function in useAll classes for different differentiation formulas are similar: theconstructor is the same, only call differsCode duplication (of the constructors) is a bad thing ( rule!)The general OO idea: place code common to many classes ina superclass and inherit that code – here we let a superclassimplement the common constructorSubclasses extend the superclass with code specific to a mathformula

Why should these formulas be implemented in a classhierarchy?A numerical differentiation formula can be implemented as aclass: h and f are attributes and a call special methodevaluates the formulaThe Derivative class appears as a plain function in useAll classes for different differentiation formulas are similar: theconstructor is the same, only call differsCode duplication (of the constructors) is a bad thing ( rule!)The general OO idea: place code common to many classes ina superclass and inherit that code – here we let a superclassimplement the common constructorSubclasses extend the superclass with code specific to a mathformula

Why should these formulas be implemented in a classhierarchy?A numerical differentiation formula can be implemented as aclass: h and f are attributes and a call special methodevaluates the formulaThe Derivative class appears as a plain function in useAll classes for different differentiation formulas are similar: theconstructor is the same, only call differsCode duplication (of the constructors) is a bad thing ( rule!)The general OO idea: place code common to many classes ina superclass and inherit that code – here we let a superclassimplement the common constructorSubclasses extend the superclass with code specific to a mathformula

Why should these formulas be implemented in a classhierarchy?A numerical differentiation formula can be implemented as aclass: h and f are attributes and a call special methodevaluates the formulaThe Derivative class appears as a plain function in useAll classes for different differentiation formulas are similar: theconstructor is the same, only call differsCode duplication (of the constructors) is a bad thing ( rule!)The general OO idea: place code common to many classes ina superclass and inherit that code – here we let a superclassimplement the common constructorSubclasses extend the superclass with code specific to a mathformula

Class hierarchy for numerical differentiationSuperclass:class Diff:def init (self, f, h 1E-9):self.f fself.h float(h)Subclass for simple forward formula:class Forward1(Diff):def call (self, x):f, h self.f, self.hreturn (f(x h) - f(x))/hSubclass for 4-th order central formula:class Central4(Diff):def call (self, x):f, h self.f, self.hreturn (4./3)*(f(x h)- f(x-h)) /(2*h) - \(1./3)*(f(x 2*h) - f(x-2*h))/(4*h)

Use of the differentiation classesInteractive example: f (x) sin x, compute f ′ (x) for x π from Diff import * from math import sin mycos Central4(sin) # compute sin’(pi): mycos(pi)-1.000000082740371Note: Central4(sin) calls inherited constructor in superclass,while mycos(pi) calls call in the subclass Central4

A flexible main program for numerical differentiationSuppose we want to differentiate function expressions fromthe command line:Unix/DOS python df.py ’exp(sin(x))’ Central 2 3.1-1.04155573055Unix/DOS python df.py f(x) difftype difforder xf’(x)With eval and the Diff class hierarchy this main program canbe realized in a few lines (many lines in C# and Java!):import sysfrom Diff import *from math import *from scitools.StringFunction import StringFunctionf StringFunction(sys.argv[1])difftype sys.argv[2]difforder sys.argv[3]classname difftype difforderdf eval(classname ’(f)’)x float(sys.argv[4])print df(x)

A flexible main program for numerical differentiationSuppose we want to differentiate function expressions fromthe command line:Unix/DOS python df.py ’exp(sin(x))’ Central 2 3.1-1.04155573055Unix/DOS python df.py f(x) difftype difforder xf’(x)With eval and the Diff class hierarchy this main program canbe realized in a few lines (many lines in C# and Java!):import sysfrom Diff import *from math import *from scitools.StringFunction import StringFunctionf StringFunction(sys.argv[1])difftype sys.argv[2]difforder sys.argv[3]classname difftype difforderdf eval(classname ’(f)’)x float(sys.argv[4])print df(x)

A flexible main program for numerical differentiationSuppose we want to differentiate function expressions fromthe command line:Unix/DOS python df.py ’exp(sin(x))’ Central 2 3.1-1.04155573055Unix/DOS python df.py f(x) difftype difforder xf’(x)With eval and the Diff class hierarchy this main program canbe realized in a few lines (many lines in C# and Java!):import sysfrom Diff import *from math import *from scitools.StringFunction import StringFunctionf StringFunction(sys.argv[1])difftype sys.argv[2]difforder sys.argv[3]classname difftype difforderdf eval(classname ’(f)’)x float(sys.argv[4])print df(x)

Investigating numerical approximation errorsWe can empirically investigate the accuracy of our family of 6numerical differentiation formulasSample function: f (x) exp ( 10x)See the book for a little program that computes the .95E-03Forward1-2.56418286E 00-1.41170013E 08halving h from row to row reduces the errors by a factor of 2,4 and 16, i.e, the errors go like h, h2 , and h4Observe the superior accuracy of Central4 compared with thesimplef (x h) f (x)hForward1

Alternative implementations (in the book)Pure Python functionsdownside: more arguments to transfer, cannot apply formulastwice to get 2nd-order derivatives etc.Functional programminggives the same flexibility as the OO solutionOne class and one common math formulaapplies math notation instead of programming techniques togeneralize codeThese techniques are beyond scope in the course, but place OOinto a bigger perspective

Formulas for numerical integrationThere are numerous formulas for numerical integration and allof them can be put into a common notation:Zbf (x)dx an 1Xwi f (xi )i 0wi : weights, xi : points (specific to a certain formula)The Trapezoidal rule has h (b a)/(n 1) andxi a ih,w0 wn 1 h, wi h (i 6 0, n 1)2The Midpoint rule has h (b a)/n andxi a h ih,2wi h

More formulasSimpson’s rule hasxi a ih,h b an 1h62hhfor i oddwi for i even, wi 33Other rules have more complicated formulas for wi and xiw0 wn 1

Why should these formulas be implemented in a classhierarchy?A numerical integration formula can be implemented as aclass: a, b and n are attributes and an integrate methodevaluates the formulaWe argued in the class chapter that this is smartPAll such classes are quite similar: the evaluation of j wj f (xj )is the same, only the definition of the points and weightsdiffer among the classesRecall: code duplication is a bad thing!The general OO idea: place code common to manyP classes ina superclass and inherit that code – here we put j wj f (xj ) ina superclass (method integrate)Subclasses extend the superclass with code specific to a mathformula, i.e., definition of wi and xi in this case, in a methodconstruct rule

Why should these formulas be implemented in a classhierarchy?A numerical integration formula can be implemented as aclass: a, b and n are attributes and an integrate methodevaluates the formulaWe argued in the class chapter that this is smartPAll such classes are quite similar: the evaluation of j wj f (xj )is the same, only the definition of the points and weightsdiffer among the classesRecall: code duplication is a bad thing!The general OO idea: place code common to manyP classes ina superclass and inherit that code – here we put j wj f (xj ) ina superclass (method integrate)Subclasses extend the superclass with code specific to a mathformula, i.e., definition of wi and xi in this case, in a methodconstruct rule

Why should these formulas be implemented in a classhierarchy?A numerical integration formula can be implemented as aclass: a, b and n are attributes and an integrate methodevaluates the formulaWe argued in the class chapter that this is smartPAll such classes are quite similar: the evaluation of j wj f (xj )is the same, only the definition of the points and weightsdiffer among the classesRecall: code duplication is a bad thing!The general OO idea: place code common to manyP classes ina superclass and inherit that code – here we put j wj f (xj ) ina superclass (method integrate)Subclasses extend the superclass with code specific to a mathformula, i.e., definition of wi and xi in this case, in a methodconstruct rule

Why should these formulas be implemented in a classhierarchy?A numerical integration formula can be implemented as aclass: a, b and n are attributes and an integrate methodevaluates the formulaWe argued in the class chapter that this is smartPAll such classes are quite similar: the evaluation of j wj f (xj )is the same, only the definition of the points and weightsdiffer among the classesRecall: code duplication is a bad thing!The general OO idea: place code common to manyP classes ina superclass and inherit that code – here we put j wj f (xj ) ina superclass (method integrate)Subclasses extend the superclass with code specific to a mathformula, i.e., definition of wi and xi in this case, in a methodconstruct rule

Why should these formulas be implemented in a classhierarchy?A numerical integration formula can be implemented as aclass: a, b and n are attributes and an integrate methodevaluates the formulaWe argued in the class chapter that this is smartPAll such classes are quite similar: the evaluation of j wj f (xj )is the same, only the definition of the points and weightsdiffer among the classesRecall: code duplication is a bad thing!The general OO idea: place code common to manyP classes ina superclass and inherit that code – here we put j wj f (xj ) ina superclass (method integrate)Subclasses extend the superclass with code specific to a mathformula, i.e., definition of wi and xi in this case, in a methodconstruct rule

Why should these formulas be implemented in a classhierarchy?A numerical integration formula can be implemented as aclass: a, b and n are attributes and an integrate methodevaluates the formulaWe argued in the class chapter that this is smartPAll such classes are quite similar: the evaluation of j wj f (xj )is the same, only the definition of the points and weightsdiffer among the classesRecall: code duplication is a bad thing!The general OO idea: place code common to manyP classes ina superclass and inherit that code – here we put j wj f (xj ) ina superclass (method integrate)Subclasses extend the superclass with code specific to a mathformula, i.e., definition of wi and xi in this case, in a methodconstruct rule

Why should these formulas be implemented in a classhierarchy?A numerical integration formula can be implemented as aclass: a, b and n are attributes and an integrate methodevaluates the formulaWe argued in the class chapter that this is smartPAll such classes are quite similar: the evaluation of j wj f (xj )is the same, only the definition of the points and weightsdiffer among the classesRecall: code duplication is a bad thing!The general OO idea: place code common to manyP classes ina superclass and inherit that code – here we put j wj f (xj ) ina superclass (method integrate)Subclasses extend the superclass with code specific to a mathformula, i.e., definition of wi and xi in this case, in a methodconstruct rule

The superclass for integrationclass Integrator:def init (self, a, b, n):self.a, self.b, self.n a, b, nself.points, self.weights self.construct method()def construct method(self):raise NotImplementedError(’no rule in class %s’ %def integrate(self, f):s 0for i in range(len(self.weights)):s self.weights[i]*f(self.points[i])return sdef vectorized integrate(self, f):# f must be vectorizedreturn dot(self.weights, f(self.points))

A subclass: the Trapezoidal ruleclass Trapezoidal(Integrator):def construct method(self):h (self.b - self.a)/float(self.n - 1)x linspace(self.a, self.b, self.n)w zeros(len(x))w[1:-1] hw[0] h/2; w[-1] h/2return x, wNote: Both superclass and subclass implement construct rule! The superclassprovides a default dummy version, while class Trapezoidal overloads (redefines)this method. This is often done in OO programming. If we forget to implementthe method in a subclass, the superclass version is inherited, and this gives anerror message about the missing implementation.

Another subclass: Simpson’s ruleSimpson’s rule is more tricky to implement because ofdifferent formulas for odd and even pointsDon’t bother with the details of wi and xi in Simpson’s rulenow – focus on the class design!class Simpson(Integrator):def construct method(self):if self.n % 2 ! 1:print ’n %d must be odd, 1 is added’ % self.nself.n 1 code for computing x and w return x, w

About the program flowLet us integrateR20x 2 dx using 101 points:def f(x):return x*xm Simpson(0, 2, 101)print m.integrate(f)m Simpson(.):this invokes the superclass constructor,which calls construct ruleThere are two construct rule methods, the one in the subclassis used (the subclass overloads (overrides) the superclassversion)m.integrate(f)invokes the inherited integrate method

Applications of the family of integration classesWe can empirically test out the accuracy of differentintegration methods Midpoint, Trapezoidal, Simpson,GaussLegendre2, .Sample integral: Z1 11t m dt 11 m0This integral is ”difficult” numerically for m 1Key problem: the error in numerical integration formulas is ofthe form Cn r , mathematical theory can predict r (the”order”), but we can estimate r empirically tooSee the book for computational detailsHere we focus on the conclusions

Convergence rates for m 1 (easy case)Simpson and Gauss-Legendre reduce the error faster than Midpointand Trapezoidal (plot has ln(error) versus ln n)m error)-10-15-20-25-3022.533.544.5ln(n)55.566.5

Convergence rates for m 1 (problematic case)Simpson and Gauss-Legendre, which are theoretically ”smarter”than Midpoint and Trapezoidal do not show superior behavior!m .5

Ordinary differential equationsMathematical problem:u ′ (t) f (u, t)Initial condition:u(0) u0Possible applications:Exponential growth of money or populations: f (u, t) αu,α constLogistic growth of a population under limited resources: u f (u, t) αu 1 Rwhere R is the maximum possible value of uRadioactive decay of a substance: f (u, t) αu, α const

Nu

Object-oriented programming (OO) means different things to different people: programming with classes (better: object-based programming) programming with class hierarchies (class fam