First Order of Business: Get your notebook

  • Open a terminal in vscode, run command: cd _notebooks, type 'wget' and paste this link into said terminal and run it

  • Take notes wherever you please, but you will be graded on participating

So, what is a simulation anyway?

  • A simulation is a tested scenario used for viewing results/outputs to prepare for them in real world situations

  • These can be used for games like dice rolling, spinners, etc

  • These can be used for practical things such as building structures, testing car crashes, and other things before engaging in them in the real world

  • These simulations can have the option of obeying real world physics (Gravity, collision) or they can go against these norms since this is a fictitious scenario, and couldn't happen in real life

Big Question

  • Which of the following simulations could be the LEAST useful?

  • A retailer trying to identify which products sold the most

  • A restaurant determining the efficiency of robots
  • An insurance company studying the rain impact of cars
  • A sports bike company studying design changes to their new bike design
  • If you guessed a bike company, you're wrong, because the retail simulation was the right answer. Simulating robots in food service, sudying rain impact on vehicles, and new bike design can contribute a lot more to society in comparison to seeing what products sell more than others.

Next Big Question

If you were making a simulation for making a new train station, which of the following would be true about this simulation?

  • It could reveal potential problems/safety issues before construction starts
  • It cannot be used to test the train station in different weather
  • Simulation will add high costs to projects
  • Simulation is not needed because this train station already exists
  • Potential Saftey was the right answer, because you need somewhere to test the safety and ethicness of what you're about to do before you start building it. Otherwise, let's just say you'll have a special plaque for FBI's Most Wanted

Simulation 1:

Both programs below do the same thing. Given a height and a weight, they calculate how long it will take for a object to fall to the ground in a vacuum subjected to normal Earth levels of gravity.

However, the second one is a simulation. It calculates the distance the object has fallen every 0.1 seconds. This is useful for if you wanted a visual representation of a falling object, which pure math can't do as smoothly.

height = float(input("height in meters?"))

weight = input("weight in pounds?")

stuff = (2 * (height / 9.8))**(1/2)

print("It will take", stuff,"seconds for an object that weighs",weight,"pounds","to fall ",height,"meters in a vacuum")
It will take 2.0203050891044216 seconds for an object that weighs 20 pounds to fall  20.0 meters in a vacuum
t = 0
g = 0
d = 0
false = True
while false:
    t = t + 0.1
    d = 9.8 / 2 * (t**2)
    if d >= height:
        false = False
    #print(d) # if you want to print the distance every time it calculates it. Too long to output to a terminal, but this could be useful to display graphically. 
    #print(t)

print(t)
print(d)
2.1000000000000005
21.609000000000012

Simulation 2:

  • This simulation is made in order to simulate movement on a 2d plane vs a 3d plane.

  • How it works: we have multiple variables, if statements and equations under a while command in order to randomy generate steps on a 2d plane. Once it reaches the set destination, it will say that the man made it home after x amount of steps.

  • For the 3D plane, it takes a lot longer due to how big and open the 3d environment is, so there are more if statements in the 3d plane

(explain further)

import random
x = 0
y = 0
nights = 0
turn = 0
stopped = 0
turns = []

while (nights < 100):
    step = random.randrange(4)
    if step == 0:
        x = x+1
    if step == 1:
        x = x-1
    if step == 2:
        y = y+1
    if step == 3:
        y = y-1

    turn = turn + 1

    if x == 0 and y == 0:
        nights = nights + 1
        print("The Man Has Made It Home After ", turn, "Turns")
        turns.append(turn)
        turn = 0
    if turn/1000 % 1000 == 0 and x + y != 0:
        print("(", x,y, ")")
    if (turn > 10000000):
        stopped = stopped + 1
        turn = 0
        x = 0
        y = 0
        nights = nights + 1
        print("Caped")

