CoCalc Shared FilesHandouts / lab 3 8A.sagews
Author: joceline mayer
Views : 49
#1
srange(10)
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]

srange(1,100)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
srange(1,100,2)

[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]
srange(1,100,include_endpoint=True)

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100]
srange(1,50,0.5)

[1.00000000000000, 1.50000000000000, 2.00000000000000, 2.50000000000000, 3.00000000000000, 3.50000000000000, 4.00000000000000, 4.50000000000000, 5.00000000000000, 5.50000000000000, 6.00000000000000, 6.50000000000000, 7.00000000000000, 7.50000000000000, 8.00000000000000, 8.50000000000000, 9.00000000000000, 9.50000000000000, 10.0000000000000, 10.5000000000000, 11.0000000000000, 11.5000000000000, 12.0000000000000, 12.5000000000000, 13.0000000000000, 13.5000000000000, 14.0000000000000, 14.5000000000000, 15.0000000000000, 15.5000000000000, 16.0000000000000, 16.5000000000000, 17.0000000000000, 17.5000000000000, 18.0000000000000, 18.5000000000000, 19.0000000000000, 19.5000000000000, 20.0000000000000, 20.5000000000000, 21.0000000000000, 21.5000000000000, 22.0000000000000, 22.5000000000000, 23.0000000000000, 23.5000000000000, 24.0000000000000, 24.5000000000000, 25.0000000000000, 25.5000000000000, 26.0000000000000, 26.5000000000000, 27.0000000000000, 27.5000000000000, 28.0000000000000, 28.5000000000000, 29.0000000000000, 29.5000000000000, 30.0000000000000, 30.5000000000000, 31.0000000000000, 31.5000000000000, 32.0000000000000, 32.5000000000000, 33.0000000000000, 33.5000000000000, 34.0000000000000, 34.5000000000000, 35.0000000000000, 35.5000000000000, 36.0000000000000, 36.5000000000000, 37.0000000000000, 37.5000000000000, 38.0000000000000, 38.5000000000000, 39.0000000000000, 39.5000000000000, 40.0000000000000, 40.5000000000000, 41.0000000000000, 41.5000000000000, 42.0000000000000, 42.5000000000000, 43.0000000000000, 43.5000000000000, 44.0000000000000, 44.5000000000000, 45.0000000000000, 45.5000000000000, 46.0000000000000, 46.5000000000000, 47.0000000000000, 47.5000000000000, 48.0000000000000, 48.5000000000000, 49.0000000000000, 49.5000000000000]
#2
srange(50)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
srange(0,50)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]
srange(0,50,2)

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48]
srange(0,50,include_endpoint=True)

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50]
srange(0,25,0.5)

[0.000000000000000, 0.500000000000000, 1.00000000000000, 1.50000000000000, 2.00000000000000, 2.50000000000000, 3.00000000000000, 3.50000000000000, 4.00000000000000, 4.50000000000000, 5.00000000000000, 5.50000000000000, 6.00000000000000, 6.50000000000000, 7.00000000000000, 7.50000000000000, 8.00000000000000, 8.50000000000000, 9.00000000000000, 9.50000000000000, 10.0000000000000, 10.5000000000000, 11.0000000000000, 11.5000000000000, 12.0000000000000, 12.5000000000000, 13.0000000000000, 13.5000000000000, 14.0000000000000, 14.5000000000000, 15.0000000000000, 15.5000000000000, 16.0000000000000, 16.5000000000000, 17.0000000000000, 17.5000000000000, 18.0000000000000, 18.5000000000000, 19.0000000000000, 19.5000000000000, 20.0000000000000, 20.5000000000000, 21.0000000000000, 21.5000000000000, 22.0000000000000, 22.5000000000000, 23.0000000000000, 23.5000000000000, 24.0000000000000, 24.5000000000000]
#3
nums = srange(0,11)
for n in nums:
n

0 1 2 3 4 5 6 7 8 9 10
#4
nums = srange(10.,41.,10)
for n in nums:
sqrt(n)

3.16227766016838 4.47213595499958 5.47722557505166 6.32455532033676
#7
mult3 = []

nums = srange(1,6)

for n in nums:
mult3.append(3*n)

mult3

[3, 6, 9, 12, 15]
#mult3.append(3*n) take the multiples between the set 1 and 6 and multiply it by 3.

#8
mult4 = []

nums = srange(1,11)

for n in nums:
mult4.append(4*n)

mult4

[4, 8, 12, 16, 20, 24, 28, 32, 36, 40]
#9
numbers = mult4
list_plot(numbers)

#10
var("N")
f(N)=0.2*N(1-N/100)
dt=0.2
N=10
for t in srange(0.,2,dt, include_endpoint=True):
Np=f(N)
dN=dt*Np
[t,N,Np,dN]
N=N+dN

