Please I neeed a code or script for this 1D leaf in the wind
Please I neeed a code or script for this.
(1D leaf in the wind, many time steps, using functions!). In this problem you’re going to create a function file to encapsulate the Euler update step, then another to encapsulate the simulation. Once you’ve done this it makes it easy to play around with different starting conditions, time steps h and mass m values. TODO: a) Create a function file to do one integration step. This function takes in x, v, m, and h. Put your force function calculation in the function file. Return the new x, the new v, and f (return the force, f, so that you can plot it). You must use a function file, and the force
calculation must be in that function file, not in the calling script.
functi on [ xNew, vNew, forceAt X ] = Eul erI nt egrat e( xOl d, vOl d, mass, h )
b) Copy your script from Problem 2 and replace the update steps with this function. Output for this should be exactly the same as it was in Problem 2. Include both your output from Problem 2 and the “new” output. Note: If you didn’t get Problem 2 working before, you won’t get penalized for it not working this time - just demonstrate that you know how to put the function in. Who knows, it might fix it… Make sure to put a note in the
doc if Problem 2 wasn’t working.
c) Make a new function that takes in initial conditions, total time, time step, and mass and
returns arrays for time, x, v, and F.
functi on [ times, xArray, vArrray, f Array ] = Euler Simul at e ( xI niti al, vI niti al, mass, h, total Time )
d) Check that your function works by trying it with four different values of h: 0.1, 0.05, 0.01 and 0.005. Plot each value of h on a different row, x vs t in the first column, then v vs t,
then f vs t.
e) Answer the following question(s) in your .doc:
a. Do you get the same plots with different step sizes? Why/why not? b. If you made the step size smaller, would the result change much? Why/why not?
Implementation notes: Part c) Start with your for loop from problem 2. If you haven’t stored the data in arrays, now is the time to do it… Should look something like
Calculate number of loops Allocate arrays For number of loops Store current x,v,t in array Calculate new x,v,f Update t Store f in array End
Solution
Remember when you first started programming in Processing? Perhaps you wanted to draw a lot of circles on the screen. So you said to yourself: “Oh, I know. I’ll draw all these circles at random locations, with random sizes, and random colors.” In a computer graphics system, it’s often easiest to seed a system with randomness. In this book, however, we’re looking to build systems modeled on what we see in nature. Defaulting to randomness is not a particularly thoughtful solution to every design problem—in particular, the kind of problems that involve creating an organic or natural-looking design. With a few tricks, we can change the way we use random() to produce “non-uniform” distributions of random numbers. This will come in handy throughout the book as we look at a number of different scenarios. When we examine genetic algorithms, for example, we’ll need a methodology for performing “selection”—which members of our population should be selected to pass their DNA down to the next generation. Remember the concept of survival of the fittest? Let’s say we have a population of monkeys evolving. Not every monkey will have a equal chance of reproducing. To simulate Darwinian evolution, we can’t simply pick two random monkeys to be parents. We need the more “fit” ones to be more likely to be chosen. We need to define the “probability of the fittest.” For example, perhaps a particularly fast and strong monkey has a 90% chance of procreating, while a weaker one has only a 10% chance. Let’s review the basic principles of probability, first looking at “Single Event Probability,” i.e. the likelihood of something to occur. Given a system with a certain number of possible outcomes, the probability of any given event occurring is the number of outcomes that qualify as that event divided by the total number of possible outcomes. The simplest example is a coin toss. There are a total of two possible outcomes (heads or tails). There is only one way to flip heads. Therefore, the probability of heads is one divided by two, i.e. 1/2 or 50%. Consider a deck of fifty-two cards. The probability of drawing an ace from that deck
Do you get the same plots with different step sizes? Why/why not? b. If you made the step size smaller, would the result change much? Why/why not?
Implementation notes: Part c) Start with your for loop from problem 2. If you haven’t stored the data in arrays, now is the time to do it… Should look something like
Calculate number of loops Allocate arrays For number of loops Store current x,v,t in array Calculate new x,v,f Update t Store f in array End
number of aces / number of cards = 4 / 52 = 0.077 = ~ 8%
Does this accurately depict the heights of real-world beings? Think of a crowded sidewalk in New York City. Pick a random person and it may appear that their height is random. Nevertheless, it’s not the kind of random that the random() produces. People’s heights are not uniformly distributed; there are a great deal more people of average height than there are very tall or very short ones. To simulate nature, we may want it to be more likely that our monkeys are of average height (250 pixels), yet allow them to still on occasion be very short or very tall. A distribution of values that cluster around an average (referred to as the “mean”) is known as a “normal” distribution. It is also called the Gaussian distribution (named for mathematician Carl Friedrich Gauss) or, if you are French, the Laplacian distribution (named for Pierre-Simon Laplace). Both mathematicians were working concurrently in the early nineteenth century on defining such a distribution. When you graph the distribution, you get something that looks like the following, informally known as the bell curve.