average = sum(turns) / len(turns)
print("Avaerage", average, "Ones that when't too long ", stopped)
( 1482 106 )
( 796 578 )
( 81 851 )
( 23 -463 )
( -368 -1224 )
( 307 -583 )
( 454 1270 )
( 954 1284 )
( 1076 408 )
( 129 751 )
Caped
The Man Has Made It Home After  12 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  6778 Turns
( -1117 223 )
( -1510 1234 )
( -816 260 )
( -1041 -545 )
( -1476 -1410 )
( -996 -2604 )
( -3026 -2988 )
( -2348 -1598 )
( -2716 -1606 )
( -2976 -2544 )
Caped
The Man Has Made It Home After  2858 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  6 Turns
The Man Has Made It Home After  124 Turns
The Man Has Made It Home After  4 Turns
The Man Has Made It Home After  12 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  7340 Turns
The Man Has Made It Home After  22 Turns
The Man Has Made It Home After  10 Turns
The Man Has Made It Home After  6 Turns
The Man Has Made It Home After  242 Turns
The Man Has Made It Home After  30 Turns
The Man Has Made It Home After  16 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  558 Turns
The Man Has Made It Home After  4 Turns
The Man Has Made It Home After  8 Turns
( -120 -796 )
( -266 342 )
( -1420 -806 )
( -174 -1348 )
( -604 -1846 )
( -83 -1859 )
( -790 -2130 )
( -287 -1453 )
( 339 -687 )
( 937 611 )
Caped
The Man Has Made It Home After  6 Turns
The Man Has Made It Home After  6 Turns
( -700 354 )
( -157 -1383 )
( -589 -751 )
( -1213 -1179 )
( -2076 -1466 )
( -2417 -2027 )
( -1555 -2105 )
( -1444 -1992 )
( -1050 -2274 )
( -54 -2266 )
Caped
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  180 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  2 Turns
The Man Has Made It Home After  2 Turns
( 718 -214 )
( 1394 -1282 )
( 1146 -844 )
( 970 -388 )
( 1721 -753 )
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
/Users/aniket/vscode/automatic-spork/_notebooks/2022-12-12-Simulations.ipynb Cell 10 in <cell line: 9>()
      <a href='vscode-notebook-cell:/Users/aniket/vscode/automatic-spork/_notebooks/2022-12-12-Simulations.ipynb#X12sZmlsZQ%3D%3D?line=5'>6</a> stopped = 0
      <a href='vscode-notebook-cell:/Users/aniket/vscode/automatic-spork/_notebooks/2022-12-12-Simulations.ipynb#X12sZmlsZQ%3D%3D?line=6'>7</a> turns = []
----> <a href='vscode-notebook-cell:/Users/aniket/vscode/automatic-spork/_notebooks/2022-12-12-Simulations.ipynb#X12sZmlsZQ%3D%3D?line=8'>9</a> while (nights < 100):
     <a href='vscode-notebook-cell:/Users/aniket/vscode/automatic-spork/_notebooks/2022-12-12-Simulations.ipynb#X12sZmlsZQ%3D%3D?line=9'>10</a>     step = random.randrange(4)
     <a href='vscode-notebook-cell:/Users/aniket/vscode/automatic-spork/_notebooks/2022-12-12-Simulations.ipynb#X12sZmlsZQ%3D%3D?line=10'>11</a>     if step == 0:

KeyboardInterrupt: 
import random
x = 0
y = 0
z = 0
nights = 0
turn = 0
stopped = 0
turns = []

while (nights < 100):
    #rando movement
    step = random.randrange(6)
    if step == 0:
        x = x+1
    if step == 1:
        x = x-1
    if step == 2:
        y = y+1
    if step == 3:
        y = y-1
    if step == 4:
        z = z+1
    if step == 5:
        z = z-1
    #Turn counter
    turn = turn + 1
    #Goal check
    if x == 0 and y == 0 and z == 0:
        nights = nights + 1
        print("The Bird Has Made It Home After ", turn, "Turns")
        turns.append(turn)
        turn = 0
    if turn/1000 % 1000 == 0 and x + y + z != 0:
        print("(", x,y, ") ","| ", z)
    #Too long Stoper
    if (turn > 10000000):
        stopped = stopped + 1
        turn = 0
        x = 0
        y = 0
        z = 0
        nights = nights + 1
        print("Caped")

