Cobweb plot python

Cobweb plot python DEFAULT

Fixed point iteration and cobweb plot

Your questions

  1. Functions can be function arguments, too.
  2. You can add a parameter for that.
  3. See additional comments. I think it's fine that you use a class.
  4. That is usually not in the range of codereview. That should be asked in StackOverflow (but I think I know what you want - just look at the code).

Additional comments

  • Add a shebang. If you do, you can directly execute the script. See What does #!/usr/bin/python mean?
  • Read PEP8 or at least use pep8online.com to make your coding style standard conform and hence easier to read. It's mostly about setting whitespaces correct.
  • Use docstrings and eventually doctest. That makes your code again easier to read and you can easily verify if it works as you expect.
  • If you don't use a variable, you should remove it at all. If that is not possible (e.g. in the case of unzipping values) you should name the variable .

The code

answered Aug 21 '14 at 21:26

Martin ThomaMartin Thoma

1,1351010 silver badges2222 bronze badges

\$\endgroup\$Sours: https://codereview.stackexchange.com/questions/56281/fixed-point-iteration-and-cobweb-plot

5.3: 5.3 Cobweb Plots for One-Dimensional Iterative Maps

One possible way to solve the overcrowded phase space of a discrete-time system is to create two phase spaces, one for time \(t−1\) and another for \(t\), and then draw trajectories of the system’s state in a meta-phase space that is obtained by placing those two phase spaces orthogonally to each other. In this way, you would potentially disentangle the tangled trajectories to make them visually understandable.

However, this seemingly brilliant idea has one fundamental problem. It works only for one-dimensional systems, because two- or higher dimensional systems require fouror more dimensions to visualize the meta-phase space, which can’t be visualized in the three-dimensional physical world in which we are confined.

This meta-phase space idea is still effective and powerful for visualizing the dynamics of one-dimensional iterative maps. The resulting visualization is called a cobweb plot, which plays an important role as an intuitive analytical tool to understand the nonlinear dynamics of one-dimensional systems.

Here is how to manually draw a cobweb plot of a one-dimensional iterative map, \(x_{t} = f(x_{t−1})\), with the range of \(x_{t}\) being \([x_{min},x_{max}]\). Get a piece of paper and a pen, and do the following:

1. Draw a square on your paper. Label the bottom edge as the axis for \(x_{t−1}\), and the left edge as the axis for \(x_{t}\). Label the range of their values on the axes (Fig. 5.3.1).

Fig. 5.4.PNG

2. Draw a curve \(x_{t} = f(x_{t−1})\) and a diagonal line \(x_{t} = x_{t−1}\) within the square (Fig. 5.3.2). Note that the system’s equilibrium points appear in this plot as the points where the curve and the line intersect.

3. Draw a trajectory from \(x_{t−1}\) to \(x_{t}\). This can be done by using the curve \(x_{t} = f(x_{t−1)}\) (Fig. 5.3.3). Start from a current state value on the bottom axis (initially, this is the initial value \(x_{0}\), as shown in Fig. 5.3.3), and move vertically until you reach the curve. Then switch the direction of the movement to horizontal and reach the left axis. You end up at the next value of the system’s state (\(x_{1}\) in Fig. 5.3.3). The two red arrows connecting the two axes represent the trajectory between the two consecutive time points.

4. Reflect the new state value back onto the horizontal axis. This can be done as a simple mirror reflection using the diagonal line (Fig. 5.3.4). This completes one step of the “manual simulation” on the cobweb plot.

Fig. 5.5.PNG

5. Repeat the steps above to see where the system eventually goes (Fig. 5.3.5).
6. Once you get used to this process, you will notice that you don’t really have to touch either axis. All you need to do to draw a cobweb plot is to bounce back and forth between the curve and the line (Fig. 5.3.6)—move vertically to the curve, horizontally to the line, and repeat.

Exercise \(\PageIndex{1}\)

