CoCalc Shared Files2020-02-24-124729.sagews
Author: Caterina Alvarez
Views : 19
exercise1List = srange(1,101, 1)
print "exercise 1:"
print(exercise1List)
exercise2List = srange(1,51,1)
print "exercise 2:"
print(exercise2List)
print "exercise 3:"
exercise3List = srange(0, 11, 1)
for i in exercise3List:
print i
print "exercise 4:"
exercise4List = srange(10, 50, 10)
for j in exercise4List:
sqareroot = float(sqrt(j))
print(sqareroot)

print "exercise 7:"
print "the line mult3.append(3*n) appends the empty list (mult3) with the values from the nums list, multiplied by 3"
mult3 = [] #Create an empty list to hold the results.
nums = srange(1,6) #Why do we use 6? #Because the srange function will only actually print values from 1 to 5
for n in nums:
mult3.append(3*n)
print mult3
[3, 6, 9, 12, 15]
print "exercise 8:"
exercise8List = []
multiplesof4 = srange(1,11)
for i in multiplesof4:
exercise8List.append(4*i)
print exercise8List
print "exercise 9:"
print "Plot of multiples of 4:"
show(points([(multiplesof4[0],exercise8List[0]),(multiplesof4[1],exercise8List[1]),(multiplesof4[2],exercise8List[2]),(multiplesof4[3],exercise8List[3]),(multiplesof4[4],exercise8List[4]),(multiplesof4[5],exercise8List[5]),(multiplesof4[6],exercise8List[6]),(multiplesof4[7],exercise8List[7]),(multiplesof4[8],exercise8List[8]),(multiplesof4[9],exercise8List[9])], color ='darkgreen', pointsize=50), aspect_ratio=.5)

print "exercise 10"

N = 10
func=0.2*N*(1-N/100) ## the formula for X’
dt=.2 ## the time step >>X=10. ## the starting value of X
for t in srange(0,10,dt, include_endpoint=True): ## ?????
Xp = func ## get X’
dX=dt*Xp ## use it to get Delta X
[t,N,Xp,dX] ## print the row of the Euler table
N=N+dX ## find the new X value

print "exercise 11"
N = 10
func=0.2*N*(1-N/100) ## the formula for X’
dt=.2 ## the time step >>X=10. ## the starting value of X
Xvals = []
tvals = srange(0,50,dt, include_endpoint=True)
for t in tvals: ## ?????
Xp = func ## get X’
dX=dt*Xp ## use it to get Delta X
#[t,N,Xp,dX] ## print the row of the Euler table
Xvals.append(N)##
N=N+dX ## find the new X value
#Xvals.append(N)##
p=list_plot(zip(tvals,Xvals),axes_labels=["t","X"])
show(p,figsize=5,svg=False)

print "exercise 12"
N = 30
func=0.2*N*(1-N/100) ## the formula for X’
dt=.2 ## the time step >>X=10. ## the starting value of X
Xvals1 = []
tvals = srange(0,50,dt, include_endpoint=True)
for t in tvals: ## ?????
Xp = func ## get X’
dX=dt*Xp ## use it to get Delta X
#[t,N,Xp,dX] ## print the row of the Euler table
Xvals1.append(N)##
N=N+dX ## find the new X value
#Xvals.append(N)##
N = 20
func=0.2*N*(1-N/100) ## the formula for X’
dt=.2 ## the time step >>X=10. ## the starting value of X
Xvals2 = []
tvals = srange(0,50,dt, include_endpoint=True)
for t in tvals: ## ?????
Xp = func ## get X’
dX=dt*Xp ## use it to get Delta X
#[t,N,Xp,dX] ## print the row of the Euler table
Xvals2.append(N)##
N=N+dX ## find the new X value
#Xvals.append(N)##
N = 10
func=0.2*N*(1-N/100) ## the formula for X’
dt=.2 ## the time step >>X=10. ## the starting value of X
Xvals3 = []
tvals = srange(0,50,dt, include_endpoint=True)
for t in tvals: ## ?????
Xp = func ## get X’
dX=dt*Xp ## use it to get Delta X
#[t,N,Xp,dX] ## print the row of the Euler table
Xvals3.append(N)##
N=N+dX ## find the new X value
#Xvals.append(N)##
p=list_plot(zip(tvals,Xvals3),axes_labels=["t","X"],color='darkgreen')
p1=list_plot(zip(tvals,Xvals2),axes_labels=["t","X"],color='red')
p2=list_plot(zip(tvals,Xvals1),axes_labels=["t","X"])
show((p+p1+p2),figsize=5,svg=False)
#line([(0,0), (1,2), (1/2,pi), (1/2,pi/2)], color='darkgreen', thickness=3)

print "exercise 13"
def Euler(Xprime,N,t_start,t_end,dt):
tvals = srange(t_start,t_end,dt, include_endpoint=True)
Xlist = []
for t in tvals: ## ?????
Xp = Xprime
dX=dt*Xp ## use it to get Delta X
Xlist.append(N)##
N=N+dX
#print Xlist
return(Xlist) ## return the list of X values.

print "exercise 14"
#Using your function Euler from Exercise 13, simulate X = 0.05X for 100 time units, starting with
#X(0) = 10, using step sizes of 1, 0.1 and 0.01.
#2. Plot each of your results and the actual solution X(t) given above on the same set of axes.
#3. Describe how similar each simulation is to the true solution.
onestep = Euler(0.05*N,10,0,100,1)
g=list_plot(zip(tvals,onestep),axes_labels=["t","X"],color='darkgreen')
twostep = Euler(0.05*N,10,0,100,.1)
g1=list_plot(zip(tvals,onestep),axes_labels=["t","X"],color='red')
threestep = Euler(0.05*N,10,0,100,.01)
g2=list_plot(zip(tvals,onestep),axes_labels=["t","X"],color='blue')
show((g+g1+g2),figsize=5,svg=False)