average = sum(turns) / len(turns)
print("Avaerage", average,"Ones that when't too long ", stopped)
The Bird Has Made It Home After  2 Turns
( -199 705 )  |  -36
( -112 1054 )  |  54
( -268 890 )  |  764
( -1047 1054 )  |  491
( -2237 1249 )  |  -296
( -3369 1408 )  |  -463
( -3294 1921 )  |  -397
( -4211 449 )  |  308
( -4152 267 )  |  777
( -5213 -150 )  |  799
Caped
The Bird Has Made It Home After  32 Turns
The Bird Has Made It Home After  108 Turns
( 357 779 )  |  294
( -326 -477 )  |  91
( 9 -370 )  |  511
( 536 -1275 )  |  355
( 792 -1358 )  |  1108
( 161 -1123 )  |  992
( 537 -557 )  |  1564
( 566 -757 )  |  1349
( 1736 -513 )  |  947
( 2117 -509 )  |  694
Caped
( 95 256 )  |  133
( -481 462 )  |  -1
( -660 959 )  |  -105
( -1529 600 )  |  -639
( -1667 -599 )  |  138
( -664 -287 )  |  -17
( -362 -704 )  |  -790
( -373 -1709 )  |  -1294
( -865 -2275 )  |  -2024
( -285 -2603 )  |  -1806
Caped
( -633 -239 )  |  -236
( 421 431 )  |  -404
( -248 -137 )  |  -737
( -164 -619 )  |  -421
( 670 -361 )  |  95
( 750 -1205 )  |  -881
( 1733 -1332 )  |  -1187
( 2240 -2276 )  |  -1220
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
/Users/aniket/vscode/automatic-spork/_notebooks/2022-12-12-Simulations.ipynb Cell 11 in <cell line: 10>()
     <a href='vscode-notebook-cell:/Users/aniket/vscode/automatic-spork/_notebooks/2022-12-12-Simulations.ipynb#X13sZmlsZQ%3D%3D?line=25'>26</a> turn = turn + 1
     <a href='vscode-notebook-cell:/Users/aniket/vscode/automatic-spork/_notebooks/2022-12-12-Simulations.ipynb#X13sZmlsZQ%3D%3D?line=26'>27</a> #Goal check
---> <a href='vscode-notebook-cell:/Users/aniket/vscode/automatic-spork/_notebooks/2022-12-12-Simulations.ipynb#X13sZmlsZQ%3D%3D?line=27'>28</a> if x == 0 and y == 0 and z == 0:
     <a href='vscode-notebook-cell:/Users/aniket/vscode/automatic-spork/_notebooks/2022-12-12-Simulations.ipynb#X13sZmlsZQ%3D%3D?line=28'>29</a>     nights = nights + 1
     <a href='vscode-notebook-cell:/Users/aniket/vscode/automatic-spork/_notebooks/2022-12-12-Simulations.ipynb#X13sZmlsZQ%3D%3D?line=29'>30</a>     print("The Bird Has Made It Home After ", turn, "Turns")

KeyboardInterrupt: 

Simulations in the wild

Simulations are used extremely frequently in real life applications. One of the most common examples of simulations are video games. A games physics engine can accurately simulate objects colliding

Another example is Blender, the software used in 3d animations class, here at Del Norte. Blender is made up of many small simulations, but one big one it uses is simulating the way light bounces off of and interacts with objects.

HW !!!

Create a simulation. It can be anything, just has to simulate something.

Some ideas:

  • Two objects colliding
  • Gravity on other planets

AND

Find an example of a simulation in a software/game you use, screenshot, and explain how it is a simulation

I decided to make a simulation of acceleration on Earth. The initial velocity and position of the object are set to 0, and the time step, or dt, and number of iterations are set to 0.1 and 1000. The acceleration is set to 9.81 m/s^2, and the simulation is performed by updating the position and velocity of the object at each iteration according to the equations of motion that I've learned in physics 1 so far.

velocity = 0
position = 0

# Set time step and number of iterations
dt = 0.1
n_iterations = 1000

# Set gravitational acceleration
g = 9.81

# Perform simulation
for i in range(n_iterations):
  # Calculate new position
  position += velocity * dt
  # Calculate new velocity
  velocity += g * dt
  
print(velocity)
print(position)
980.999999999995
49000.94999999976

As we spoke in class, an example of a simulation is Blender. I am currently in 3D animation 1 with Mr Askegreen, and over the past few weeks I've made this donut. A key thing to notice is how blender is able to simulate how the light bounces off the donut's icing.