Below is my current code:
import pandas as pd
import math
import csv
fund = 10000
print("investment",fund)
pval = 0
oldportfolio = []
dts = ["06 Feb 2017", "07 Feb 2017", "08 Feb 2017", "09 Feb 2017", "10 Feb 2017", "13 Feb 2017", "14 Feb 2017", "15 Feb 2017", "16 Feb 2017", "17 Feb 2017",
"20 Feb 2017", "21 Feb 2017", "22 Feb 2017", "23 Feb 2017", "27 Feb 2017"]
for dt in dts:
files = ["stocklistcustom.csv"]
for file in files:
df = pd.read_csv(file, header=None)
i = 0
filecount = len(df)
result = []
while i < filecount:
# while i < 10:
name = df[0][i]
link = df[1][i]
mcsym = df[2][i]
i = i + 1
filepath = "data/nse/his/" + mcsym + ".csv"
try:
sp = pd.read_csv(filepath, header=None)
endrow = sp[sp[0] == dt].index[0] + 1
parray = []
tarray = []
starray = []
intdate = []
p1 = 0
p2 = 0
p3 = 0
p4 = 0
j = 0
mavg15 = ''
mavg60 = ''
olddiff = 0
days = 2
strtrow = endrow - days - 60
for k in range (strtrow, endrow):
date = sp[0][k]
price = float(sp[4][k])
k = k + 1
parray.append(price)
j = j + 1
strtavg = j - 15
mavg15 = sum(parray[strtavg:j]) / 15
strtavg = j - 60
mavg60 = sum(parray[strtavg:j]) / 60
# buy criteria
if j > 59:
diff = mavg60 - mavg15
if diff < 0 and olddiff > 0:
trigger = 1
intdate.append(date)
else:
trigger = 0
tarray.append(trigger)
olddiff = diff
# sell criteria
if j == (days + 60):
pricep = (price - p1) * 100 / p1
p1p = (p1 - p2) * 100 / p2
p2p = (p2 - p3) * 100 / p3
p3p = (p3 - p4) * 100 / p4
if pricep < -5 or pricep > 8:
sell = 1
if price < p1 and p1 < p2 and p2 < p3:
sell = 1
else:
sell = 0
p4 = p3
p3 = p2
p2 = p1
p1 = price
if sum(tarray) > 0:
result.append([name,mcsym,"buy",price])
if sell > 0:
result.append([name,mcsym,"sell",price])
except:
# print(name,"not found")
pass
# print(result)
output = "output/triggers/"+dt+"trigger.csv"
with open(output, "wb") as f:
writer = csv.writer(f)
writer.writerows(result)
print(output,"exported")
The above code create an array named result and exports various csv files with calls...
The code below now process the data in result array to compute portfolio value
# Code for calculating investment
portfolio = []
for row in result:
if row[2] == "sell" and len(oldportfolio) > 0:
pindex = 0
for buys in oldportfolio:
bindex = 0
for stock in buys:
if row[0] == stock[0]:
sellqty = stock[2]
sellp = row[3]
sellval = sellqty * sellp
purchasep = stock[1]
sellcost = purchasep * sellqty
print(dt,"selling",row[0],row[1],sellp,sellqty,sellval)
# print(oldportfolio)
del oldportfolio[pindex][bindex]
# print(oldportfolio)
fund = fund + sellval
pval = pval - sellcost
bindex = bindex + 1
pindex = pindex + 1
# print("op", oldportfolio)
# print(dt,"fund after selling",fund)
buycount = sum(1 for row in result if row[2]==("buy"))
if buycount > 0:
maxinvest = fund / buycount
for row in result:
if row[2] == "buy":
name = row[0]
price = row[3]
qty = math.floor(maxinvest / price)
if qty > 0:
val = qty * price
print(dt,"buying",name,row[1],price,qty,val)
portfolio.append([name,price,qty,val])
fund = fund - val
# print("portfolio",portfolio)
pval = pval + sum(row[3] for row in portfolio)
print(dt,"cash",fund,"portfolio value",pval,"total",fund+pval)
oldportfolio.append(portfolio)
print(oldportfolio)
It gives me the value of portfolio for each day after trading based on certain rules. But its execution time is too much. How to reduce its execution time?
Also, I need to change pval as it is calculated incorrectly in current code. It must be calculated based on that particular day's prices.
Your code has multiple nested loops which probably why it is so slow.
But your biggest problem isn't speed, it's readability. It is really hard to reason about your code, consider refactoring.
I'm sure you'll find some bottlenecks and be able to improve your code while refactoring.
Related
I have requirement to generate 4 character serial numbers like below in Python or Shell Scripting.
Serial number should start from 0001, 0002..... when reached 999 it should generate A001,A002....A999, then B001, so on.
I tried below code in Python, but its not fully working, After few number it starts generating 5 characters..
def excel_format(num):
res = ""
while num:
mod = (num - 1) % 26
res = chr(65 + mod) + res
num = (num - mod) // 26
return res
def full_format(num, d=3):
set_flag = 0
chars = num // (10**d-1) + 1 # this becomes A..ZZZ
if len(excel_format(chars)) >= 2:
set_flag = 1
if len(excel_format(chars)) > 2:
set_flag = 2
if set_flag == 1:
d = 2
chars = num // (10 ** d - 1) + 1 # this becomes A..ZZZ
digit = num % (10**d-1) + 1 # this becomes 001..999
return excel_format(chars) + "{:0{}d}".format(digit, d)
if __name__ == '__main__':
for i in range(1,10001):
unique_code = full_format(j, d=3)
print('Unique Code is =>', unique_code)
This Python code will generate the required 4-character (Unique_code) serial numbers:
#!/usr/bin/python3
import re
for i in range(1,10000):
if (i < 1000):
print ("i =", str(i).zfill(4))
else:
m = re.findall(r'(\d)(\d\d\d)', str(i))
code = 64+int(m[0][0])
print ("i =",i, "Unique_code =", chr(code) + m[0][1])
Excerpts from output:
i = 0001
i = 0002
...
i = 0999
i = 1000 Unique_code = A000
i = 1001 Unique_code = A001
i = 1002 Unique_code = A002
...
i = 1997 Unique_code = A997
i = 1998 Unique_code = A998
i = 1999 Unique_code = A999
i = 2000 Unique_code = B000
i = 2001 Unique_code = B001
i = 2002 Unique_code = B002
i = 2003 Unique_code = B003
...
i = 9997 Unique_code = I997
i = 9998 Unique_code = I998
i = 9999 Unique_code = I999
Not familiar enough with Python but since you've flagged ksh
#!/bin/ksh
typeset -Z3 sn
Letter=( 0 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z )
Index=0
while [[ $Index -lt 28 ]]; do
sn=0
while [[ $sn -lt 1000 ]]; do
print ${Letter[$Index]}$sn
((sn++))
done
((Index++))
done
I am new to GLPK, so my apologies in advance if I'm missing something simple!
I have a largeish LP that I am feeding through GLPK to model an energy market. I'm running the following command line to GLPK to process this:
winglpk-4.65\glpk-4.65\w64\glpsol --lp problem.lp --data ExampleDataFile.dat --output results2.txt
When I open the resulting text file I can see the outputs, which all look sensible. I have one big problem: each record is split over two rows, making it very difficult to clean the file. See an extract below:
No. Row name St Activity Lower bound Upper bound Marginal
------ ------------ -- ------------- ------------- ------------- -------------
1 c_e_SpecifiedDemand(UTOPIA_CSV_ID_1990)_
NS 0 0 = < eps
2 c_e_SpecifiedDemand(UTOPIA_CSV_ID_1991)_
NS 0 0 = < eps
3 c_e_SpecifiedDemand(UTOPIA_CSV_ID_1992)_
NS 0 0 = < eps
4 c_e_SpecifiedDemand(UTOPIA_CSV_ID_1993)_
NS 0 0 = < eps
5 c_e_SpecifiedDemand(UTOPIA_CSV_ID_1994)_
NS 0 0 = < eps
6 c_e_SpecifiedDemand(UTOPIA_CSV_ID_1995)_
NS 0 0 = < eps
7 c_e_SpecifiedDemand(UTOPIA_CSV_ID_1996)_
NS 0 0 = < eps
8 c_e_SpecifiedDemand(UTOPIA_CSV_ID_1997)_
NS 0 0 = < eps
9 c_e_SpecifiedDemand(UTOPIA_CSV_ID_1998)_
NS 0 0 = < eps
10 c_e_SpecifiedDemand(UTOPIA_CSV_ID_1999)_
NS 0 0 = < eps
11 c_e_SpecifiedDemand(UTOPIA_CSV_ID_2000)_
NS 0 0 = < eps
12 c_e_SpecifiedDemand(UTOPIA_CSV_ID_2001)_
NS 0 0 = < eps
13 c_e_SpecifiedDemand(UTOPIA_CSV_ID_2002)_
NS 0 0 = < eps
14 c_e_SpecifiedDemand(UTOPIA_CSV_ID_2003)_
NS 0 0 = < eps
15 c_e_SpecifiedDemand(UTOPIA_CSV_ID_2004)_
NS 0 0 = < eps
I would be very grateful of any suggestions for either:
How I can get each record in the output text file onto a single row, or
Ideas on how to clean / post-process the existing text file output.
I'm sure I'm missing something simple here, but the output is in a very unhelpful format at the moment!
Thanks!
I wrote a Python parser for the GLPK output file. It is not beautiful and not save (try-catch) but it is working (for pure simplex problems).
You can call it on output file:
outp = GLPKOutput('myoutputfile')
print(outp)
val1 = outp.getCol('mycolvar','Activity')
val2 = outp.getRow('myrowname','Upper_bound') # row names should be defined
The class is as follows:
class GLPKOutput:
def __init__(self,filename):
self.rows = {}
self.columns = {}
self.nRows = 0
self.nCols = 0
self.nNonZeros = 0
self.Status = ""
self.Objective = ""
self.rowHeaders = []
self.rowIdx = {}
self.rowWidth = []
self.Rows = []
self.hRows = {}
self.colHeaders = []
self.colIdx = {}
self.colWidth = []
self.Cols = []
self.hCols = {}
self.wcols = ['Activity','Lower_bound','Upper bound','Marginal']
self.readFile(filename)
# split columns with weird line break
def smartSplit(self,line,type,job):
ret = []
line = line.rstrip()
if type == 'ROWS':
cols = len(self.rowHeaders)
idx = self.rowWidth
else:
cols = len(self.colHeaders)
idx = self.colWidth
if job == 'full':
start = 0
for i in range(cols):
stop = start+idx[i]+1
ret.append(line[start:stop].strip())
start = stop
elif job == 'part1':
entries = line.split()
ret = entries[0:2]
elif job == 'part2':
start = 0
for i in range(cols):
stop = start+idx[i]+1
ret.append(line[start:stop].strip())
start = stop
ret = ret[2:]
# print()
# print("SMART:",job,line.strip())
# print(" TO:",ret)
return ret
def readFile(self,filename):
fp = open(filename,"r")
lines = fp.readlines()
fp.close
i = 0
pos = "HEAD"
while pos == 'HEAD' and i<len(lines):
entries = lines[i].split()
if len(entries)>0:
if entries[0] == 'Rows:':
self.nRows = int(entries[1])
elif entries[0] == 'Columns:':
self.nCols = int(entries[1])
elif entries[0] == 'Non-zeros:':
self.nNonZeros = int(entries[1])
elif entries[0] == 'Status:':
self.Status = entries[1]
elif entries[0] == 'Objective:':
self.Objective = float(entries[3]) #' '.join(entries[1:])
elif re.search('Row name',lines[i]):
lines[i] = lines[i].replace('Row name','Row_name')
lines[i] = lines[i].replace('Lower bound','Lower_bound')
lines[i] = lines[i].replace('Upper bound','Upper_bound')
entries = lines[i].split()
pos = 'ROWS'
self.rowHeaders = entries
else:
pass
i+= 1
# formatting of row width
self.rowWidth = lines[i].split()
for k in range(len(self.rowWidth)): self.rowWidth[k] = len(self.rowWidth[k])
# print("Row Widths:",self.rowWidth)
i+= 1
READY = False
FOUND = False
while pos == 'ROWS' and i<len(lines):
if re.match('^\s*[0-9]+',lines[i]): # new line
if len(lines[i].split())>2: # no linebrak
entries = self.smartSplit(lines[i],pos,'full')
READY = True
else: # line break
entries = self.smartSplit(lines[i],pos,'part1')
READY = False
FOUND = True
else:
if FOUND and not READY: # second part of line
entries += self.smartSplit(lines[i],pos,'part2')
READY = True
FOUND = False
if READY:
READY = False
FOUND = False
# print("ROW:",entries)
if re.match('[0-9]+',entries[0]): # valid line with solution data
self.Rows.append(entries)
self.hRows[entries[1]] = len(self.Rows)-1
else:
print("wrong line format ...")
print(entries)
sys.exit()
elif re.search('Column name',lines[i]):
lines[i] = lines[i].replace('Column name','Column_name')
lines[i] = lines[i].replace('Lower bound','Lower_bound')
lines[i] = lines[i].replace('Upper bound','Upper_bound')
entries = lines[i].split()
pos = 'COLS'
self.colHeaders = entries
else:
pass #print("NOTHING: ",lines[i])
i+= 1
# formatting of row width
self.colWidth = lines[i].split()
for k in range(len(self.colWidth)): self.colWidth[k] = len(self.colWidth[k])
# print("Col Widths:",self.colWidth)
i+= 1
READY = False
FOUND = False
while pos == 'COLS' and i<len(lines):
if re.match('^\s*[0-9]+',lines[i]): # new line
if len(lines[i].split())>2: # no linebreak
entries = self.smartSplit(lines[i],pos,'full')
READY = True
else: # linebreak
entries = self.smartSplit(lines[i],pos,'part1')
READY = False
FOUND = True
else:
if FOUND and not READY: # second part of line
entries += self.smartSplit(lines[i],pos,'part2')
READY = True
FOUND = False
if READY:
READY = False
FOUND = False
# print("COL:",entries)
if re.match('[0-9]+',entries[0]): # valid line with solution data
self.Cols.append(entries)
self.hCols[entries[1]] = len(self.Cols)-1
else:
print("wrong line format ...")
print(entries)
sys.exit()
elif re.search('Karush-Kuhn-Tucker',lines[i]):
pos = 'TAIL'
else:
pass #print("NOTHING: ",lines[i])
i+= 1
for i,e in enumerate(self.rowHeaders): self.rowIdx[e] = i
for i,e in enumerate(self.colHeaders): self.colIdx[e] = i
def getRow(self,name,attr):
if name in self.hRows:
if attr in self.rowIdx:
try:
val = float(self.Rows[self.hRows[name]][self.rowIdx[attr]])
except:
val = self.Rows[self.hRows[name]][self.rowIdx[attr]]
return val
else:
return -1
def getCol(self,name,attr):
if name in self.hCols:
if attr in self.colIdx:
try:
val = float(self.Cols[self.hCols[name]][self.colIdx[attr]])
except:
val = self.Cols[self.hCols[name]][self.colIdx[attr]]
return val
else:
print("key error:",name,"not known ...")
return -1
def __str__(self):
retString = '\n'+"="*80+'\nSOLUTION\n'
retString += "nRows: "+str(self.nRows)+'/'+str(len(self.Rows))+'\n'
retString += "nCols: "+str(self.nCols)+'/'+str(len(self.Cols))+'\n'
retString += "nNonZeros: "+str(self.nNonZeros)+'\n'
retString += "Status: "+str(self.Status)+'\n'
retString += "Objective: "+str(self.Objective)+'\n\n'
retString += ' '.join(self.rowHeaders)+'\n'
for r in self.Rows: retString += ' # '.join(r)+' #\n'
retString += '\n'
retString += ' '.join(self.colHeaders)+'\n'
for c in self.Cols: retString += ' # '.join(r)+' #\n'
return retString
I will be grateful for help with the above-stated error message from running my pyomo script file - "pyomo solve Katrina_Model5.py Katrina_paper.dat --solver=gurobi --summary --stream-solver --report-timing" at the command prompt. I am still new to the software. I have included full code and data file to aid with your help.
Below is the entire syntax of my problem below:
from pyomo.environ import *
#--define the mode:
model = AbstractModel()
#--declaring parameters:
model.n = Param(within=PositiveIntegers, doc='total no. of depots & afected areas')
model.L = Param(within=PositiveIntegers, doc='Max. no. of nodes a salesman may visit')
model.K = Param(initialize=2, within=PositiveIntegers, doc='Min. no. of nodes a salesman may visit')
#model.m = Param(within=PositiveIntegers, doc='no. of initial salesmen positioned at depot i &j')
#--declare model sets names:
model.I = RangeSet(model.n, name='Set of Origin/intermediary nodes')
model.J = RangeSet(model.n, name='Set of affected areas/destination nodes')
model.A = model.I*model.J
model.D = RangeSet(2, name='Set of depots comprises first d nodes of Set V')
model.U = RangeSet(3,5, name='Set of impacted areas/or customers')
model.V = model.D | model.U
#-- define additional parameters with indexed sets:
model.d = Param(model.I, model.J, doc='Represents cost/travel time matrix.')
#--define model Variables:
model.x = Var(model.I, model.J, within=Binary, name="Var of a salesman traveling.")
model.u = Var(within=RangeSet(2,5), name="no. of nodes visited on traveler's path from origin up to node i")
model.m =Var(model.D, name='no. of initial salesmen positioned at depot i &j')
"""#model's objective function defined.#"""
def objective_rule(model):
return sum(model.d[i,j]*model.x[i,j] for (i,j) in model.A)
model.objective = Objective(rule=objective_rule, sense=minimize, name="Total distance traveled")
"""--Below we define and declare the constraints of the model --"""
#.....constraint # 2
def constrTWO_rule(model, i):
return sum(model.x[i,j] for j in model.U) == m[i]
model.ConsOutTrvler = Constraint(model.D, rule=constrTWO_rule)
#.....constraint # 3
def constrTHREE_rule(model, j):
return sum(model.x[i,j] for i in model.U) == m[j]
model.ConsInTrvler = Constraint(model.D, rule=constrTHREE_rule)
#.....constraint # 4
def constrFOUR_rule(model, j):
return sum(model.x[i,j] for i in model.V) == 1
model.ConsTrvlerInn = Constraint(model.U, rule=constrFOUR_rule)
#.....constraint # 5
def constrFIVE_rule(model, i):
return sum(model.x[i,j] for j in model.V) == 1
model.ConsTrvlerOut = Constraint(model.U, rule=constrFIVE_rule)
#......constraint # 6
def constrSIX_rule(model, i):
return u[i] + (L-2)*sum(model.x[k,i]-model.x[i,k] for k in model.D)-L + 1 <= 0
model.consLowBounds = Constraint(model.U, rule=constrSIX_rule)
#.....constraint # 7
def constrSEVEN_rule(model, i):
return u[i] + sum(model.x[k,i] + (2-K)*model.x[i,k] for k in model.D) >= 2
model.consUpBounds = Constraint(model.U, rule=constrSEVEN_rule)
#.....constraint # 8 ---DOUBLE-CHECK FORMULATION
def constrEIGHT_rule(model, k, i):
return model.x[k,i] + model.x[i,k] <= 1
model.consNotOneAffArea = Constraint(model.D, model.U, rule=constrEIGHT_rule)
#.....constraint # 9 ---DOUBLE-CHECK FORMULATION
def constrNINE_rule(model, i, j):
return ( u[i] - u[j] + L*x[i,j] + (L-2)*x[i,j] ) <= L-1
model.consSubTourElim = Constraint(model.U, rule=constrNINE_rule)
And, here are the '.dat' data file used:
param n := 5 ;
param L := 5 ;
param K := 2 ;
param d: 1 2 3 4 5 :=
1 0 8 4 9 9
2 8 0 6 7 10
3 4 6 0 5 6
4 9 7 5 0 4
5 9 10 6 4 0 ;
I'm trying to plot two files of data of this type:
name1.fits 0 0 2.40359218172
name2.fits 0 0 2.15961244263
The third column has values from 0 to 5. I want to plot column 2 vs column 4, but, for lines with values in col 3 less than 2 (0 and 1), I want to shift col 2 by -0.1, and for lines with values greater than 3 (4 and 5) I want to shift col 2 by +0.1.
However my code seems to be shifting all values by +0.1. Here is what I have so far:
import matplotlib.pyplot as plt
import numpy as np
with open('file1.txt') as data, open('file2.txt') as stds:
lines1 = data.readlines()
lines2 = stds.readlines()
x1a = []
x2a = []
x1b = []
x2b = []
x1c = []
x2c = []
y1a = []
y2a = []
y1b = []
y2b = []
y1c = []
y2c = []
for line1 in lines1:
p = line1.split()
if p[2] < 2:
x1a.append(float(p[1]))
y1a.append(float(p[3]))
elif 1 < p[2] < 4:
x1b.append(float(p[1]))
y1b.append(float(p[3]))
elif p[2] > 3:
x1c.append(float(p[1]))
y1c.append(float(p[3]))
for line2 in lines2:
q = line2.split()
if q[2] < 2:
x2a.append(float(q[1]))
y2a.append(float(q[3]))
elif 1 < q[2] < 4:
x2b.append(float(q[1]))
y2b.append(float(q[3]))
elif q[2] > 3:
x2c.append(float(q[1]))
y2c.append(float(q[3]))
x1a = np.array(x1a)
x2a = np.array(x2a)
x1b = np.array(x1b)
x2b = np.array(x2b)
x1c = np.array(x1c)
x2c = np.array(x2c)
y1a = np.array(y1a)
y2a = np.array(y2a)
y1b = np.array(y1b)
y2b = np.array(y2b)
y1c = np.array(y1c)
y2c = np.array(y2c)
minorLocator = AutoMinorLocator(5)
fig, ax = plt.subplots(figsize=(8, 8))
fig.subplots_adjust(left=0.11, right=0.95, top=0.94)
plt.plot(x1a-0.1,y1a,'b^',mec='blue',label=r'B0',ms=8)
plt.plot(x2a-0.1,y2a,'r^',mec='red',fillstyle='none',mew=0.8,ms=8)
plt.plot(x1b,y1b,'bo',mec='blue',label=r'B0',ms=8)
plt.plot(x2b,y2b,'ro',mec='red',fillstyle='none',mew=0.8,ms=8)
plt.plot(x1c+0.1,y1c,'bx',mec='blue',label=r'B0',ms=8)
plt.plot(x2c+0.1,y2c,'rx',mec='red',fillstyle='none',mew=0.8,ms=8)
plt.axis([-1.0, 3.0, 0., 4])
ax.xaxis.set_tick_params(labeltop='on')
ax.yaxis.set_minor_locator(minorLocator)
plt.show()
Here is the plot:
plot
I'm pretty sure the problem is in my "ifs". I hope you can clear the way and/or show me a better option for this.
When you do your queries (if) you must ensure the conversion happens before the question so:
for line1 in lines1:
p = line1.split()
if p[2] < 2:
x1a.append(float(p[1]))
y1a.append(float(p[3]))
elif 1 < p[2] < 4:
x1b.append(float(p[1]))
y1b.append(float(p[3]))
elif p[2] > 3:
x1c.append(float(p[1]))
y1c.append(float(p[3]))
, should actually be:
for line1 in lines1:
p = line1.split()
if float(p[2]) < 2: # changed here
x1a.append(float(p[1]))
y1a.append(float(p[3]))
elif 1 < float(p[2]) < 4: # There seems to be a problem with this if
x1b.append(float(p[1]))
y1b.append(float(p[3]))
elif float(p[2]) > 3: # changed here
x1c.append(float(p[1]))
y1c.append(float(p[3]))
The same for your q variables. Also notice that asking 1 < x < 4 will intercept with x > 3 and x < 2. You should also correct this.
I am trying to update my function arguments after each iteration but failed to do so. Kindly check my code because I am new to python language. My task is to calculate xps, (represents collection of positions) and v2ps, (represents collection of velocities) after each iteration and want to plot them against each other. Basic this program represents the collision of objects moving vertical down and one of object also collide with plane above which they are moving.
acc_grav = 10
m1 =float(input(" Input mass of ball one, m1: "))
m2 =float(input(" Input mass of ball two, m2: "))
time_steps =10000
num_coll_bounce = 0
num_ball_coll = 0
eps=1.e-6
def ball_coll(x1_old,v1_old,x2_old,v2_old,time_ball_coll):
v1 = v1_old - acc_grav*time_ball_coll
v2 = v2_old - acc_grav*time_ball_coll
x1 = x1_old + time_ball_coll*v1_old - 0.5*acc_grav*(time_ball_coll)**2
x2 = x2_old + time_ball_coll*v2_old - 0.5*acc_grav*(time_ball_coll)**2
v1_ball_coll = (v1*(m1-m2)+(2*m2*v2))/(m1+m2)
v2_ball_coll = (v2*(m2-m1)+(2*m1*v1))/(m1+m2)
cumlv2=v2
return [v1,v2,x1,x2,v1_ball_coll,v2_ball_coll]
def floor_coll(x1_old,v1_old,x2_old,v2_old,time_floor_coll):
v1 = v1_old - acc_grav*time_floor_coll
v2 = v2_old - acc_grav*time_floor_coll
x1 = 0 #at the time of bonuce
x2 = x2_old + time_floor_coll*v2_old - 0.5*acc_grav*time_floor_coll**2
#update velocities following rules for collision with walls
v1_bounce = -v1
v2_bounce = v2
return [v1,v2,x1,x2,v1_bounce,v2_bounce]
for i in range(0, 10):
x1_0 = 1
x2_0 = 3 - (i-1)*0.1
v1_0 = 2
v2_0 = 2*v1_0
xps = []
v2ps = []
for n in range (time_steps-1):
time_ball_coll = (x2_0-x1_0)/(v1_0 - v2_0)
time_floor_coll = (v1_0 + (v1_0**2 + 2*acc_grav*x1_0)**1/2)/acc_grav
if ((time_ball_coll - time_floor_coll)<eps and v1_0 - v2_0 > 0):
num_coll_bounce = num_coll_bounce + 1
num_ball_coll = num_ball_coll + 1
ball_coll(x1_0,v1_0,x2_0,v2_0,time_ball_coll)
#xps[n] = x2_0
#v2ps(n,num_ballcoll) = v2ini
xps.append(x2_0)
v2ps.append(v2_0)
else:
num_coll_bounce = num_coll_bounce + 1
floor_coll(x1_0,v1_0,x2_0,v2_0,time_floor_coll)
#x1_old,v1_old,x2_old,v2_old,time_floor_coll = dd2
x_1.append(x1_0)
x_2.append(x2_0)