analisis en matlab

Upload: cesar

Post on 28-Feb-2018

232 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/25/2019 Analisis en matlab

    1/30

    In the first chapter, we already had a look at the Monte Carlo method.

    1

  • 7/25/2019 Analisis en matlab

    2/30

    Now its a good time to dig into the details.

    2

  • 7/25/2019 Analisis en matlab

    3/30

    First the deterministic problem to find a numerical value for pi is solved.

    3

  • 7/25/2019 Analisis en matlab

    4/30

    We generate random values for points within a square of 2 by 2 with centerpoint

    (0,0).The x-coordinates and the y-coordinates are independent results of a uniform

    distributed random experiment.

    They range from -1 to 1.

    In the square lets look at a circle with center point (0,0) and radius 1.

    4

  • 7/25/2019 Analisis en matlab

    5/30

    The area of the circle and the area of the square have approximately the same ratio

    as the number of points in the circle to the number of points in the square.

    5

  • 7/25/2019 Analisis en matlab

    6/30

    Thus we obtain a simple equation for pi.

    6

  • 7/25/2019 Analisis en matlab

    7/30

    This is an easy code in Matlab calculating pi

    7

  • 7/25/2019 Analisis en matlab

    8/30

    and visualizing the method. It is straightforward, but somewhat specialized in Matlab.

    Especially Lines 2-5 make use of vectoring. Some people with a classical C-programming background are not so familiar with vectoring. Therefore I will also

    present a more classical variant.

    8

  • 7/25/2019 Analisis en matlab

    9/30

    It is not possible to declare an empty array in Matlab.

    9

  • 7/25/2019 Analisis en matlab

    10/30

    But the typical combination, declaration and definition of an array can be done by the

    zeros command.

    10

  • 7/25/2019 Analisis en matlab

    11/30

    For really big amounts of points, the plot is not very useful. Therefore only the pi-

    calculating part is done.

    11

  • 7/25/2019 Analisis en matlab

    12/30

    Comparing C-style to vectoring style, the vectoring style is a little bit faster.

    12

  • 7/25/2019 Analisis en matlab

    13/30

    But when stepping to even more points, the vectoring approach can cause out of

    memory errors, as very big arrays have to be generated. N=1E9 worked for the C-stylecode only on my machine.

    13

  • 7/25/2019 Analisis en matlab

    14/30

    14

  • 7/25/2019 Analisis en matlab

    15/30

    15

  • 7/25/2019 Analisis en matlab

    16/30

    16

  • 7/25/2019 Analisis en matlab

    17/30

    17

  • 7/25/2019 Analisis en matlab

    18/30

    18

  • 7/25/2019 Analisis en matlab

    19/30

    19

  • 7/25/2019 Analisis en matlab

    20/30

    Now to Gary Foshees famous boy born on a Tuesday problemI have two children. One is a boy born on a Tuesday. What is the probability I have

    two boys?

    Do you think the Tuesday is important information?

    Well see by employing the Monte Carlo method.

    And we try to do it like this:

    20

  • 7/25/2019 Analisis en matlab

    21/30

    Now to Gary Foshees famous boy born on a Tuesday problemI have two children. One is a boy born on a Tuesday. What is the probability I have

    two boys?

    Do you think the Tuesday is important information?

    Well see by employing the Monte Carlo method.

    And we try to do it like this:

    21

  • 7/25/2019 Analisis en matlab

    22/30

    Now to Gary Foshees famous boy born on a Tuesday problemI have two children. One is a boy born on a Tuesday. What is the probability I have

    two boys?

    Do you think the Tuesday is important information?

    Well see by employing the Monte Carlo method.

    And we try to do it like this:

    22

  • 7/25/2019 Analisis en matlab

    23/30

    Here is the code for the boy born on a Tuesday problem

    23

  • 7/25/2019 Analisis en matlab

    24/30

    The first and the last line of this code are used to measure the cpu-time for the

    program.

    24

  • 7/25/2019 Analisis en matlab

    25/30

    In line two the number of families is hard coded , this means the user of the

    program is not asked a number, but the number is written into the code.

    25

  • 7/25/2019 Analisis en matlab

    26/30

    Randi in lines 3-6 is the random number generator for discrete uniform distributions.

    26

  • 7/25/2019 Analisis en matlab

    27/30

    Note the bitwise logical operators | and & in line 7 and 8. You might have tried .|| or

    .&& which might give the same result if they were defined within Matlab.The bitwise &, | and == operators have arrays as operands and arrays as results. If the

    conditions are met, the corresponding array element is 1, otherwise it is 0.

    Nnz (read number of non zeros) then just counts the ones.

    27

  • 7/25/2019 Analisis en matlab

    28/30

    Note that the solution 0.48 clearly shows that the Tuesday information is important

    (the strange point is, Wednesday would work as well, the day just has to be fixed).Without the Tuesday, the probability of two boys would be only one third.

    28

  • 7/25/2019 Analisis en matlab

    29/30

    We have reached the end of our little problem solvers toolbox journey.

    I hope you enjoyed it and will find it useful.

    29

  • 7/25/2019 Analisis en matlab

    30/30

    In fact we did cover a wide range of problems. Maybe after dealing with the tools and

    some Matlab details this is a good time to step back to the modelling. Any modelreduces the worlds complexity, keeping phenomena that are important for the

    problem at hand and forgetting about the rest. Like good managers do first things first

    and second not all.

    Think about which phenomena we kept, what were the phenomena we neglected or

    didnt care about. (Like we did not care about the modulus of the gear wheels, or the

    width of the gear wheels, or the material the gear box was made of. But we used the

    non slipping assumption.) So this is goodbye from me, but maybe we will meet on the

    discussion board or in our next Mooc, modelling and simulation using Simulink.