Draw a cobweb plot for each of the following models:

  • \[x_{t} = x_{t-1} +0.1, x_{0}=0.1\]
  • \[x_{t} =1.1x_{t-1}, x_{0}=0.1\]

Exercise \(\PageIndex{2}\)

Draw a cobweb plot of the following logistic growth model with \(r = 1\),\(K=1\), \(N_{0} =0.1\):

\[N_{t} =N_{t-1} +rN_{t-1}(1-\frac{N_{t-1}}{K})\label{ (5.17)}\]

Fig. 5.6.PNG

Cobweb plots can also be drawn using Python. Code 5.4 is an example of how to draw a cobweb plot of the exponential growth model (Code 4.9). Its output is given in Fig. 5.4.1.

code 5.4.png

Code alternate 1.png

Fig 5.8.PNG

code alternate 2.png

Exercise \(\PageIndex{3}\)

Using Python, draw a cobweb plot of the logistic growth model with \(r = 2.5\), \(K = 1\), \(N_{0} = 0.1\).

Fig 5.9.PNG
Sours: https://math.libretexts.org/Bookshelves/Scientific_Computing_Simulations_and_Modeling/Book%3A_Introduction_to_the_Modeling_and_Analysis_of_Complex_Systems_(Sayama)/05%3A_DiscreteTime_Models_II__Analysis/5.03%3A_5.3_Cobweb_Plots_for_One-Dimensional_Iterative_Maps
  1. File cabinet hanging racks
  2. Loyola university t shirts
  3. 2013 mustang v6 tune
  4. Large cap blend stocks
  5. Best miata brake pads

Cobweb plots are a way of visualizing iterations of a function.

cobweb plot for iterating cosine

For a function f and a starting point x, you plot (x, f(x)) as usual. Then since f(x) will be the next value of x, you convert it to an x by drawing a horizontal line from (x, f(x)) to (f(x), f(x)). In other words, you convert the previous y value to an x value by moving to where a horizontal line intersects the line y = x. Then you go up from the new x to f applied to the new x. The Python code below makes this all explicit.

Update: I made a couple changes after this post was first published. I added the dotted line y = x to the plots, and I changed the aspect ratio from the default to 1 to make the horizontal and vertical scales the same.

import matplotlib.pyplot as plt from scipy import cos, linspace def cobweb(f, x0, N, a=0, b=1): # plot the function being iterated t = linspace(a, b, N) plt.plot(t, f(t), 'k') # plot the dotted line y = x plt.plot(t, t, "k:") # plot the iterates x, y = x0, f(x0) for _ in range(N): fy = f(y) plt.plot([x, y], [y, y], 'b', linewidth=1) plt.plot([y, y], [y, fy], 'b', linewidth=1) x, y = y, fy plt.axes().set_aspect(1) plt.show() plt.close()

The plot above was made by calling

cobweb(cos, 1, 20)

to produce the cobweb plot for 20 iterations of cosine starting with x = 1. There’s one fixed point, and the cobweb plot spirals into that fixed point.

Next let’s look at several iterations of the logistic map f(x) = rx(1 – x) for differing values of r.

# one fixed point cobweb(lambda x: 2.9*x*(1-x), 0.1, 100) # converging to two-point attractor cobweb(lambda x: 3.1*x*(1-x), 0.1, 100) # starting exactly on the attractor cobweb(lambda x: 3.1*x*(1-x), 0.558, 100) # in the chaotic region. cobweb(lambda x: 4.0*x*(1-x), 0.1, 100)

The logistic map also has one stable fixed point if r ≤ 3. In the plot below, r = 2.9.

cobweb plot for logistic map, r = 2.9

Next we set r = 3.1. We start at x = 0.1 and converge to the two attractor points.

cobweb plot for logistic map, r = 3.1

If we start exactly on one of the attractor points the cobweb plot is simply a square.

cobweb plot for logistic map, r = 3.1, starting on attractor point

Finally, when we set r = 4 we’re in the chaotic region.

