De presentatie wordt gedownload. Even geduld aub

De presentatie wordt gedownload. Even geduld aub

Informatica Universiteit AntwerpenScripting 5.1 Informatica 1rste BAC Biologie Hoofdstuk 5 Scripting.

Verwante presentaties


Presentatie over: "Informatica Universiteit AntwerpenScripting 5.1 Informatica 1rste BAC Biologie Hoofdstuk 5 Scripting."— Transcript van de presentatie:

1 Informatica Universiteit AntwerpenScripting 5.1 Informatica 1rste BAC Biologie Hoofdstuk 5 Scripting

2 Informatica Universiteit AntwerpenScripting 5.2 Inhoud Inleiding Programeerconstructies –functies –variabelen (toekenning) –statements (print,...) –controlestructuren (if, while, until, for) Oefeningen

3 Informatica Universiteit AntwerpenScripting 5.3 Context "functionzero.zip" –gebruik die van vorige week –of haal via de web-site vervang in alle rekenbladen de functie "sinus" door "cosinus" –find-replace: "sin("  "cos(" –hoeveel cellen moet je aanpassen ? –hoeveel operaties heb je nodig ? nulpuntberekening –convergeert niet meer ! –pas a0 en b0 aan zie grafiek Kan dit eenvoudiger ?

4 Informatica Universiteit AntwerpenScripting 5.4 Editor voor Programmeercode Project: Alle plaatsen waar VisualBasic code kan zitten Programeercode wordt best bewaard in Modules Programeercode wordt geschreven in de editor Immediate: eventjes proberen Inspecteren / veranderen van Properties (o.a. naam)

5 Informatica Universiteit AntwerpenScripting 5.5 Voorbereiding Open VisualBasic Editor –In Excel –>>Tools>>Macro >>Visual Basic Editor Maak Module –(1) rechtsklik op "VBA Project (functionzero)" –>>Insert>>Module Hernoem Module1 –nieuw naam "GlobaleFuncties" –(2) venster "Properties" 1 2

6 Informatica Universiteit AntwerpenScripting 5.6 Eerste Functie "VBASampleCode.txt" –van de web-site In Editor –(1) Copy/Paste 1rste code Public Function datafunction(x) If x = 0 Then datafunction = 0 Else datafunction = Cos(1 / x) End If End Function –(2) Bewaren (3) Ga terug naar excel –in cel "=datafunction(0)" 1 2 3

7 Informatica Universiteit AntwerpenScripting 5.7 Even uitproberen Ga terug naar Visual Basic Editor in venster "Immediate" –"MsgBox datafunction(0.1)" + keyboard: Enter –"Debug.print datafunction (0.2)" + keyboard: Enter Merk op: Amerikaanse conventie voor komma getallen

8 Informatica Universiteit AntwerpenScripting 5.8 Functie (vorm) Public Function name ( argumenten ) statements End Function gereserveerde woorden –public, function, end public –wat hier gedefinieerd wordt is "overal" te gebruiken in tegenstelling tot "private" alleen binnen module Function –we maken een functie –zelf in te vullen naam –tussen haakjes, gescheiden door kommas –programmacode elk statement begint op een nieuwe lijn End Function –'t is gedaan...

9 Informatica Universiteit AntwerpenScripting 5.9 Statements name =... Exit Function name =... –toekenning ("assignment") –name wordt gelijk aan... Exit Function –berekening van functie is gedaan, ga naar "End Function" MsgBox... –waarschuwing via dialoog MsgBox "Hello World" MsgBox "datafunction(" & x & ")" Debug.print... –schrijf uit in "Immediate Window" Debug.Print "datafunction(" & x & ")"

10 Informatica Universiteit AntwerpenScripting 5.10 Varia commentaar rem... '... –alles na rem of ' wordt genegeerd –uitleg naar lezer van programmacode –tijdelijk een bepaald statement niet uitvoeren lange regels... _ –lange regels splitsen over meerdere regels ? –"spatie" gevolgd door "_" gevolgd door "Enter  "

11 Informatica Universiteit AntwerpenScripting 5.11 IF-statement ( vorm ) If expr Then then-block Else else-block End If If expr Then then-block End If expr then-block else-block truefalse expr then-block true false

12 Informatica Universiteit AntwerpenScripting 5.12 IF-statement variant ( vorm ) If expr1 Then block-1 ElseIf expr2 Then block-2... ElseIf exprn-1 Then block-n-1 Else block-n End If expr1 block-1 true false expr2 block-2 true false exprn-1 block-n-1 true false... block-n...

13 Informatica Universiteit AntwerpenScripting 5.13 Voorbeeld (1) Vervang code voor datafunction(x) door –Copy/Paste vanuit "VBASampleCode.txt" (web-site) Public Function datafunction(x) ' a function used as input for a spreadsheet ' producing graphs and calculating zero points MsgBox "oproep van datafunction(" _ & x & ")" If x = 0 Then datafunction = 0 Else datafunction = Cos(1 / x) End If End Function –Roep datafunction(x) op vanuit cel in rekenblad –Vervang "MsgBox" door "Debug.Print" –Roep datafunction(x) op vanuit "Immediate" window

