Problem Solving and Computers - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Problem Solving and Computers

Description:

Performs mathematical operations at a very quick pace. ... There exists many problems that are solved using iterative ... winnings--; // minus one for betting ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 13
Provided by: adria9
Category:

less

Transcript and Presenter's Notes

Title: Problem Solving and Computers


1
Problem Solving and Computers
2
Why use a computer?
  • Performs mathematical operations at a very quick
    pace.
  • Todays computers can easily perform hundreds of
    Mflops (106 floating-point operations per second)
  • Computers do not make mistakes (mistakes arise
    from bad programming)
  • There exists many problems that are solved using
    iterative techniques or via simulations.
  • Iterative technique the application of a series
    of steps multiple times
  • Simulations determining a systems behavior
    through a mathematical model

3
Iterative problem
  • Assume we are given a function f(x) and we would
    like to see where this function intersects the
    line y x. Stated another way, we are trying to
    determine where x f(x).
  • Fixed point theorem
  • If f(x) ? K lt 1 for all x ? a,b, then the
    fixed point iteration pn f(pn-1) will converge
    to the fixed point x f(x).
  • The fixed point theorem basically states that we
    can use the fixed point iteration to determine x
    f(x) under a certain condition. The condition
    is that the absolute value of the first
    derivative of f(x), in the range a,b, is less
    than some constant K, which is less than 1.

4
Iterative problem
  • Fixed point iteration
  • Start with a point p0 in the range a,b
  • Determine the point pn f(pn-1). Point pn will
    be closer to the value x f(x) than point pn-1.
  • Repeat step 2 until we have approximated x close
    enough.
  • Example Let f(x) e-x in the range 0, 1.
    Start with p0 0.5.
  • p1 e-p0 e-0.5 0.60653
  • p2 e-p1 e-0.60653 0.54524
  • p3 e-p2 e-0.54524 0.57970
  • p4 e-p3 e-0.57970 0.56007
  • This process will eventually converge to lim n??
    pn 0.567143

5
Iterative problem
  • Graphically we can see this process. It is
    obvious that this iterative process is indeed
    converging to the fixed point where the line y
    x crosses the function f(x).

6
Iterative problem
Performing this by hand is easy but very tedious
however, a computer can easily accomplish this
task.
  • include ltiostream.hgt
  • include ltmath.hgt
  • void main(void)
  • double f
  • double p 0.5
  • int n
  • for (n 0 n lt 100 n)
  • f exp(-p)
  • cout ltlt "p(" ltlt n ltlt ") " ltlt p
  • ltlt " and f(p(" ltlt n ltlt ")) " ltlt f ltlt
    endl
  • p exp(-p) // compute p(n) f(p(n-1))

7
Iterative problem
  • p(0) 0.5 and f(p(0)) 0.606531
  • p(1) 0.606531 and f(p(1)) 0.545239
  • p(2) 0.545239 and f(p(2)) 0.579703
  • p(3) 0.579703 and f(p(3)) 0.560065
  • p(4) 0.560065 and f(p(4)) 0.571172
  • p(5) 0.571172 and f(p(5)) 0.564863
  • p(6) 0.564863 and f(p(6)) 0.568438
  • p(7) 0.568438 and f(p(7)) 0.566409
  • p(8) 0.566409 and f(p(8)) 0.56756
  • p(9) 0.56756 and f(p(9)) 0.566907
  • p(10) 0.566907 and f(p(10)) 0.567277
  • p(11) 0.567277 and f(p(11)) 0.567067
  • p(12) 0.567067 and f(p(12)) 0.567186
  • p(13) 0.567186 and f(p(13)) 0.567119
  • p(14) 0.567119 and f(p(14)) 0.567157
  • p(15) 0.567157 and f(p(15)) 0.567135
  • p(16) 0.567135 and f(p(16)) 0.567148
  • p(17) 0.567148 and f(p(17)) 0.567141
  • p(18) 0.567141 and f(p(18)) 0.567145

8
Iterative problem
Instead of using a for loop we can use a do-while
loop that checks whether or not pn1 is much
different than pn. The iterations stop when the
difference is sufficiently small. The code below
only iterates 22 times.
  • include ltiostream.hgt
  • include ltmath.hgt
  • void main(void)
  • double f
  • double oldp
  • double p 0.5
  • int n 0
  • do
  • oldp p
  • f exp(-oldp)
  • cout ltlt "p(" ltlt n ltlt ") " ltlt oldp
  • ltlt " and f(p(" ltlt n ltlt ") " ltlt f ltlt
    endl
  • n
  • p exp(-oldp) // compute p(n) f(p(n-1))
  • while (fabs(p oldp) gt 0.5e-6)
  • // 0.5e-6 represents the tolerance on the answer

9
Simulation problem
  • Sic Bo is a game of chance that uses three dice.
    The simplest bet is to predict a face that will
    appear on at least one of the dice. If your
    prediction is correct you receive your bet amount
    for each die that has your prediction. If your
    prediction is wrong you lose your bet.
  • For example, if you bet 1 on three
  • No die shows a three, you lose your 1
  • One die shows a three, you get back your 1
  • Two dice show a three, you get your 1 back plus
    1
  • All three dice show a three, you get your 1 back
    plus 2
  • It can be shown that your winnings per game is
    (some thought is required here)

10
Simulation problem
While this solution can be derived, it can be
approximated by simulating a number of games. The
simulation of many games is very quick when using
a computer.
  • include ltiostream.hgt
  • include ltstdlib.hgt
  • include lttime.hgt
  • void main(void)
  • const int NUM_GAMES 20000
  • int dice1, dice2, dice3
  • int predict
  • int winnings 0
  • int i
  • // initialize the random generator using the
    current time
  • srand((int))

11
Simulation problem
  • for (i 0 i lt NUM_GAMES i)
  • // rand() returns a number between 0 and
    RAND_MAX
  • predict (int) (6.0rand()/(RAND_MAX 1.0))
    1
  • dice1 (int) (6.0rand()/(RAND_MAX 1.0))
    1
  • dice2 (int) (6.0rand()/(RAND_MAX 1.0))
    1
  • dice3 (int) (6.0rand()/(RAND_MAX 1.0))
    1
  • winnings-- // minus one for betting
  • if ( (predict dice1) ) // plus one for
    every correct prediction
  • winnings
  • if ( (predict dice2) )
  • winnings
  • if ( (predict dice3) )
  • winnings

12
Simulation problem
  • On average you will win -10019/20000 -0.50095
    per game
  • On average you will win -9962/20000 -0.4981
    per game
  • On average you will win -9805/20000 -0.49025
    per game
  • On average you will win -10128/20000 -0.5064
    per game
  • On average you will win -9992/20000 -0.4996
    per game
Write a Comment
User Comments (0)
About PowerShow.com