cobweb plot for logistic map, r = 4

More posts on iterated functions

Categories : Math

Tags : Dynamical systems

Bookmark the permalink

Sours: https://www.johndcook.com/blog/2020/01/19/cobweb-plots/

Cobweb Process Modeling with Python

4 minute read

Updated:

The task is to devise a model representing Cobweb Process.

Python is used for representation.


Modeling:

Let’s model the cobweb process into code.

Let’s install additional package first: pynverse.

This package is used to get inverse function.

Why?

In economic modeling, price P, the independent variable is usually located vertical axis, not horizontal axis.

Let’s import the setups.

Firstly, Let’s define the function to plot market status, plotMarket.

In here, plotMarket gets 4 parameters

  • D, S: functions(lambda) representing Demand and Supply Curve
  • Q_max, P_max: representing maximum range of the coordinate

And also, let’s setup a function which draws line between two points, connectPoints.

In here, connectPoints gets 2 parameters representing coordinates, by a list.

Next, let’s devise a cobweb process.

In here, plotMarket gets 6 parameters

  • D, S: functions(lambda) representing Demand and Supply Curve
  • S_adj: functions(lambda) representing suppliers’ adjusting behavior. How would suppliers’ react to unexpected, wrong “quantity demanded”?
  • init_price: as it’s name suggests!
  • ϵ: epsilon, which meant to be admissible error. If we can get the situation of S(Q) = S(D), it would be perfect. However, it is sometimes impossible because of market status, or by technical problem.
  • periods: This represents how many iterations or process being done.

Let’s do the setup first.

And in iteration, we have,

We now have current status of market as : S_cur, D_cur

If the economy is not in equilibrium, what would be the supplier’s response?

According to our model, given D(P), supplier would respond as supplying S_adjusting_function(D(P)).

Let’s experiment few examples of adjusting function later on!

Anyway, by that adjustment, next periods’ supply is determined. Here, S_next.

Next, we can define equilibrium condition as abs(D(P) - S(P)) < ϵ, which means quantity supplied and quantity demanded are almost identical.

If we are near equilibrium, break. report.

else, just report.

Case Study:

Let’s do some case analysis.

Case 1

$ D: Q = 1 - P $

$ S: Q = P $

$ Adj: Q_{n+1} = D^{-1}(P_n) * 0.9 $

$ P_0 = 0 $

Result for case 1:

case1

case 2

$ D: Q = 1 - P $

$ S: Q = P $

$ Adj: Q_{n+1} = D^{-1}(P_n) * 0.6 $

$ P_0 = 0 $

Result for case 2:

case2

case 3: Square Supply

$ D: Q = 100 - P $

$ S: Q = \frac{ P^2 }{100} $

$ Adj: Q_{n+1} = 0.9 * D^{-1}(P_n) $

$ P_0 = 0 $

Result for case 3:

case2


Code in jupyter notebook is available at my github.

Tags: cobweb, economic modeling, economics, python, quantecon

Categories: cobweb model

Updated:

Share on

Twitter Facebook LinkedIn
Sours: https://dongminkim0220.github.io/posts/cobweb/

Plot python cobweb

To the lake. - Yeah. Oddly enough, the unambiguous liberty of the grandfather did not in the least irritate or offend Irka.

Plotting sine function Python

And your game is not limited by any moral boundaries. Anyone who can say to himself: I am completely satisfied with my partner is a truly happy man. Most women just think they can please their partner well in sex.

You will also be interested:

Around the head and lick the eggs. Danya took Machines pigtails in his hands and began to thrust Masha's mouth onto his dick. I had no choice but to sit in a chair and watch this debauchery, while stroking my dick. Having poured out enough, Andrei asked him to suck, which Masha with great desire immediately fulfilled, substituting her ass under Danil's tongue.

Then Masha took a condom in her mouth and put on Andreev's penis: - Fuck me in the ass, Andrei, said.



558 559 560 561 562