Start learning today, and be successful in your academic & professional career. Start Today!

Loading video...

Enter your Sign on user name and password.

Start learning today, and be successful in your academic & professional career. Start Today!

Loading video...

## Discussion

## Study Guides

## Practice Questions

## Download Lecture Slides

## Table of Contents

## Related Books

- Simulation is an area of application where computers have come into their own. A simulation is a
*computer experiment*which mirrors some aspect of the real world that appears to be based on random processes, or is too complicated to understand properly. (Whether events can be really random is actually a philosophical or theological question.) Some examples are: radioactive decay, rolling dice, bacteria division and traffic flow. The essence of a simulation program is that the programmer is unable to predict beforehand exactly what the outcome of the program will be, which is true to the event being simulated. For example, when you spin a coin, you do not know for sure what the result will be.

Write a program to simulate the spinning coin problem, but in this exercise coin is not fair (i.e. the chance of having head is 60%). Do it for 100 times and display the ratio of number of heads over 100.

H = 0;

T = 0;

N = 100;

for i = 1:N

Â Â r = rand;

Â Â if r Â < Â 0.6

Â Â Â Â Â H = H + 1;

Â Â else

Â Â Â Â Â T = T + 1;

Â Â end

end

disp('Head Percentage = ')

disp(100*H/N)

T = 0;

N = 100;

for i = 1:N

Â Â r = rand;

Â Â if r Â < Â 0.6

Â Â Â Â Â H = H + 1;

Â Â else

Â Â Â Â Â T = T + 1;

Â Â end

end

disp('Head Percentage = ')

disp(100*H/N)

In a game of Bingo the numbers 1 to 99 are drawn at random from a bag. Â Write a script to simulate the draw of the numbers (each number can be drawn only once), printing them ten to a line.

bingo = 1 : 99;

for i = 1 : 99

temp = bingo(i);

swop = floor(rand * 99 + 1);

bingo(i) = bingo(swop);

bingo(swop) = temp;

end

for i = 1 : 10 : 81

disp(bingo(i : i + 9))

end

disp(bingo(91 : 99))

for i = 1 : 99

temp = bingo(i);

swop = floor(rand * 99 + 1);

bingo(i) = bingo(swop);

bingo(swop) = temp;

end

for i = 1 : 10 : 81

disp(bingo(i : i + 9))

end

disp(bingo(91 : 99))

Two players, A and B, play a game called Eights. They take it in turns to choose a number 1, 2 or 3, which may not be the same as the last number chosen (so if A starts with 2, B may only choose 1 or 3 at the next move). A starts, and may choose any of the three numbers for the first move. After each move, the number chosen is added to a common running total. If the total reaches 8 exactly, the player whose turn it was wins the game. If a player causes the total to go over 8, the other player wins. For example, suppose A starts with 1 (total 1), B chooses 2 (total 3), A chooses 1 (total 4) and B chooses 2 (total 6). A would like to play 2 now, to win, but he can’t because B cunningly played it on the last move, so A chooses 1 (total 7). This is even smarter, because B is forced to play 2 or 3, making the total go over 8 and thereby losing. Write a script to simulate each player’s chances of winning, if they always play at random.

counter = 0;

for i = 1:1000

Â Â ChofA = floor(rand*3+1);

Â Â ChofB = floor(rand*3+1);

Â Â while ChofB == ChofA

Â Â Â Â ChofB = floor(rand*3+1);

Â Â end

Â Â total = 0;

Â Â if (ChofB hicksim = ChofA) (total Â < = 8)

Â Â Â Â total = ChofA + ChofB + total;

Â Â Â Â temp = floor(rand*3+1);

Â Â Â Â while (temp == ChofA)|(temp == ChofB)

Â Â Â Â Â Â Â temp = floor(rand*3+1);

Â Â Â Â end

Â Â Â Â ChofA = temp;

Â Â Â Â total = total + ChofA;

Â Â end

Â Â ChofB = floor(rand*3+1);

Â Â ChofA = floor(rand*3+1);

Â Â while ChofA == ChofB

Â Â Â Â ChofA = floor(rand*3+1);

Â Â end

Â Â total = total + ChofB;

Â Â if total == 8

Â Â Â Â disp('B wins');

Â Â Â Â counter = counter +1;

