Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Decision Making in Mathematical Computing: Boolean Statements and Pseudocode - Prof. John , Study notes of Mathematics

A lecture note from mat 305: mathematical computing at the university of southern mississippi, fall 2009. It covers decision making in sage, including boolean statements, piecewise functions, and characterizing concavity. The lecture includes pseudocode examples and sage code implementation.

Typology: Study notes

2009/2010

Uploaded on 02/24/2010

koofers-user-my8
koofers-user-my8 🇺🇸

10 documents

1 / 54

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MAT 305:
Mathematical
Computing
John Perry
Decision-
making
Boolean
statements
Having said all
that.. .
Summary
MAT 305: Mathematical Computing
Lecture 7: Decision-making in Sage
John Perry
University of Southern Mississippi
Fall 2009
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36

Partial preview of the text

Download Decision Making in Mathematical Computing: Boolean Statements and Pseudocode - Prof. John and more Study notes Mathematics in PDF only on Docsity!

Mathematical Computing John Perry

Decision- making Boolean statements Having said all that... Summary

MAT 305: Mathematical Computing

Lecture 7: Decision-making in Sage

John Perry

University of Southern Mississippi

Fall 2009

Mathematical Computing John Perry

Decision- making Boolean statements Having said all that... Summary

Outline

1 Decision-making

2 Boolean statements

3 Having said all that...

4 Summary

You should be in worksheet mode to repeat the examples.

Mathematical Computing John Perry

Decision- making Boolean statements Having said all that... Summary

Decision making?

A function may have to act in different ways, depending on the arguments.

Mathematical Computing John Perry

Decision- making Boolean statements Having said all that... Summary

Decision making?

A function may have to act in different ways, depending on the arguments.

Example

Piecewise functions:

f (x) =

f 1 (x) , x ∈

a 0 , a 1

f 2 (x)^ , x ∈

a 1 , a 2

Mathematical Computing John Perry

Decision- making Boolean statements Having said all that... Summary

if statements

if (condition): if-statement if-statement

... non-if statement

where

  • condition: expression that evaluates to True or False
  • (^) condition True? statement1, statement2, etc. performed
    • (^) control passes finally to non-if statement
  • condition False? statement1, statement2,... skipped
    • control passes immediately to non-if statement

Mathematical Computing John Perry

Decision- making Boolean statements Having said all that... Summary

Example

sage: f = cos(x) sage: ddf = diff(f,2) sage: if (ddf(3pi/4) > 0): print 'concave up at', 3pi/ concave up at 3/4*pi

Mathematical Computing John Perry

Decision- making Boolean statements Having said all that... Summary

if-elif-else statements

if (condition1): if-statement

... elif (condition2): elif1-statement ... elif (condition3): elif2-statement ... ... else: else-statement ... non-if statement

where

  • (^) statement block selected by condition

Mathematical Computing John Perry

Decision- making Boolean statements Having said all that... Summary

Pseudocode for if-elif-else

if condition if-statement

... else if condition elseif1-statement ... else if condition elseif2-statement ... ... else condition else-statement ...

Notice:

  • (^) indentation
  • no parentheses, colons
  • else if, not elif

Mathematical Computing John Perry

Decision- making Boolean statements Having said all that... Summary

Example: concavity

Write a Sage function that tests whether a function f is concave up or down at a given point. Have it return the string ’concave up’, ’concave down’, or ’neither’.

Different choices =⇒ need to make a decision! =⇒ if

Mathematical Computing John Perry

Decision- making Boolean statements Having said all that... Summary

Example: concavity

Write a Sage function that tests whether a function f is concave up or down at a given point. Have it return the string ’concave up’, ’concave down’, or ’neither’.

Different choices =⇒ need to make a decision! =⇒ if

Start with pseudocode.

  • What inputs are needed?
  • (^) What output is expected?
  • What has to be done?
    • step by step
    • Divide et impera! Divide and conquer!

Mathematical Computing John Perry

Decision- making Boolean statements Having said all that... Summary

Pseudocode for Example

algorithm check_concavity inputs a ∈ R f (x), a twice-differentiable function at x = a outputs

Mathematical Computing John Perry

Decision- making Boolean statements Having said all that... Summary

Pseudocode for Example

algorithm check_concavity inputs a ∈ R f (x), a twice-differentiable function at x = a outputs ’concave up’ if f is concave up at x = a ’concave down’ if f is concave down at x = a ’neither’ otherwise do

Mathematical Computing John Perry

Decision- making Boolean statements Having said all that... Summary

Try it!

sage: def check_concavity(a, f, x): ddf = diff(f, x, 2) if (ddf(x=a) > 0): return 'concave up' elif (ddf(x=a) < 0): return 'concave down' else: return 'neither'

Mathematical Computing John Perry

Decision- making Boolean statements Having said all that... Summary

Try it!

sage: def check_concavity(a, f, x): ddf = diff(f, x, 2) if (ddf(x=a) > 0): return 'concave up' elif (ddf(x=a) < 0): return 'concave down' else: return 'neither' sage: check_concavity(3*pi/4, cos(x), x) 'concave up' sage: check_concavity(pi/4, cos(x), x) 'concave down'