N [0.000000000000000, 10, 0.180000000000000, 0.0360000000000000] [0.200000000000000, 10.0360000000000, 0.179928000000000, 0.0359856000000000] [0.400000000000000, 10.0719856000000, 0.179856028800000, 0.0359712057600000] [0.600000000000000, 10.1079568057600, 0.179784086388480, 0.0359568172776960] [0.800000000000000, 10.1439136230377, 0.179712172753925, 0.0359424345507849] [1.00000000000000, 10.1798560575885, 0.179640287884823, 0.0359280575769646] [1.20000000000000, 10.2157841151654, 0.179568431769669, 0.0359136863539338] [1.40000000000000, 10.2516978015194, 0.179496604396961, 0.0358993208793923] [1.60000000000000, 10.2875971223988, 0.179424805755202, 0.0358849611510405] [1.80000000000000, 10.3234820835498, 0.179353035832900, 0.0358706071665801] [2.00000000000000, 10.3593526907164, 0.179281294618567, 0.0358562589237134]
var("N")
f(N)=0.2*N(1-N/100)
dt=0.2
N=10
for t in srange(0.,11,dt, include_endpoint=True):
Np=f(N)
dN=dt*Np
[t,N,Np,dN]
N=N+dN

N [0.000000000000000, 10, 0.180000000000000, 0.0360000000000000] [0.200000000000000, 10.0360000000000, 0.179928000000000, 0.0359856000000000] [0.400000000000000, 10.0719856000000, 0.179856028800000, 0.0359712057600000] [0.600000000000000, 10.1079568057600, 0.179784086388480, 0.0359568172776960] [0.800000000000000, 10.1439136230377, 0.179712172753925, 0.0359424345507849] [1.00000000000000, 10.1798560575885, 0.179640287884823, 0.0359280575769646] [1.20000000000000, 10.2157841151654, 0.179568431769669, 0.0359136863539338] [1.40000000000000, 10.2516978015194, 0.179496604396961, 0.0358993208793923] [1.60000000000000, 10.2875971223988, 0.179424805755202, 0.0358849611510405] [1.80000000000000, 10.3234820835498, 0.179353035832900, 0.0358706071665801] [2.00000000000000, 10.3593526907164, 0.179281294618567, 0.0358562589237134] [2.20000000000000, 10.3952089496401, 0.179209582100720, 0.0358419164201440] [2.40000000000000, 10.4310508660603, 0.179137898267880, 0.0358275796535759] [2.60000000000000, 10.4668784457138, 0.179066243108572, 0.0358132486217145] [2.80000000000000, 10.5026916943355, 0.178994616611329, 0.0357989233222658] [3.00000000000000, 10.5384906176578, 0.178923018764684, 0.0357846037529369] [3.20000000000000, 10.5742752214107, 0.178851449557179, 0.0357702899114357] [3.40000000000000, 10.6100455113222, 0.178779908977356, 0.0357559817954711] [3.60000000000000, 10.6458014931177, 0.178708397013765, 0.0357416794027529] [3.80000000000000, 10.6815431725204, 0.178636913654959, 0.0357273827309918] [4.00000000000000, 10.7172705552514, 0.178565458889497, 0.0357130917778994] [4.20000000000000, 10.7529836470293, 0.178494032705941, 0.0356988065411883] [4.40000000000000, 10.7886824535705, 0.178422635092859, 0.0356845270185718] [4.60000000000000, 10.8243669805891, 0.178351266038822, 0.0356702532077644] [4.80000000000000, 10.8600372337968, 0.178279925532406, 0.0356559851064813] [5.00000000000000, 10.8956932189033, 0.178208613562193, 0.0356417227124387] [5.20000000000000, 10.9313349416157, 0.178137330116769, 0.0356274660233537] [5.40000000000000, 10.9669624076391, 0.178066075184722, 0.0356132150369444] [5.60000000000000, 11.0025756226760, 0.177994848754648, 0.0355989697509296] [5.80000000000000, 11.0381745924270, 0.177923650815146, 0.0355847301630292] [6.00000000000000, 11.0737593225900, 0.177852481354820, 0.0355704962709640] [6.20000000000000, 11.1093298188610, 0.177781340362278, 0.0355562680724556] [6.40000000000000, 11.1448860869334, 0.177710227826133, 0.0355420455652266] [6.60000000000000, 11.1804281324986, 0.177639143735003, 0.0355278287470005] [6.80000000000000, 11.2159559612456, 0.177568088077509, 0.0355136176155018] [7.00000000000000, 11.2514695788611, 0.177497060842278, 0.0354994121684555] [7.20000000000000, 11.2869689910296, 0.177426062017941, 0.0354852124035882] [7.40000000000000, 11.3224542034332, 0.177355091593134, 0.0354710183186267] [7.60000000000000, 11.3579252217518, 0.177284149556496, 0.0354568299112993] [7.80000000000000, 11.3933820516631, 0.177213235896674, 0.0354426471793348] [8.00000000000000, 11.4288246988424, 0.177142350602315, 0.0354284701204630] [8.20000000000000, 11.4642531689629, 0.177071493662074, 0.0354142987324148] [8.40000000000000, 11.4996674676953, 0.177000665064609, 0.0354001330129219] [8.60000000000000, 11.5350676007082, 0.176929864798584, 0.0353859729597167] [8.80000000000000, 11.5704535736680, 0.176859092852664, 0.0353718185705328] [9.00000000000000, 11.6058253922385, 0.176788349215523, 0.0353576698431046] [9.20000000000000, 11.6411830620816, 0.176717633875837, 0.0353435267751674] [9.40000000000000, 11.6765265888568, 0.176646946822286, 0.0353293893644573] [9.60000000000000, 11.7118559782212, 0.176576288043558, 0.0353152576087115] [9.80000000000000, 11.7471712358299, 0.176505657528340, 0.0353011315056680] [10.0000000000000, 11.7824723673356, 0.176435055265329, 0.0352870110530658] [10.2000000000000, 11.8177593783887, 0.176364481243223, 0.0352728962486445] [10.4000000000000, 11.8530322746373, 0.176293935450725, 0.0352587870901451] [10.6000000000000, 11.8882910617275, 0.176223417876545, 0.0352446835753090] [10.8000000000000, 11.9235357453028, 0.176152928509394, 0.0352305857018789] [11.0000000000000, 11.9587663310046, 0.176082467337991, 0.0352164934675981]
#The approximte value of N(10) is [10.000000000000, 11.78247236733356, 0.176435055265239]

