Programa Pentru Simularea Din Decembrie 2015 La BAC La Proba de Biologie
Informatica 1rste BAC Biologie
description
Transcript of Informatica 1rste BAC Biologie
Universiteit Antwerpen
Scripting
5.1Informatica
Informatica1rste BAC Biologie
Hoofdstuk 5Scripting
Universiteit Antwerpen
Scripting
5.2Informatica
Inhoud• Inleiding• Programeerconstructies
– functies– variabelen (toekenning)– statements (print, ...)– controlestructuren (if, while, until, for)
• Oefeningen
Universiteit Antwerpen
Scripting
5.3Informatica
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 ?
Universiteit Antwerpen
Scripting
5.4Informatica
Editor voor Programmeercode
Project: Alle plaatsen waar VisualBasic code kan zittenProgrameercode wordt best bewaard in Modules
Programeercode wordt geschreven in de editor
Immediate: eventjes proberen
Inspecteren / veranderen van Properties (o.a. naam)
Universiteit Antwerpen
Scripting
5.5Informatica
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
Universiteit Antwerpen
Scripting
5.6Informatica
Eerste Functie• "VBASampleCode.txt"
– van de web-site• In Editor
– (1) Copy/Paste 1rste codePublic Function datafunction(x) If x = 0 Then datafunction = 0 Else datafunction = Cos(1 / x) End IfEnd Function
– (2) Bewaren• (3) Ga terug naar excel
– in cel "=datafunction(0)"
1
2
3
Universiteit Antwerpen
Scripting
5.7Informatica
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
Universiteit Antwerpen
Scripting
5.8Informatica
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
• <name>– zelf in te vullen naam
• <argumenten>– tussen haakjes, gescheiden door kommas
• <statements>– programmacode
• elk statement begint op een nieuwe lijn
• End Function– 't is gedaan ...
Universiteit Antwerpen
Scripting
5.9Informatica
Statementsname = ...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 & ")"
Universiteit Antwerpen
Scripting
5.10Informatica
Varia• commentaarrem ...' ...
– 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"
Universiteit Antwerpen
Scripting
5.11Informatica
IF-statement (vorm)If expr Then
then-blockElse
else-blockEnd If
If expr Thenthen-block
End If
expr
then-block else-block
true false
expr
then-block
true
false
Universiteit Antwerpen
Scripting
5.12Informatica
IF-statement variant (vorm)If expr1 Then
block-1ElseIf expr2 Then
block-2...ElseIf exprn-1 Then
block-n-1Else
block-nEnd If
expr1
block-1
true
false
expr2
block-2
true
false
exprn-1
block-n-1
true
false
...
block-n
...
Universiteit Antwerpen
Scripting
5.13Informatica
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 IfEnd Function
– Roep datafunction(x) op vanuit cel in rekenblad– Vervang "MsgBox" door "Debug.Print"– Roep datafunction(x) op vanuit "Immediate" window
Universiteit Antwerpen
Scripting
5.14Informatica
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 IfEnd Function
Vervang sin(1/x) door•tan(1/x)•cos(1/x)Bewaar. Grafiek ennulpunten ?
Universiteit Antwerpen
Scripting
5.15Informatica
Herhaling: Nulpunten (Bissectie methode)
• kies a0 en b0 zodat f(a0) < 0 en f(b0) > 0• stap 0: stel m0 := (a0 + b0) / 2
– f(m0) = 0 ? GEVONDEN
– f(m0) < 0 ? a1 := m0 en b1 := b0– f(m0) > 0 ? a1 := a0 en b1 := m0
• ...• stap n: stel mn := (an + bn) / 2
– f(mn) = 0 ? GEVONDEN
– f(mn) < 0 ? an+1 := mn en bn+1 := bn– f(m0) > 0 ? an+1 := an en bn+1 := mn
Benodigheden•variabelen•lus
Universiteit Antwerpen
Scripting
5.16Informatica
Variabelen, Toekenning ("assignment")
• variabele = waardevb1=0vb2=0vb3=0 'vb1, vb2, vb3 bevatten 0vb1 = 1 'vb1 bevat 1vb2 = 2 'vb2 bevat 2
vb3 = vb1 + vb2 'vb3 bevat 3
vb1 = vb2 ' vb1 bevat 2
vb3 = vb3 + 1 ' vb3 bevat 4
0 0 0vb1 vb2 vb3
1 0 0vb1 vb2 vb3
1 2 0vb1 vb2 vb3
1 23
vb1 vb2
vb3
+
22
3 4vb3
+ 1
Universiteit Antwerpen
Scripting
5.17Informatica
WHILE/UNTIL statement (vorm 1)
Do While exprblock
Loop
expr
block
truefalse
block wordt misschien
0 x uitgevoerd !
Do Until exprblock
Loop
expr
block
truefalse
Universiteit Antwerpen
Scripting
5.18Informatica
WHILE/UNTIL statement (vorm 1)
Doblock
Loop While exprexpr
block
true
false
Doblock
Loop Until expr
expr
block
false
true
block wordt minstens
1 x uitgevoerd !
Universiteit Antwerpen
Scripting
5.19Informatica
FOR-statement (vorm)For naam = first To last Step step
blockNext naam
true falsenaam <= last
block
naam = first
naam = naam + step
first, last, step:rekenkundige expressies
met als resultaat een geheel getal !!!
Universiteit Antwerpen
Scripting
5.20Informatica
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 = mEnd Function
Probeer eens• computezero(0.38, 0.18)•computezero(0.18, 0.38)...keyboard: ctrl-break om te onderbreken
Universiteit Antwerpen
Scripting
5.21Informatica
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 ...
Universiteit Antwerpen
Scripting
5.22Informatica
Matrix met nulpuntberekeningen• Maak een matrix
– rij: 0 tot 0,39 step 0,1
– kolom: 0 tot 0,39 step 0,1
– waarde: computezero(...)
Universiteit Antwerpen
Scripting
5.23Informatica
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) + (x2 /
2!) + ...+ (xn / n!)
– stop voor n = 10– stop als (xn / 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
Universiteit Antwerpen
Scripting
5.24Informatica
Conclusie• Inleiding• Programeerconstructies
– functies– variabelen (toekenning)– statements (print, ...)– controlestructuren (if, while, until, for)
• Oefeningen