print "exercise 15"
print "in order to generate more accurate graphs we must make the step size sufficiently small"
print "exercise 16"
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.1 ## the time step
J=10 ## the starting values of J
R=50 ## and R
Jvals=[] ## start the lists for Jvals and Rvals
Rvals=[] ##
tvals=srange(0,120,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"])

exercise 1: [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] exercise 2: [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] exercise 3: 0 1 2 3 4 5 6 7 8 9 10 exercise 4: 3.16227766017 4.472135955 5.47722557505 6.32455532034 exercise 7: the line mult3.append(3*n) appends the empty list (mult3) with the values from the nums list, multiplied by 3 [3, 6, 9, 12, 15] [3, 6, 9, 12, 15] exercise 8: [4, 8, 12, 16, 20, 24, 28, 32, 36, 40] exercise 9: Plot of multiples of 4:
exercise 10 [0.000000000000000, 10, 1.80000000000000, 0.360000000000000] [0.200000000000000, 10.3600000000000, 1.80000000000000, 0.360000000000000] [0.400000000000000, 10.7200000000000, 1.80000000000000, 0.360000000000000] [0.600000000000000, 11.0800000000000, 1.80000000000000, 0.360000000000000] [0.800000000000000, 11.4400000000000, 1.80000000000000, 0.360000000000000] [1.00000000000000, 11.8000000000000, 1.80000000000000, 0.360000000000000] [1.20000000000000, 12.1600000000000, 1.80000000000000, 0.360000000000000] [1.40000000000000, 12.5200000000000, 1.80000000000000, 0.360000000000000] [1.60000000000000, 12.8800000000000, 1.80000000000000, 0.360000000000000] [1.80000000000000, 13.2400000000000, 1.80000000000000, 0.360000000000000] [2.00000000000000, 13.6000000000000, 1.80000000000000, 0.360000000000000] [2.20000000000000, 13.9600000000000, 1.80000000000000, 0.360000000000000] [2.40000000000000, 14.3200000000000, 1.80000000000000, 0.360000000000000] [2.60000000000000, 14.6800000000000, 1.80000000000000, 0.360000000000000] [2.80000000000000, 15.0400000000000, 1.80000000000000, 0.360000000000000] [3.00000000000000, 15.4000000000000, 1.80000000000000, 0.360000000000000] [3.20000000000000, 15.7600000000000, 1.80000000000000, 0.360000000000000] [3.40000000000000, 16.1200000000000, 1.80000000000000, 0.360000000000000] [3.60000000000000, 16.4800000000000, 1.80000000000000, 0.360000000000000] [3.80000000000000, 16.8400000000000, 1.80000000000000, 0.360000000000000] [4.00000000000000, 17.2000000000000, 1.80000000000000, 0.360000000000000] [4.20000000000000, 17.5600000000000, 1.80000000000000, 0.360000000000000] [4.40000000000000, 17.9200000000000, 1.80000000000000, 0.360000000000000] [4.60000000000000, 18.2800000000000, 1.80000000000000, 0.360000000000000] [4.80000000000000, 18.6400000000000, 1.80000000000000, 0.360000000000000] [5.00000000000000, 19.0000000000000, 1.80000000000000, 0.360000000000000] [5.20000000000000, 19.3600000000000, 1.80000000000000, 0.360000000000000] [5.40000000000000, 19.7200000000000, 1.80000000000000, 0.360000000000000] [5.60000000000000, 20.0800000000000, 1.80000000000000, 0.360000000000000] [5.80000000000000, 20.4400000000000, 1.80000000000000, 0.360000000000000] [6.00000000000000, 20.8000000000000, 1.80000000000000, 0.360000000000000] [6.20000000000000, 21.1600000000000, 1.80000000000000, 0.360000000000000] [6.40000000000000, 21.5200000000000, 1.80000000000000, 0.360000000000000] [6.60000000000000, 21.8800000000000, 1.80000000000000, 0.360000000000000] [6.80000000000000, 22.2400000000000, 1.80000000000000, 0.360000000000000] [7.00000000000000, 22.6000000000000, 1.80000000000000, 0.360000000000000] [7.20000000000000, 22.9600000000000, 1.80000000000000, 0.360000000000000] [7.40000000000000, 23.3200000000000, 1.80000000000000, 0.360000000000000] [7.60000000000000, 23.6800000000000, 1.80000000000000, 0.360000000000000] [7.80000000000000, 24.0400000000000, 1.80000000000000, 0.360000000000000] [8.00000000000000, 24.4000000000000, 1.80000000000000, 0.360000000000000] [8.20000000000000, 24.7600000000000, 1.80000000000000, 0.360000000000000] [8.40000000000000, 25.1200000000000, 1.80000000000000, 0.360000000000000] [8.60000000000000, 25.4800000000000, 1.80000000000000, 0.360000000000000] [8.80000000000000, 25.8400000000000, 1.80000000000000, 0.360000000000000] [9.00000000000000, 26.2000000000000, 1.80000000000000, 0.360000000000000] [9.20000000000000, 26.5600000000000, 1.80000000000000, 0.360000000000000] [9.40000000000000, 26.9200000000000, 1.80000000000000, 0.360000000000000] [9.60000000000000, 27.2800000000000, 1.80000000000000, 0.360000000000000] [9.80000000000000, 27.6400000000000, 1.80000000000000, 0.360000000000000] [10.0000000000000, 28.0000000000000, 1.80000000000000, 0.360000000000000] exercise 11
exercise 12
exercise 13 exercise 14
exercise 15 in order to generate more accurate graphs we must make the step size sufficiently small exercise 16 (J, R)