Â Â elseif total Â > Â 8

Â Â Â Â disp('A wins');

Â Â elseif total Â < Â 8

Â Â Â Â disp('B wins');

Â Â Â Â counter = counter +1;

Â Â end

end

disp('chance of B winning: ') disp(100*counter/1000)

for i = 1:1000

Â Â ChofA = floor(rand*3+1);

Â Â ChofB = floor(rand*3+1);

Â Â while ChofB == ChofA

Â Â Â Â ChofB = floor(rand*3+1);

Â Â end

Â Â total = 0;

Â Â if (ChofB hicksim = ChofA) (total Â < = 8)

Â Â Â Â total = ChofA + ChofB + total;

Â Â Â Â temp = floor(rand*3+1);

Â Â Â Â while (temp == ChofA)|(temp == ChofB)

Â Â Â Â Â Â Â temp = floor(rand*3+1);

Â Â Â Â end

Â Â Â Â ChofA = temp;

Â Â Â Â total = total + ChofA;

Â Â end

Â Â ChofB = floor(rand*3+1);

Â Â ChofA = floor(rand*3+1);

Â Â while ChofA == ChofB

Â Â Â Â ChofA = floor(rand*3+1);

Â Â end

Â Â total = total + ChofB;

Â Â if total == 8

Â Â Â Â disp('B wins');

Â Â Â Â counter = counter +1;

Â Â elseif total Â > Â 8

Â Â Â Â disp('A wins');

Â Â elseif total Â < Â 8

Â Â Â Â disp('B wins');

Â Â Â Â counter = counter +1;

Â Â end

end

disp('chance of B winning: ') disp(100*counter/1000)

*These practice questions are only helpful when you work on them offline on a piece of paper and then use the solution steps function to check your answer.

Answer

Lecture Slides are screen-captured images of important points in the lecture. Students can download and print out these lecture slide images to do practice problems as well as take notes while watching the lecture.

- Intro 0:00
- Example: Flipping Coins 0:34
- MATLAB Equation
- Changing Value of N 3:14
- Example: Roll of Dice 3:49
- Example: Random Walk 4:44
- Illustration
- MATLAB Equation
- Probability

I. Introduction | ||
---|---|---|

Â | MATLAB Essentials, Part 1 | 8:36 |

Â | MATLAB Essentials, Part 2 | 7:39 |

II. MATLAB Fundamentals | ||

Â | Introduction to Programs | 6:42 |

Â | Arrays & Matrices | 7:06 |

Â | Operator, Expression & Statements | 13:26 |

Â | Repeating With For | 14:21 |

Â | Decisions, Part 1 | 14:35 |

Â | Decisions, Part 2 | 8:53 |

Â | Last Notes on MATLAB Introduction | 9:47 |

III. Logical Vectors | ||

Â | Logical Vectors, Part 1 | 11:23 |

Â | Logical Vectors, Part 2 | 11:40 |

IV. Program Design | ||

Â | Program Design & Algorithm Development | 19:36 |

V. Graphics | ||

Â | Graphics, Part 1 | 9:14 |

Â | Graphics, Part 2 | 17:20 |

Â | Graphics, Part 3 | 18:37 |

VI. Loops | ||

Â | Loops | 10:38 |

Â | While | 17:01 |

VII. Project | ||

Â | Projectile Problem | 16:33 |

VIII. Function M-Files | ||

Â | Function M-Files, Part 1 | 12:55 |

Â | Function M-Files, Part 2 | 14:14 |

Â | Function M-Files, Part 3 | 11:54 |

IX. Graphics Continued | ||

Â | More on Graphics | 11:13 |

Â | Graphical User Interface, Part 1 | 7:25 |

Â | Graphical User Interface, Part 2 | 9:33 |

X. Dynamical Systems | ||

Â | Dynamical Systems | 12:45 |

XI. Simulation | ||

Â | Examples of Simulation | 10:45 |

XII. Numerical Methods | ||

Â | Numerical Methods | 22:20 |

XIII. Simulink | ||

Â | Simulink | 9:37 |

0 answers

Post by Adegoke Adelabu on June 16, 2014

Hi: Please how can you simulate a satellite orbiting around the earth and showing its ground tracks in a resulting plot after the simulation is done?