Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Lab 3

Views: 222
#Exercise1
srange(1,101)
[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]
#Exercise2
srange(0,52,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, 50]
#Exercise3
nums = srange(0,11) for n in nums: n
0 1 2 3 4 5 6 7 8 9 10
#Exercise4
nums = srange(10.,50,10) for n in nums: sqrt(n)
3.16227766016838 4.47213595499958 5.47722557505166 6.32455532033676
#Exercise7 It appends (3*n) to the list of mult3. For every n in the list, you multiply by 3 and put the result in mult 3. #Exercise8
mult4 = [] nums = srange(1,11) for n in nums: mult4.append(4*n) mult4
[4, 8, 12, 16, 20, 24, 28, 32, 36, 40]
#Exercise9 list_plot(mult4)
#Exercise10 For the value of N(10), you get 11.7824723673356.
var("X") f(X)=0.2*X dt=1/2 X=10 for t in srange(0.,2,dt, include_endpoint=True): Xp=f(X) dX=dt*Xp [t,X,Xp,dX] X=X+dX
X [0.000000000000000, 10, 2.00000000000000, 1.00000000000000] [0.500000000000000, 11.0000000000000, 2.20000000000000, 1.10000000000000] [1.00000000000000, 12.1000000000000, 2.42000000000000, 1.21000000000000] [1.50000000000000, 13.3100000000000, 2.66200000000000, 1.33100000000000] [2.00000000000000, 14.6410000000000, 2.92820000000000, 1.46410000000000]
var("N") f(N)=0.2*N(1-N/100) dt=0.2 N=10 for t in srange(0.,10,dt, include_endpoint=True): Np=f(N) dN=dt*Np [t,N,Np,dN] N=N+dN
N
/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py:1234: DeprecationWarning: Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future release of Sage; you can use named arguments instead, like EXPR(x=..., y=...) See http://trac.sagemath.org/5930 for details. flags=compile_flags), namespace, locals)
[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]
#Exercise11
var("X") f(X)=0.2*X dt=0.1 X=10 Xvals=[X] tvals=srange(0.,10,dt, include_endpoint=True) for t in tvals: Xp=f(X) dX=dt*Xp X=X+dX Xvals.append(X) p=list_plot(zip(tvals,Xvals),axes_labels=["t","X"]) show(p,figsize=5,svg=False)
X
var("N") f(N)=0.2*N*(1-N/100) dt=0.1 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) p=list_plot(zip(tvals,Nvals),axes_labels=["t","N"]) show(p,figsize=5,svg=False)
N
#Exercise12 var("N") f(N)=0.2*N*(1-N/100) dt=0.1 N=3. 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) s=list_plot(zip(tvals,Nvals), axes_labels=["t","N"], color="red") show(s,figsize=5,svg=False)
N
var("N") f(N)=0.2*N*(1-N/100) dt=0.1 N=200 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) q=list_plot(zip(tvals,Nvals), axes_labels=["t","N"], color="purple") show(q,figsize=5,svg=False)
N
p+s+q
### Exercise 4:5/5 ### Exercise 12: 5/5 ### Overall Score: 10/10 (note that this is only the first half of the lab, the second half of the lab will be added to this score next week) ### nice job! ################################################################
#Exercise13 def f_of_list(fcn,xlist): ylist=[] for x in xlist: y=fcn(x) ylist.append(y) return ylist
h(x)=x^3 Nlist=srange(0,5) f_of_list(h,Nlist) [0,1,8,27,64]
[0, 1, 8, 27, 64] [0, 1, 8, 27, 64]
def Euler(Xprime,X0,t_start,t_end,dt): X=X0 Xvals=[X] tvals=srange(t_start,t_end,dt,include_endpoint=True) for t in tvals: Xp=Xprime(X) dX=Xp*dt X=dX+X Xvals.append(X) return(Xvals)
var("X") f(X)=0.2*X Euler(f(X),10,0,50,0.1)
X [10, 10.2000000000000, 10.4040000000000, 10.6120800000000, 10.8243216000000, 11.0408080320000, 11.2616241926400, 11.4868566764928, 11.7165938100227, 11.9509256862231, 12.1899441999476, 12.4337430839465, 12.6824179456255, 12.9360663045380, 13.1947876306287, 13.4586833832413, 13.7278570509061, 14.0024141919242, 14.2824624757627, 14.5681117252780, 14.8594739597835, 15.1566634389792, 15.4597967077588, 15.7689926419140, 16.0843724947523, 16.4060599446473, 16.7341811435402, 17.0688647664111, 17.4102420617393, 17.7584469029741, 18.1136158410335, 18.4758881578542, 18.8454059210113, 19.2223140394315, 19.6067603202202, 19.9988955266246, 20.3988734371571, 20.8068509059002, 21.2229879240182, 21.6474476824986, 22.0803966361485, 22.5220045688715, 22.9724446602489, 23.4318935534539, 23.9005314245230, 24.3785420530134, 24.8661128940737, 25.3634351519552, 25.8707038549943, 26.3881179320942, 26.9158802907361, 27.4541978965508, 28.0032818544818, 28.5633474915714, 29.1346144414029, 29.7173067302309, 30.3116528648355, 30.9178859221322, 31.5362436405749, 32.1669685133864, 32.8103078836541, 33.4665140413272, 34.1358443221537, 34.8185612085968, 35.5149324327687, 36.2252310814241, 36.9497357030526, 37.6887304171137, 38.4425050254559, 39.2113551259650, 39.9955822284844, 40.7954938730540, 41.6114037505151, 42.4436318255254, 43.2925044620359, 44.1583545512766, 45.0415216423022, 45.9423520751482, 46.8611991166512, 47.7984230989842, 48.7543915609639, 49.7294793921832, 50.7240689800268, 51.7385503596274, 52.7733213668199, 53.8287877941563, 54.9053635500394, 56.0034708210402, 57.1235402374610, 58.2660110422103, 59.4313312630545, 60.6199578883156, 61.8323570460819, 63.0690041870035, 64.3303842707436, 65.6169919561584, 66.9293317952816, 68.2679184311872, 69.6332767998110, 71.0259423358072, 72.4464611825234, 73.8953904061738, 75.3732982142973, 76.8807641785832, 78.4183794621549, 79.9867470513980, 81.5864819924260, 83.2182116322745, 84.8825758649200, 86.5802273822184, 88.3118319298627, 90.0780685684600, 91.8796299398292, 93.7172225386258, 95.5915669893983, 97.5033983291863, 99.4534662957700, 101.442535621685, 103.471386334119, 105.540814060801, 107.651630342017, 109.804662948858, 112.000756207835, 114.240771331992, 116.525586758632, 118.856098493804, 121.233220463680, 123.657884872954, 126.131042570413, 128.653663421821, 131.226736690258, 133.851271424063, 136.528296852544, 139.258862789595, 142.044040045387, 144.884920846295, 147.782619263220, 150.738271648485, 153.753037081455, 156.828097823084, 159.964659779545, 163.163952975136, 166.427232034639, 169.755776675332, 173.150892208838, 176.613910053015, 180.146188254075, 183.749112019157, 187.424094259540, 191.172576144731, 194.996027667625, 198.895948220978, 202.873867185398, 206.931344529106, 211.069971419688, 215.291370848081, 219.597198265043, 223.989142230344, 228.468925074951, 233.038303576450, 237.699069647979, 242.453051040938, 247.302112061757, 252.248154302992, 257.293117389052, 262.438979736833, 267.687759331570, 273.041514518201, 278.502344808565, 284.072391704737, 289.753839538831, 295.548916329608, 301.459894656200, 307.489092549324, 313.638874400311, 319.911651888317, 326.309884926083, 332.836082624605, 339.492804277097, 346.282660362639, 353.208313569892, 360.272479841289, 367.477929438115, 374.827488026878, 382.324037787415, 389.970518543163, 397.769928914027, 405.725327492307, 413.839834042153, 422.116630722996, 430.558963337456, 439.170142604205, 447.953545456290, 456.912616365415, 466.050868692724, 475.371886066578, 484.879323787910, 494.576910263668, 504.468448468941, 514.557817438320, 524.848973787087, 535.345953262828, 546.052872328085, 556.973929774647, 568.113408370139, 579.475676537542, 591.065190068293, 602.886493869659, 614.944223747052, 627.243108221993, 639.787970386433, 652.583729794162, 665.635404390045, 678.948112477846, 692.527074727403, 706.377616221951, 720.505168546390, 734.915271917318, 749.613577355664, 764.605848902777, 779.897965880833, 795.495925198449, 811.405843702418, 827.633960576467, 844.186639787996, 861.070372583756, 878.291780035431, 895.857615636140, 913.774767948863, 932.050263307840, 950.691268573997, 969.705093945477, 989.099195824386, 1008.88117974087, 1029.05880333569, 1049.63997940241, 1070.63277899045, 1092.04543457026, 1113.88634326167, 1136.16407012690, 1158.88735152944, 1182.06509856003, 1205.70640053123, 1229.82052854185, 1254.41693911269, 1279.50527789494, 1305.09538345284, 1331.19729112190, 1357.82123694434, 1384.97766168322, 1412.67721491689, 1440.93075921523, 1469.74937439953, 1499.14436188752, 1529.12724912527, 1559.70979410778, 1590.90398998993, 1622.72206978973, 1655.17651118553, 1688.28004140924, 1722.04564223742, 1756.48655508217, 1791.61628618381, 1827.44861190749, 1863.99758414564, 1901.27753582855, 1939.30308654512, 1978.08914827603, 2017.65093124155, 2058.00394986638, 2099.16402886371, 2141.14730944098, 2183.97025562980, 2227.64966074240, 2272.20265395724, 2317.64670703639, 2363.99964117712, 2411.27963400066, 2459.50522668067, 2508.69533121428, 2558.86923783857, 2610.04662259534, 2662.24755504725, 2715.49250614819, 2769.80235627116, 2825.19840339658, 2881.70237146451, 2939.33641889380, 2998.12314727168, 3058.08561021711, 3119.24732242145, 3181.63226886988, 3245.26491424728, 3310.17021253223, 3376.37361678287, 3443.90108911853, 3512.77911090090, 3583.03469311892, 3654.69538698129, 3727.78929472092, 3802.34508061534, 3878.39198222765, 3955.95982187220, 4035.07901830964, 4115.78059867583, 4198.09621064935, 4282.05813486234, 4367.69929755959, 4455.05328351078, 4544.15434918099, 4635.03743616461, 4727.73818488791, 4822.29294858566, 4918.73880755738, 5017.11358370852, 5117.45585538270, 5219.80497249035, 5324.20107194016, 5430.68509337896, 5539.29879524654, 5650.08477115147, 5763.08646657450, 5878.34819590599, 5995.91515982411, 6115.83346302059, 6238.15013228100, 6362.91313492662, 6490.17139762515, 6619.97482557766, 6752.37432208921, 6887.42180853099, 7025.17024470161, 7165.67364959565, 7308.98712258756, 7455.16686503931, 7604.27020234010, 7756.35560638690, 7911.48271851464, 8069.71237288493, 8231.10662034263, 8395.72875274948, 8563.64332780447, 8734.91619436056, 8909.61451824777, 9087.80680861272, 9269.56294478498, 9454.95420368068, 9644.05328775429, 9836.93435350938, 10033.6730405796, 10234.3465013912, 10439.0334314190, 10647.8141000474, 10860.7703820483, 11077.9857896893, 11299.5455054831, 11525.5364155927, 11756.0471439046, 11991.1680867827, 12230.9914485183, 12475.6112774887, 12725.1235030385, 12979.6259730992, 13239.2184925612, 13504.0028624124, 13774.0829196607, 14049.5645780539, 14330.5558696150, 14617.1669870073, 14909.5103267474, 15207.7005332824, 15511.8545439480, 15822.0916348270, 16138.5334675235, 16461.3041368740, 16790.5302196115, 17126.3408240037, 17468.8676404838, 17818.2449932934, 18174.6098931593, 18538.1020910225, 18908.8641328429, 19287.0414154998, 19672.7822438098, 20066.2378886860, 20467.5626464597, 20876.9138993889, 21294.4521773767, 21720.3412209242, 22154.7480453427, 22597.8430062496, 23049.7998663746, 23510.7958637021, 23981.0117809761, 24460.6320165956, 24949.8446569275, 25448.8415500661, 25957.8183810674, 26476.9747486887, 27006.5142436625, 27546.6445285358, 28097.5774191065, 28659.5289674886, 29232.7195468384, 29817.3739377752, 30413.7214165307, 31021.9958448613, 31642.4357617585, 32275.2844769937, 32920.7901665335, 33579.2059698642, 34250.7900892615, 34935.8058910467, 35634.5220088677, 36347.2124490450, 37074.1566980259, 37815.6398319864, 38571.9526286262, 39343.3916811987, 40130.2595148227, 40932.8647051191, 41751.5219992215, 42586.5524392059, 43438.2834879900, 44307.0491577498, 45193.1901409048, 46097.0539437229, 47018.9950225974, 47959.3749230493, 48918.5624215103, 49896.9336699405, 50894.8723433394, 51912.7697902061, 52951.0251860103, 54010.0456897305, 55090.2466035251, 56192.0515355956, 57315.8925663075, 58462.2104176336, 59631.4546259863, 60824.0837185060, 62040.5653928762, 63281.3767007337, 64547.0042347484, 65837.9443194433, 67154.7032058322, 68497.7972699488, 69867.7532153478, 71265.1082796548, 72690.4104452479, 74144.2186541528, 75627.1030272359, 77139.6450877806, 78682.4379895362, 80256.0867493269, 81861.2084843135, 83498.4326539997, 85168.4013070797, 86871.7693332213, 88609.2047198858, 90381.3888142835, 92189.0165905691, 94032.7969223805, 95913.4528608281, 97831.7219180447, 99788.3563564056, 101784.123483534, 103819.805953204, 105896.202072268, 108014.126113714, 110174.408635988, 112377.896808708, 114625.454744882, 116917.963839780, 119256.323116575, 121641.449578907, 124074.278570485, 126555.764141895, 129086.879424733, 131668.617013227, 134301.989353492, 136988.029140562, 139727.789723373, 142522.345517840, 145372.792428197, 148280.248276761, 151245.853242296, 154270.770307142, 157356.185713285, 160503.309427551, 163713.375616102, 166987.643128424, 170327.395990992, 173733.943910812, 177208.622789028, 180752.795244809, 184367.851149705, 188055.208172699, 191816.312336153, 195652.638582876, 199565.691354534, 203557.005181624]
<string>:1: DeprecationWarning: Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future release of Sage; you can use named arguments instead, like EXPR(x=..., y=...) See http://trac.sagemath.org/5930 for details.
var("X") #Assigning X as a variable. f(X)=0.2*X*(1-X/100) #Given a function for X prime. Nvals=Euler(f(X),10,0,50,0.1) #Making the X values come out of Eulers and putting it into a list. tvals=srange(0,50,0.1,include_endpoint=True) #Tvalues are being manufactured. list_plot(zip(tvals,Nvals)) #Shows the graph.
X
<string>:1: DeprecationWarning: Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future release of Sage; you can use named arguments instead, like EXPR(x=..., y=...) See http://trac.sagemath.org/5930 for details.
#Exercise14.1 var("X") f(X)=0.05*X Nvals=Euler(f(X),10,0,100,1) tvals=srange(0,100,1,include_endpoint=True) W=list_plot(zip(tvals,Nvals)) W
X
var("X") f(X)=0.05*X Nvals=Euler(f(X),10,0,100,0.1) tvals=srange(0,100,0.1,include_endpoint=True) Z=list_plot(zip(tvals,Nvals)) Z
X
var("X") f(X)=0.05*X Nvals=Euler(f(X),10,0,100,0.01) tvals=srange(0,100,0.01,include_endpoint=True) V=list_plot(zip(tvals,Nvals)) V
X
#Exercise14.2 E=plot(10*exp(0.05*x), (x,0,100)) E
W+Z+V+E
#Exercise14.3 The simulation with the step size of 1 is least efficient to the true solution. The simulations with the stepsize of 0.1, and 0.01 are the most efficient to the true solution. #Exercise15 var("J,R") vector_field(J,R)=[R,-J] ## the formula for J' and R’ ## make a vector field plot to use later pvf=plot_vector_field(vector_field,(J,-2,2),(R,-2,2),plot_points=10,color="green") dt=0.001 ## the time step J=1 ## the starting values of J R=0 ## and R Jvals=[J] ## start the lists for Jvals and Rvals Rvals=[R] ## tvals=srange(0.,7,dt, include_endpoint=True) ## list of time values for t in tvals: [Jp,Rp]=vector_field(J,R) ## get J' and R' dJ=dt*Jp ## use it to get Delta J dR=dt*Rp ## and Delta R J=J+dJ ## find the new J value R=R+dR ## and the new R value Jvals.append(J) ## append the new R and J values Rvals.append(R) ## to the lists. ## trajectory ptr=list_plot(zip(Jvals,Rvals),plotjoined=True,axes_labels=["J","R"],aspect_ratio=1) ## time series J pts1=list_plot(zip(tvals,Jvals),plotjoined=True,color='black',legend_label="Juliet") ## time series R pts2=list_plot(zip(tvals,Rvals),plotjoined=True,color='red',legend_label="Romeo") ## combine vector field and trajectory show(pvf+ptr,frame=False,axes_labels=["J","R"],aspect_ratio=1) ## combine the two time series show(pts1+pts2,axes_labels=["t","Fish"])
(J, R)
#Delta t or the step size must be at 0.001 for it to close.
#Exercise16
var("T,S") vector_field(T,S)=[0.5*T-0.01*S*T,0.005*S*T-0.2*S] pvf=plot_vector_field(vector_field,(T,0,120),(S,0,120),plot_points=10,color="green") dt=0.1 T=100 S=50 Tvals=[T] Svals=[S] tvals=srange(0.,50,dt, include_endpoint=True) for t in tvals: [Tp,Sp]=vector_field(T,S) dT=dt*Tp dS=dt*Sp T=T+dT S=S+dS Tvals.append(T) Svals.append(S) ptr=list_plot(zip(Tvals,Svals),plotjoined=True,axes_labels=["T","S"],aspect_ratio=1) pts1=list_plot(zip(tvals,Tvals),plotjoined=True,color='black',legend_label="Tuna") pts2=list_plot(zip(tvals,Svals),plotjoined=True,color='red',legend_label="Sharks") show(pvf+ptr,frame=False,axes_labels=["T","S"],aspect_ratio=1) show(pts1+pts2,axes_labels=["T","Fish"])
(T, S)