14 Informatica Universiteit AntwerpenScripting 5.14 Oproepen vanuit Spreadsheet Vervang in "functionzero" –alle IF(…=0;0;(COS(1/…))) –door datafunction(...) –bekijk grafiek en nulpunten Vervang code door voorbeeld3 –Copy/Paste vanuit "VBASampleCode.txt" Application.volatile –elke verandering aan rekenblad: alle oproepen datafunction herberekenen –zie "Immediate window" Public Function datafunction(x) ' a function used as input... ' producing graphs and... Application.Volatile Debug.print _ "oproep van datafunction(" _ & x & ")" If x = 0 Then datafunction = 0 Else datafunction = sin(1 / x) End If End Function Vervang sin(1/x) door tan(1/x) cos(1/x) Bewaar. Grafiek en nulpunten ?

15 Informatica Universiteit AntwerpenScripting 5.15 Herhaling: Nulpunten (Bissectie methode) kies a 0 en b 0 zodat f(a 0 ) 0 stap 0: stel m 0 := (a 0 + b 0 ) / 2 –f(m 0 ) = 0 ?GEVONDEN –f(m 0 ) < 0 ? a 1 := m 0 en b 1 := b 0 –f(m 0 ) > 0 ? a 1 := a 0 en b 1 := m 0... stap n: stel m n := (a n + b n ) / 2 –f(m n ) = 0 ?GEVONDEN –f(m n ) < 0 ? a n+1 := m n en b n+1 := b n –f(m 0 ) > 0 ? a n+1 := a n en b n+1 := m n Benodigheden variabelen lus

16 Informatica Universiteit AntwerpenScripting 5.16 Variabelen, Toekenning ("assignment") variabele = waarde vb1=0 vb2=0 vb3=0'vb1, vb2, vb3 bevatten 0 vb1 = 1'vb1 bevat 1 vb2 = 2'vb2 bevat 2 vb3 = vb1 + vb2'vb3 bevat 3 vb1 = vb2' vb1 bevat 2 vb3 = vb3 + 1' vb3 bevat 4 000 vb1vb2vb3 100 vb1vb2vb3 120 vb1vb2vb3 12 3 vb1vb2 vb3 + 2 2 3 4 vb3 +1+1

17 Informatica Universiteit AntwerpenScripting 5.17 WHILE/UNTIL statement (vorm 1) Do While expr block Loop expr block true false block wordt misschien 0 x uitgevoerd ! Do Until expr block Loop expr block true false

18 Informatica Universiteit AntwerpenScripting 5.18 WHILE/UNTIL statement (vorm 1) Do block Loop While expr expr block true false Do block Loop Until expr expr block false true block wordt minstens 1 x uitgevoerd !

19 Informatica Universiteit AntwerpenScripting 5.19 FOR-statement (vorm) For naam = first To last Step step block Next naam truefalse naam <= last block naam = first naam = naam + step first, last, step: rekenkundige expressies met als resultaat een geheel getal !!!

20 Informatica Universiteit AntwerpenScripting 5.20 Bissectiemethode ( 1rste poging ) Public Function computeZero(a0, b0) 'calculates a zero point of a function using the bissection method a = a0 b = b0 m = (a + b) / 2 Do While Abs(datafunction(m)) > 0.000000001 ' Debug.Print " a = " & a & " - b = " & " - m = " & m If datafunction(m) > 0 Then b = m Else a = m End If m = (a + b) / 2 Loop computeZero = m End Function Probeer eens computezero(0.38, 0.18) computezero(0.18, 0.38)... keyboard: ctrl-break om te onderbreken

21 Informatica Universiteit AntwerpenScripting 5.21 Bissectiemethode ( 2de poging ) Public Function computeZero(a0, b0) 'calculates a zero point of a function using the bissection method If datafunction(a0) > 0 Then computeZero = "First parameter " & a0 & _ " should have negative function value (has " _ & datafunction(a0) & ")" Exit Function End If If datafunction(b0) < 0 Then computeZero = "Second parameter " & b0 & _ " should have positive function value (has " _ & datafunction(b0) & ")" Exit Function End If a = a0 b = b0 m = (a + b) / 2...

22 Informatica Universiteit AntwerpenScripting 5.22 Matrix met nulpuntberekeningen Maak een matrix –rij: 0 tot 0,39 step 0,1 –kolom: 0 tot 0,39 step 0,1 –waarde: computezero(...)

23 Informatica Universiteit AntwerpenScripting 5.23 Oefeningen schrijf functie "fsin(x)" –grafiek en nulpunten via functionzero –x = 0 dan fsin(x) = 1 –x  0 dan fsin(x) = Sin(x) / x schrijf functie "fac(n)" –faculteit(n) of n! –n is positief natuurlijk getal –n = 0 dan fac(n) = 1 –n > 0 dan fac(n) = n * fac (n-1) –schrijf met FOR en WHILE schrijf functie "MExp(x)" –exponentieel via benadering door Maclaurin –exp(x) = 1 + (x) + (x 2 / 2!) +... + (x n / n!) –stop voor n = 10 –stop als (x n / n!) < 10 -10 schrijf functie "sumrange(a, b)" –a en b natuurlijke getallen –a < b: a + (a+1) +... + b –a > b: b + (b+1) +... + a

24 Informatica Universiteit AntwerpenScripting 5.24 Conclusie Inleiding Programeerconstructies –functies –variabelen (toekenning) –statements (print,...) –controlestructuren (if, while, until, for) Oefeningen


Download ppt "Informatica Universiteit AntwerpenScripting 5.1 Informatica 1rste BAC Biologie Hoofdstuk 5 Scripting."

Verwante presentaties


Ads door Google