yego.me
💡 Stop wasting time. Read Youtube instead of watch. Download Chrome Extension

Simulations and repetition | Intro to CS - Python | Khan Academy


2m read
·Nov 10, 2024

I'm running a coin flip experiment and I want to find out how likely each outcome is: heads or tails. So I flip a coin once, twice, 100 times. Once I've repeated that experiment enough times, I see that about 50% of my flips are heads and 50% are tails.

Now, that's not a particularly interesting result. You probably could have told me that's what would happen at the beginning. But what if the experiment I want to repeat is much bigger? Instead of physically performing the experiment, we can simulate it with code.

For example, maybe I want to simulate a car crash to predict the risk of injury to the passengers, or I want to simulate a forest fire to predict how far it'll spread, or I want to simulate crop growth so I can predict yields and decide what to plant. These are all things that would be far too costly, too devastating, or take far too long to repeat in the real world.

But if we build a computer simulation, we can repeat the experiment as many times as we want for free, modifying different data inputs along the way. To simulate crop growth, I might combine climate and soil data with different irrigation and fertilizer choices, and then repeat how that affects my crop growth over a series of time steps.

Weather simulations work the same way. They collect wind, air pressure, and other readings from hundreds of different balloons, buoys, and satellites, and apply mathematical models over a series of time steps.

Okay, but why is the weather forecast wrong so much of the time then? It's almost impossible to 100% model the real world in a program. There's just so much data and randomness to take into account. And as humans, we don't always have access to all the data or 100% understand all the relationships involved.

Sometimes there are simply too many relationships that the computer physically can't process that much information in a reasonable amount of time. These are some of the limitations of our current weather models. We don't have data on the conditions at every single point on Earth, and even if we did, the computer wouldn't be able to handle all that data.

We can, in theory, more accurately predict tomorrow's weather, but by the time we get the result, it'll be the day after tomorrow. So for practicality, almost all simulations make some assumptions or simplifications about the world around us and settle for good enough results according to their needs.

Whether there's constraints on the data available, the amount of time they have to build the simulation, or the sheer computing power required, with just conditionals and variables, we can start to write our own basic simulations in Python. We're only missing two things: we need to be able to repeat our experiment and we need to be able to model some of the randomness that occurs in the real world.

More Articles

View All
Relating unit rate to slope in graphs of proportional relationships | Grade 8 (TX) | Khan Academy
A farmer sold 26 kg of tomatoes for $78. Which graph has a slope that represents the cost of tomatoes in dollars per kilogram? Pause this video, work through this on your own before we do this together. So, if we’re thinking about slope, slope is all ab…
Secrets You Can Learn From Your Customers
And some point during this coffee session, the guy was like, “Hey, oh, you want my nose? You want to see my, would you like a gold mine? Yeah, for all of my thoughts, all of my everything.” [Music] Hello, this is Michael Seibel with Dotson Caldwell, and…
Senate confirmation as a check on the judicial branch | US government and civics | Khan Academy
When we think about how the executive or the legislative branch have some form of check or power over the judicial branch, a key element of that is the executive’s ability to appoint judges to federal courts, including the U.S. Supreme Court. But it’s not…
15 Steps To Force Your Way Out Of Poverty
Poor people work just as hard, if not harder, than those born into wealth. However, that hard work rarely translates into wealth because poverty, as a system, is designed for survival, not growth. You have just enough to get by until tomorrow but never en…
Factorization with substitution | Polynomial factorization | Algebra 2 | Khan Academy
We’re told that we want to factor the following expression that they have right here, and they say that we can factor the expression as ( u + v ) squared, where ( u ) and ( v ) are either constant integers or single variable expressions. What are ( u ) an…
Expected value of a binomial variable | Random variables | AP Statistics | Khan Academy
So I’ve got a binomial variable ( x ) and I’m going to describe it in very general terms. It is the number of successes after ( n ) trials, where the probability of success for each trial is ( p ). This is a reasonable way to describe really any binomial …