#11
var("N")
f(N)=0.2*N(1-N/100)
dt=0.2
N=10.
Nvals=[N]
tvals=srange(0.,50,dt, include_endpoint=True)
for t in tvals:
Np=f(N)
dN=dt*Np
N=N+dN
Nvals.append(N)

N
p=list_plot(zip(tvals,Nvals) ,axes_labels=["t","N"])
show(p,figsize=5,svg=False)

-------------------------------------------------------------------------------------------------------------------------------------------------
#13
def Eular(Xprime,X0,t_start,t_end, dt):
return(Xlist)
var("N")
Nprime(N)=0.2*N*(1-N/100)
Nlist=Eular(Nprime,10,0,40,0.1)
tvals=srange(0,40,0.1, include_endpoint=True)
list_plot(zip(tvals,Nlist) ,plotjoined=True,axes_labels=["t" , "N"])

Error in lines 1-1 Traceback (most recent call last): File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1234, in execute flags=compile_flags), namespace, locals) File "<string>", line 1 ------------------------------------------------------------------------------------------------------------------------------------------------- ^ SyntaxError: invalid syntax
#16
var("S" , "T")
vector_field(S,T)=[0.005*T*S-0.2*S,0.5*T-0.01*S*T]

pvf=plot_vector_field(vector_field,(S,0,120),
(T,0,120),plot_points=10,color="black")
dt=0.05
S=50
T=100
Svals=[S]
Tvals=[T]
tvals=srange(0.,120,dt, include_endpoint=True)
for t in tvals:
[Sp,Tp]=vector_field(S,T)
dS=dt*Sp
dT=dt*Tp
S=S+dS
T=T+dT
Svals.append(S)
Tvals.append(T)
prt=list_plot(zip(Svals,Tvals),plotjoined=True,axes_labels=
["S","T"],aspect_ratio=1)

(S, T)
prt=list_plot(zip(Svals,Tvals),plotjoined=True,axes_labels=["S","T"],aspect_ratio=1)

pts1=list_plot(zip(tvals,Svals),plotjoined=True,color="purple",legend_label="Shark")

prt=list_plot(zip(Svals,Tvals),plotjoined=True,axes_labels=["S","T"],aspect_ratio=1)

pts1=list_plot(zip(tvals,Tvals),plotjoined=True,color="purple",legend_label="Skark")

pts2=list_plot(zip(tvals,Tvals),plotjoined=True,color="green",legend_label="Tuna")

show(pvf=ptr,frame=False,axes_labels=["S","T"],aspect_ratio=1)

show(pts1+pts2,axes_labels=["t","Fish"])

#15
var("J,R")
vector_field(J,R)=[R,-J]

pvf=plot_vector_field(vector_field,(J,-2,2),
(R,-2,2),plot_points=10,color="green")
dt=.001
J=1
R=0
Jvals=[J]
Rvals=[R]
tvals=srange(0.,7,dt, include_endpoint=True)
for t in tvals:
[Jp,Rp]=vector_field(J,R)
dJ=dt*Rp
dR=dt*Rp
J=J+dR
R=R+dR
Jvals.append(J)
Rvals.append(R)

(J, R)
ptr=list_plot(zip(Jvals,Rvals),plotjoined=True,axes_labels=["J","R"],aspect_ratio=1)

pts1=list_plot(zip(tvals,Jvals),plotjoined=True,color='black',legend_label="Juliet")

pts2=list_plot(zip(tvals,Rvals),plotjoined=True,color='red',legend_label="Romeo")

show(pvf+ptr,frame=False,axes_labels=["J","R"],aspect_ratio=1)

show(pts1+pts2,axes_labels=["t","Fish"])