Ask Your Question
1

Plotting life history vectors in sage

asked 2018-04-15 20:08:56 +0100

maeve.mccarthy gravatar image

updated 2018-04-16 08:35:56 +0100

slelievre gravatar image

I'm teaching a math bio class where I need to plot a life history graph. The matrix equation is new=A*old, where A is a square matrix. I want to create a matrix of the population at each iteration (for say 10 iterations) and plot each column separately. I'm having trouble figuring it out and would appreciate some help.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2018-04-15 20:32:46 +0100

slelievre gravatar image

updated 2018-04-15 23:13:39 +0100

One way is to define the matrix a and the list p of vectors of populations, initially containing just the vector of initial populations:

sage: a = matrix(RR, 2, [1.02, -0.01, 0.03, 1.03])
sage: p = [vector(RR, 2, [300, 200])]

then extend the list by applying a number of times the matrix to the last vector:

sage: for k in range(20):
....:     p.append(a * p[-1])

then observe the evolution of populations:

sage: p
[(300.000000000000, 200.000000000000),
 (304.000000000000, 215.000000000000),
 (307.930000000000, 230.570000000000),
 (311.782900000000, 246.725000000000),
 (315.551308000000, 263.480237000000),
 (319.227531790000, 280.851183350000),
 (322.803570592300, 298.853544804200),
 (326.271106556104, 317.503258266095),
 (329.621496104565, 336.816489210761),
 (332.845761134549, 356.809628770221),
 (335.934580069538, 377.499290467364),
 (338.878278766255, 398.902306583471),
 (341.666821275745, 421.035724143963),
 (344.289800459820, 443.916800506554),
 (346.736428463951, 467.562998535545),
 (348.995527047875, 491.991981345530),
 (351.055517775377, 517.221606597332),
 (352.904412064911, 543.269920328514),
 (354.529801102924, 570.155150300316),
 (355.918845621980, 597.895698842414),
 (357.058265545995, 626.510135176345)]

or plot it as a set of points:

sage: point2d(p)
Launched png viewer for Graphics object consisting of 1 graphics primitive

or one could color the first point in red:

sage: point2d(p[0], color='red') + point2d(p[1:])
Launched png viewer for Graphics object consisting of 2 graphics primitives

or one could use line2d instead of point2d, or one could use animate to animate the evolution.

If you want to plot the evolution of each population:

sage: x, y = zip(*p)
sage: px = list_plot(x, color='green')
sage: py = list_plot(y, color='purple')
sage: px
Launched png viewer for Graphics object consisting of 1 graphics primitive
sage: py
Launched png viewer for Graphics object consisting of 1 graphics primitive
sage: px + py
Launched png viewer for Graphics object consisting of 2 graphics primitives
edit flag offensive delete link more

Comments

This helps a lot. Thank you!

maeve.mccarthy gravatar imagemaeve.mccarthy ( 2018-04-15 21:45:13 +0100 )edit

How do I plot each column separately? This gives the first column vs the second column. Suppose I want to plot the column entries from the first column on the y-axis and the index on the x-axis.

maeve.mccarthy gravatar imagemaeve.mccarthy ( 2018-04-15 21:58:30 +0100 )edit

Edited answer to address your follow-up question.

slelievre gravatar imageslelievre ( 2018-04-15 23:14:06 +0100 )edit

Thanks so much! You've really rescued me!

maeve.mccarthy gravatar imagemaeve.mccarthy ( 2018-04-16 00:03:13 +0100 )edit
1

answered 2018-04-15 20:29:47 +0100

tmonteil gravatar image

updated 2018-04-15 20:30:16 +0100

We need much more input to help seriously, like the size of the matrix, a concrete example of A, an example of picture you want to obtain.

Waiting for more details, here is a sample of what you can easily get:

sage: A = matrix([[2,1],[1,1]])
sage: A
[2 1]
[1 1]
sage: C = matrix([[2],[3]])
sage: C
[2]
[3]
sage: A * C
[7]
[5]
sage: L = []
sage: L = [C]
sage: for i in range(10):
....:     C = A*C
....:     L.append(C)
sage: L
[
[2]  [7]  [19]  [50]  [131]  [343]  [898]  [2351]  [6155]  [16114]
[3], [5], [12], [31], [ 81], [212], [555], [1453], [3804], [ 9959],

[42187]
[26073]
]
edit flag offensive delete link more

Comments

Thank you!

maeve.mccarthy gravatar imagemaeve.mccarthy ( 2018-04-15 21:45:18 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-04-15 20:08:56 +0100

Seen: 185 times

Last updated: Apr 16 '18