Can JModelica print results directly to file? - python

I am running the following JModelica script:
#!/usr/local/jmodelica/bin/jm_python.sh
import pyjmi
op = pyjmi.transfer_optimization_problem("BatchReactor", "model.mop")
opt_opts = op.optimize_options()
opt_opts['n_e'] = 40 # Number of elements
opt_opts['IPOPT_options']['tol'] = 1e-10
opt_opts['IPOPT_options']['print_level'] = 8
opt_opts['IPOPT_options']['output_file'] = '/z/out'
res = op.optimize(options=opt_opts)
I had hoped that the results (e.g. time, x1, x2, &c.) would be printed to the file /z/out. But the file only contains IPOPT verbose debugging/status info.
Is there a way to print the information that would be stored in res directly to a file? Either by somehow writing res itself or, preferably, having IPOPT/JModelica write the results without having to go through Python?

There is a way to print the information directly to a file. The following accomplishes this. Note that result_file_name is the key to making this happen.
#!/usr/local/jmodelica/bin/jm_python.sh
import pyjmi
op = pyjmi.transfer_optimization_problem("BatchReactor", "model.mop")
opt_opts = op.optimize_options()
opt_opts['n_e'] = 40 # Number of elements
opt_opts['result_file_name'] = '/z/out'
opt_opts['IPOPT_options']['tol'] = 1e-10
opt_opts['IPOPT_options']['print_level'] = 0
res = op.optimize(options=opt_opts)
Unfortunately, the contents of the file are somewhat mysterious.

You may find that using result_file_name per another answer here results in an output file which is difficult to understand.
The following produces a nicer format:
import StringIO
import numpy as np
def PrintResToFile(filename,result):
def StripMX(x):
return str(x).replace('MX(','').replace(')','')
varstr = '#Variable Name={name: <10}, Unit={unit: <7}, Val={val: <10}, Col={col:< 5}, Comment="{comment}"\n'
with open(filename,'w') as fout:
#Print all variables at the top of the file, along with relevant information
#about them.
for var in result.model.getAllVariables():
if not result.is_variable(var.getName()):
val = result.initial(var.getName())
col = -1
else:
val = "Varies"
col = result.get_column(var.getName())
unit = StripMX(var.getUnit())
if not unit:
unit = "X"
fout.write(varstr.format(
name = var.getName(),
unit = unit,
val = val,
col = col,
comment = StripMX(var.getAttribute('comment'))
))
#Ensure that time variable is printed
fout.write(varstr.format(
name = 'time',
unit = 's',
val = 'Varies',
col = 0,
comment = 'None'
))
#The data matrix contains only time-varying variables. So fetch all of
#these, couple them in tuples with their column number, sort by column
#number, and then extract the name of the variable again. This results in a
#list of variable names which are guaranteed to be in the same order as the
#data matrix.
vkeys_in_order = map(lambda x: x[1], sorted([(result.get_column(x),x) for x in result.keys() if result.is_variable(x)]))
for vk in vkeys_in_order:
fout.write("{0:>13},".format(vk))
fout.write("\n")
sio = StringIO.StringIO()
np.savetxt(sio, result.data_matrix, delimiter=',', fmt='%13.5f')
fout.write(sio.getvalue())
which looks like this:
#Variable Name=S0 , Unit=kg , Val=2.0 , Col=-1 , Comment="Solid Mass"
#Variable Name=F0 , Unit=kg , Val=0.0 , Col=-1 , Comment="Fluid Mass"
#Variable Name=a , Unit=Hz , Val=0.2 , Col=-1 , Comment="None"
#Variable Name=b , Unit=kg/s , Val=1.0 , Col=-1 , Comment="None"
#Variable Name=f , Unit=kg/s , Val=0.05 , Col=-1 , Comment="None"
#Variable Name=h , Unit=1/g , Val=0.05 , Col=-1 , Comment="None"
#Variable Name=der(F) , Unit=X , Val=Varies , Col= 1 , Comment="None"
#Variable Name=F , Unit=kg , Val=Varies , Col= 3 , Comment="None"
#Variable Name=der(S) , Unit=X , Val=Varies , Col= 2 , Comment="None"
#Variable Name=S , Unit=kg , Val=Varies , Col= 4 , Comment="None"
#Variable Name=u , Unit=X , Val=Varies , Col= 5 , Comment="None"
#Variable Name=startTime , Unit=X , Val=0.0 , Col=-1 , Comment="None"
#Variable Name=finalTime , Unit=X , Val=100.0 , Col=-1 , Comment="None"
#Variable Name=time , Unit=s , Val=Varies , Col= 0 , Comment="None"
time, der(F), der(S), F, S, u,
0.00000, 0.97097, -0.97097, 0.00000, 2.00000, 0.97097
0.38763, 1.07704, -1.05814, 0.38519, 1.61698, 1.00000
1.61237, 0.88350, -0.80485, 1.70714, 0.35885, 0.65862
2.50000, 0.00000, 0.09688, 2.14545, 0.00000, 0.00000
2.88763, 0.09842, -0.00000, 2.18330, 0.00000, 0.06851
4.11237, 0.10342, 0.00000, 2.30688, 0.00000, 0.07077
5.00000, 0.10716, 0.00000, 2.40033, 0.00000, 0.07240
5.38763, 0.10882, -0.00000, 2.44219, 0.00000, 0.07311
6.61237, 0.11421, 0.00000, 2.57875, 0.00000, 0.07535

Related

Modify the code to loop over another dataset

I am using haversine_distance function to calculate distance between coordinates in a dataset to a specific coordinate. [start_lat, start_lon = 40.6976637, -74.1197643]
def haversine_distance(lat1, lon1, lat2, lon2):
r = 6371
phi1 = np.radians(lat1)
phi2 = np.radians(lat2)
delta_phi = np.radians(lat2-lat1)
delta_lambda = np.radians(lon2-lon1)
a = np.sin(delta_phi / 2)**2 + np.cos(phi1) * np.cos(phi2) * np.sin(delta_lambda / 2)**2
res = r * (2 * np.arctan2(np.sqrt(a), np.sqrt(1-a)))
return np.round(res, 2)
start_lat, start_lon = 40.6976637, -74.1197643
distances_km = []
for row in pandas_df.itertuples(index=False):
distances_km.append(
haversine_distance(start_lat, start_lon, row.lat, row.lon)
)
pandas_df['Distance'] = distances_km
pandas_df
This successfully creates a column in my data set measuring the distance from given point like this:
Now I want to modify this code so that instead of using [start_lat, start_lon = 40.6976637, -74.1197643] I want to use another dataset containing cities.
How can I modify this existing code such that I create column for every city using its coordinates instead.
So desired output shows different columns with each city name and distance as calculated above.
Any Help is appreciated, new to python!
Cities array as requested in comments
[['Nanaimo' -123.9364 49.1642]
['Prince Rupert' -130.3271 54.3122]
['Vancouver' -123.1386 49.2636]
['Victoria' -123.3673 48.4275]
['Edmonton' -113.4909 53.5445]
['Winnipeg' -97.1392 49.8994]
['Sarnia' -82.4065 42.9746]
['Sarnia' -82.4065 42.9746]
['North York' -79.4112 43.7598]
['Kingston' -76.4812 44.2305]
['St. Catharines' -79.2333 43.1833]
['Thunder Bay' -89.2461 48.3822]
['Gaspé' -64.4833 48.8333]
['Cap-aux-Meules' -61.8607 47.3801]
['Kangiqsujuaq' -71.9667 61.6]
['Montreal' -73.5534 45.5091]
['Quebec City' -71.2074 46.8142]
['Rimouski' -68.524 48.4489]
['Sept-Îles' -66.3833 50.2167]
['Bathurst' -65.6497 47.6186]
['Charlottetown' -63.1399 46.24]
['Corner Brook' -57.9711 48.9411]
['Dartmouth' -63.5714 44.6715]
['Lewisporte' -55.0667 49.2333]
['Port Hawkesbury' -61.3642 45.6153]
['Saint John' -66.0628 45.2796]
["St. John's" -52.7072 47.5675]
['Sydney' -60.1947 46.1381]
['Yarmouth' -66.1175 43.8361]]
The beauty of Python is that you can use the same code to do different things.
To consider different [start_lat, start_lon] values for every column in your data, you can use the same code that you have now. All you need to do is to define start_lat and start_lon as arrays:
# --------------------- Array Initialization ---------------------
import pandas as pd
import numpy as np
np.random.seed(0)
pandas_df = pd.DataFrame(data = {'lat': np.random.rand(100),
'lon': np.random.rand(100)})
start_cities = pd.DataFrame([['Nanaimo' , -123.9364 , 49.1642], ['Prince Rupert' , -130.3271 , 54.3122],
['Vancouver' , -123.1386 , 49.2636], ['Victoria' , -123.3673 , 48.4275],
['Edmonton' , -113.4909 , 53.5445], ['Winnipeg' , -97.1392 , 49.8994],
['Sarnia' , -82.4065 , 42.9746], ['Sarnia' , -82.4065 , 42.9746],
['North York' , -79.4112 , 43.7598], ['Kingston' , -76.4812 , 44.2305],
['St. Catharines' , -79.2333 , 43.1833], ['Thunder Bay' , -89.2461 , 48.3822],
['Gaspé' , -64.4833 , 48.8333], ['Cap-aux-Meules' , -61.8607 , 47.3801],
['Kangiqsujuaq' , -71.9667 , 61.6 ], ['Montreal' , -73.5534 , 45.5091],
['Quebec City' , -71.2074 , 46.8142], ['Rimouski' , -68.524 , 48.4489],
['Sept-Îles' , -66.3833 , 50.2167], ['Bathurst' , -65.6497 , 47.6186],
['Charlottetown' , -63.1399 , 46.24 ], ['Corner Brook' , -57.9711 , 48.9411],
['Dartmouth' , -63.5714 , 44.6715], ['Lewisporte' , -55.0667 , 49.2333],
['Port Hawkesbury' , -61.3642 , 45.6153], ['Saint John' , -66.0628 , 45.2796],
["St. John's" , -52.7072 , 47.5675], ['Sydney' , -60.1947 , 46.1381],
['Yarmouth' , -66.1175 , 43.8361]])
start_cities.columns = 'names', 'start_lat', 'start_lon'
start_lat = start_cities.start_lat
start_lon = start_cities.start_lon
# --------------------- Same code as before (as promised) ---------------------
def haversine_distance(lat1, lon1, lat2, lon2):
r = 6371
phi1 = np.radians(lat1)
phi2 = np.radians(lat2)
delta_phi = np.radians(lat2-lat1)
delta_lambda = np.radians(lon2-lon1)
a = np.sin(delta_phi / 2)**2 + np.cos(phi1) * np.cos(phi2) * np.sin(delta_lambda / 2)**2
res = r * (2 * np.arctan2(np.sqrt(a), np.sqrt(1-a)))
return np.round(res, 2)
distances_km = []
for row in pandas_df.itertuples(index=False):
distances_km.append(
haversine_distance(start_lat, start_lon, row.lat, row.lon))
# --------------------- Store data ---------------------
distances_km = np.array(distances_km)
for ind, name in enumerate(start_cities.names):
pandas_df['distance_km_' + name] = distances_km[:,ind]
# print(pandas_df.keys())
# ["lat" , "lon" ,
# "distance_km_Nanaimo" , "distance_km_Prince Rupert" ,
# "distance_km_Vancouver" , "distance_km_Victoria" ,
# "distance_km_Edmonton" , "distance_km_Winnipeg" ,
# "distance_km_Sarnia" , "distance_km_North York" ,
# "distance_km_Kingston" , "distance_km_St. Catharines" ,
# "distance_km_Thunder Bay" , "distance_km_Gaspé" ,
# "distance_km_Cap-aux-Meules" , "distance_km_Kangiqsujuaq" ,
# "distance_km_Montreal" , "distance_km_Quebec City" ,
# "distance_km_Rimouski" , "distance_km_Sept-Îles" ,
# "distance_km_Bathurst" , "distance_km_Charlottetown" ,
# "distance_km_Corner Brook" , "distance_km_Dartmouth" ,
# "distance_km_Lewisporte" , "distance_km_Port Hawkesbury",
# "distance_km_Saint John" , "distance_km_St. John's" ,
# "distance_km_Sydney" , "distance_km_Yarmouth" ]

python trapz numerical integration giving a negative value

I am trying to integrate a numpy array which looks something like this:
I tried doing np.trapz(y=f, x=x) but end up getting a negative value of -0.000802483, which was unexpected as f is never negative over the given x interval. I expect it to be a positive value. Am I doing something wrong here and does using np.trapz make sense here? I am quite inexperienced with numerical methods in python, any help would be greatly appreciated, thanks!
f array:
[8.50448832e-06, 1.05913683e-05, 1.11672674e-05, 1.03133123e-05,
8.55576374e-06, 6.51874371e-06, 4.66984459e-06, 3.23400327e-06,
2.23952788e-06, 1.61397514e-06, 1.26551646e-06, 1.11602722e-06,
1.09337264e-06, 1.16013046e-06, 1.29750464e-06, 1.48914243e-06,
1.71015364e-06, 1.92463843e-06, 2.11557374e-06, 2.27914844e-06,
2.41413389e-06, 2.52152682e-06, 2.60410241e-06, 2.66593017e-06,
2.71192544e-06, 2.74748305e-06, 2.77821789e-06, 2.80982042e-06,
2.84802547e-06, 2.89868017e-06, 2.96550880e-06, 3.04685482e-06,
3.14027584e-06, 3.24321794e-06, 3.35292462e-06, 3.46637434e-06,
3.58025198e-06, 3.69202940e-06, 3.80158930e-06, 3.90921139e-06,
4.01521443e-06, 4.11995070e-06, 4.22380119e-06, 4.32717165e-06,
4.43048947e-06, 4.53420149e-06, 4.63876310e-06, 4.74441156e-06,
4.85115094e-06, 4.95898043e-06, 5.06790572e-06, 5.17793927e-06,
5.28910054e-06, 5.40141637e-06, 5.51492115e-06, 5.62965722e-06,
5.74566570e-06, 5.86294288e-06, 5.98146531e-06, 6.10120449e-06,
6.22212681e-06, 6.34419345e-06, 6.46736032e-06, 6.59157794e-06,
6.71679139e-06, 6.84294018e-06, 6.96995819e-06, 7.09777352e-06,
7.22630845e-06, 7.35548870e-06, 7.48530158e-06, 7.61576302e-06,
7.74689094e-06, 7.87870523e-06, 8.01122775e-06, 8.14448236e-06,
8.27849500e-06, 8.41329367e-06, 8.54890851e-06, 8.68537178e-06,
8.82271792e-06, 8.96098359e-06, 9.10020767e-06, 9.24043128e-06,
9.38169220e-06, 9.52399166e-06, 9.66731438e-06, 9.81164377e-06,
9.95696193e-06, 1.01032497e-05, 1.02504864e-05, 1.03986503e-05,
1.05477182e-05, 1.06976654e-05, 1.08484660e-05, 1.10000927e-05,
1.11525166e-05, 1.13057075e-05, 1.14596338e-05, 1.16142624e-05,
1.17695588e-05, 1.19254869e-05, 1.20820259e-05, 1.22391888e-05,
1.23969940e-05, 1.25554608e-05, 1.27146099e-05, 1.28744630e-05,
1.30350432e-05, 1.31963744e-05, 1.33584820e-05, 1.35213926e-05,
1.36851339e-05, 1.38497349e-05, 1.40152258e-05, 1.41816381e-05,
1.43490045e-05, 1.45173591e-05, 1.46867371e-05, 1.48571751e-05,
1.50287105e-05, 1.52013618e-05, 1.53751190e-05, 1.55499688e-05,
1.57258974e-05, 1.59028901e-05, 1.60809314e-05, 1.62600049e-05,
1.64400935e-05, 1.66211790e-05, 1.68032427e-05, 1.69862648e-05,
1.71702248e-05, 1.73551011e-05, 1.75408716e-05, 1.77275129e-05,
1.79150010e-05, 1.81033110e-05, 1.82924169e-05, 1.84822921e-05,
1.86729088e-05, 1.88642383e-05, 1.90562544e-05, 1.92489505e-05,
1.94423280e-05, 1.96363887e-05, 1.98311346e-05, 2.00265678e-05,
2.02226907e-05, 2.04195058e-05, 2.06170156e-05, 2.08152233e-05,
2.10141318e-05, 2.12137445e-05, 2.14140649e-05, 2.16150966e-05,
2.18168436e-05, 2.20193100e-05, 2.22225000e-05, 2.24264183e-05,
2.26310696e-05, 2.28364587e-05, 2.30425910e-05, 2.32494716e-05,
2.34571064e-05, 2.36655022e-05, 2.38746665e-05, 2.40846075e-05,
2.42953332e-05, 2.45068523e-05, 2.47191735e-05, 2.49323059e-05,
2.51462588e-05, 2.53610420e-05, 2.55766653e-05, 2.57931389e-05,
2.60104733e-05, 2.62286795e-05, 2.64477684e-05, 2.66677514e-05,
2.68886403e-05, 2.71104471e-05, 2.73331840e-05, 2.75568637e-05,
2.77814991e-05, 2.80071034e-05, 2.82336902e-05, 2.84612731e-05,
2.86898648e-05, 2.89194782e-05, 2.91501262e-05, 2.93818220e-05,
2.96145791e-05, 2.98484114e-05, 3.00833329e-05, 3.03193579e-05,
3.05565011e-05, 3.07947774e-05, 3.10342019e-05, 3.12747902e-05,
3.15165580e-05, 3.17595214e-05, 3.20036966e-05, 3.22491003e-05,
3.24957494e-05, 3.27436611e-05, 3.29928530e-05, 3.32433427e-05,
3.34951484e-05, 3.37482885e-05, 3.40027817e-05, 3.42586461e-05,
3.45158800e-05, 3.47744606e-05, 3.50343633e-05, 3.52955627e-05,
3.55580326e-05, 3.58217460e-05, 3.60866750e-05, 3.63527911e-05,
3.66200649e-05, 3.68884659e-05, 3.71579632e-05, 3.74285249e-05,
3.77001182e-05, 3.79727094e-05, 3.82462641e-05, 3.85207471e-05,
3.87961220e-05, 3.90723520e-05, 3.93493990e-05, 3.96272243e-05,
3.99057882e-05, 4.01850501e-05, 4.04649684e-05, 4.07455009e-05,
4.10266042e-05, 4.13082348e-05, 4.15903804e-05, 4.18730703e-05,
4.21563372e-05, 4.24402148e-05, 4.27247376e-05, 4.30099411e-05,
4.32958616e-05, 4.35825364e-05, 4.38700036e-05, 4.41583023e-05,
4.44474726e-05, 4.47375553e-05, 4.50285924e-05, 4.53206267e-05,
4.56137020e-05, 4.59078630e-05, 4.62031555e-05, 4.64996261e-05,
4.67973225e-05, 4.70962935e-05, 4.73965885e-05, 4.76982583e-05,
4.80013546e-05, 4.83059299e-05, 4.86120380e-05, 4.89197251e-05,
4.92289986e-05, 4.95398538e-05, 4.98522856e-05, 5.01662890e-05,
5.04818583e-05, 5.07989878e-05, 5.11176714e-05, 5.14379028e-05,
5.17596752e-05, 5.20829817e-05, 5.24078151e-05, 5.27341678e-05,
5.30620321e-05, 5.33913998e-05, 5.37222624e-05, 5.40546112e-05,
5.43884373e-05, 5.47237312e-05, 5.50604834e-05, 5.53986839e-05,
5.57383224e-05, 5.60793884e-05, 5.64218710e-05, 5.67657590e-05,
5.71110410e-05, 5.74577047e-05, 5.78057281e-05, 5.81550770e-05,
5.85057158e-05, 5.88576079e-05, 5.92107163e-05, 5.95650029e-05,
5.99204289e-05, 6.02769547e-05, 6.06345400e-05, 6.09931435e-05,
6.13527232e-05, 6.17132363e-05, 6.20746392e-05, 6.24368873e-05,
6.27999353e-05, 6.31637371e-05, 6.35282455e-05, 6.38934127e-05,
6.42591899e-05, 6.46255276e-05, 6.49923751e-05, 6.53596810e-05,
6.57273931e-05, 6.60954580e-05, 6.64638218e-05, 6.68324292e-05,
6.72012496e-05, 6.75703534e-05, 6.79398379e-05, 6.83098025e-05,
6.86803487e-05, 6.90515798e-05, 6.94236014e-05, 6.97965210e-05,
7.01704482e-05, 7.05454949e-05, 7.09217748e-05, 7.12994040e-05,
7.16785006e-05, 7.20591849e-05, 7.24415794e-05, 7.28258086e-05,
7.32119995e-05, 7.36002812e-05, 7.39907848e-05, 7.43836438e-05,
7.47789940e-05, 7.51769734e-05, 7.55777221e-05, 7.59813826e-05,
7.63880996e-05, 7.67980127e-05, 7.72111298e-05, 7.76273428e-05,
7.80465374e-05, 7.84685970e-05, 7.88934026e-05, 7.93208325e-05,
7.97507629e-05, 8.01830673e-05, 8.06176169e-05, 8.10542803e-05,
8.14929237e-05, 8.19334107e-05, 8.23756026e-05, 8.28193580e-05,
8.32645329e-05, 8.37109809e-05, 8.41585530e-05, 8.46070975e-05,
8.50564601e-05, 8.55064839e-05, 8.59570094e-05, 8.64078743e-05,
8.68589136e-05, 8.73099596e-05, 8.77608418e-05, 8.82113882e-05,
8.86615329e-05, 8.91113973e-05, 8.95611241e-05, 9.00108582e-05,
9.04607475e-05, 9.09109426e-05, 9.13615966e-05, 9.18128656e-05,
9.22649084e-05, 9.27178865e-05, 9.31719644e-05, 9.36273094e-05,
9.40840916e-05, 9.45424841e-05, 9.50026628e-05, 9.54648067e-05,
9.59290976e-05, 9.63957202e-05, 9.68648626e-05, 9.73367153e-05,
9.78114722e-05, 9.82893303e-05, 9.87704892e-05, 9.92551521e-05,
9.97435237e-05, 1.00235716e-04, 1.00731671e-04, 1.01231313e-04,
1.01734564e-04, 1.02241344e-04, 1.02751571e-04, 1.03265163e-04,
1.03782036e-04, 1.04302101e-04, 1.04825273e-04, 1.05351460e-04,
1.05880572e-04, 1.06412515e-04, 1.06947194e-04, 1.07484514e-04,
1.08024375e-04, 1.08566678e-04, 1.09111320e-04, 1.09658199e-04,
1.10207209e-04, 1.10758243e-04, 1.11311192e-04, 1.11865946e-04,
1.12422391e-04, 1.12980414e-04, 1.13539922e-04, 1.14100904e-04,
1.14663365e-04, 1.15227312e-04, 1.15792750e-04, 1.16359685e-04,
1.16928123e-04, 1.17498070e-04, 1.18069533e-04, 1.18642517e-04,
1.19217029e-04, 1.19793076e-04, 1.20370663e-04, 1.20949798e-04,
1.21530488e-04, 1.22112738e-04, 1.22696556e-04, 1.23281949e-04,
1.23868924e-04, 1.24457488e-04, 1.25047648e-04, 1.25639411e-04,
1.26232785e-04, 1.26827778e-04, 1.27424399e-04, 1.28022682e-04,
1.28622678e-04, 1.29224438e-04, 1.29828015e-04, 1.30433462e-04,
1.31040833e-04, 1.31650183e-04, 1.32261567e-04, 1.32875041e-04,
1.33490664e-04, 1.34108492e-04, 1.34728584e-04, 1.35351001e-04,
1.35975801e-04, 1.36603048e-04, 1.37232802e-04, 1.37865127e-04,
1.38500087e-04, 1.39137746e-04, 1.39778169e-04, 1.40421423e-04,
1.41067575e-04, 1.41716694e-04, 1.42368825e-04, 1.43023960e-04,
1.43682077e-04, 1.44343158e-04, 1.45007183e-04, 1.45674130e-04,
1.46343979e-04, 1.47016707e-04, 1.47692293e-04, 1.48370715e-04,
1.49051948e-04, 1.49735970e-04, 1.50422756e-04, 1.51112282e-04,
1.51804523e-04, 1.52499454e-04, 1.53197049e-04, 1.53897280e-04,
1.54600122e-04, 1.55305547e-04, 1.56013526e-04, 1.56724032e-04,
1.57437035e-04, 1.58152529e-04, 1.58870541e-04, 1.59591101e-04,
1.60314238e-04, 1.61039982e-04, 1.61768365e-04, 1.62499418e-04,
1.63233172e-04, 1.63969659e-04, 1.64708911e-04, 1.65450960e-04,
1.66195841e-04, 1.66943585e-04, 1.67694227e-04, 1.68447801e-04,
1.69204341e-04, 1.69963883e-04, 1.70726461e-04, 1.71492111e-04,
1.72260869e-04, 1.73032773e-04, 1.73807858e-04, 1.74586144e-04,
1.75367629e-04, 1.76152305e-04, 1.76940166e-04, 1.77731206e-04,
1.78525418e-04, 1.79322794e-04, 1.80123328e-04, 1.80927011e-04,
1.81733837e-04, 1.82543796e-04, 1.83356882e-04, 1.84173084e-04,
1.84992395e-04, 1.85814805e-04, 1.86640305e-04, 1.87468886e-04,
1.88300539e-04, 1.89135252e-04, 1.89973016e-04, 1.90813821e-04,
1.91657686e-04, 1.92504708e-04, 1.93354998e-04, 1.94208665e-04,
1.95065822e-04, 1.95926585e-04, 1.96791067e-04, 1.97659386e-04,
1.98531660e-04, 1.99408011e-04, 2.00288559e-04, 2.01173427e-04,
2.02062741e-04, 2.02956628e-04, 2.03855214e-04, 2.04758630e-04,
2.05667007e-04, 2.06580478e-04, 2.07499178e-04, 2.08423242e-04,
2.09352789e-04, 2.10287784e-04, 2.11228127e-04, 2.12173718e-04,
2.13124451e-04, 2.14080222e-04, 2.15040923e-04, 2.16006445e-04,
2.16976678e-04, 2.17951508e-04, 2.18930822e-04, 2.19914503e-04,
2.20902434e-04, 2.21894493e-04, 2.22890560e-04, 2.23890511e-04,
2.24894220e-04]
x array:
[192.68860048, 192.18172021, 191.72558157, 191.31429202,
190.94296221, 190.60745222, 190.30419615, 190.03007711,
189.78233599, 189.55850331, 189.35634746, 189.17383451,
189.00909674, 188.86040749, 188.72616092, 188.60485555,
188.49508064, 188.39550495, 188.30486735, 188.22196886,
188.14566599, 188.07486501, 188.00851713, 187.94561432,
187.88518575, 187.82629479, 187.7680363 , 187.70953449,
187.64994097, 187.58843501, 187.52471988, 187.4596708 ,
187.39430761, 187.3296264 , 187.26660038, 187.20618083,
187.14929649, 187.09642682, 187.04703778, 187.00046645,
186.95606702, 186.91320987, 186.87128073, 186.82967999,
186.78782204, 186.74513475, 186.70107768, 186.6555782 ,
186.6090346 , 186.56185881, 186.51445461, 186.46721785,
186.4205366 , 186.37479146, 186.33035568, 186.28759548,
186.2468023 , 186.20790552, 186.17071973, 186.13506353,
186.10075942, 186.06763367, 186.03551617, 186.00424032,
185.97364285, 185.94356377, 185.91384622, 185.8843364 ,
185.85488347, 185.82535666, 185.79574633, 185.7660932 ,
185.73643756, 185.70681915, 185.67727713, 185.64785007,
185.61857602, 185.58949246, 185.56063631, 185.53204398,
185.50375134, 185.47579373, 185.44820599, 185.42102245,
185.3942708 , 185.36793712, 185.34199086, 185.316402 ,
185.29114105, 185.26617909, 185.24148772, 185.21703906,
185.19280573, 185.16876086, 185.14487802, 185.12113129,
185.09749519, 185.0739447 , 185.05045522, 185.02700259,
185.00356308, 184.98011344, 184.95664239, 184.93316263,
184.90968964, 184.88623867, 184.86282477, 184.83946279,
184.81616738, 184.79295299, 184.76983385, 184.74682402,
184.72393734, 184.7011875 , 184.67858795, 184.65615197,
184.63389268, 184.61182296, 184.58995557, 184.56830304,
184.54687752, 184.52568079, 184.50470008, 184.48392178,
184.46333244, 184.44291885, 184.422668 , 184.40256707,
184.38260346, 184.36276475, 184.3430387 , 184.32341329,
184.30387666, 184.28441713, 184.26502323, 184.24568362,
184.22638719, 184.20712296, 184.18788013, 184.16864807,
184.14941633, 184.13017459, 184.11091392, 184.09163324,
184.07233455, 184.05301986, 184.03369114, 184.01435034,
183.99499939, 183.97564022, 183.95627473, 183.9369048 ,
183.91753228, 183.89815902, 183.87878683, 183.85941754,
183.84005291, 183.82069471, 183.80134469, 183.78200458,
183.76267609, 183.74336089, 183.72406066, 183.70477706,
183.68551173, 183.66626656, 183.64704358, 183.6278448 ,
183.60867219, 183.58952772, 183.57041331, 183.55133087,
183.53228228, 183.5132694 , 183.49429406, 183.47535805,
183.45646317, 183.43761116, 183.41880377, 183.40004269,
183.38132962, 183.3626662 , 183.34405408, 183.32549485,
183.30699012, 183.28854144, 183.27015034, 183.25181831,
183.23354652, 183.21533608, 183.19718805, 183.1791035 ,
183.16108346, 183.14312895, 183.12524098, 183.10742052,
183.08966855, 183.07198601, 183.05437384, 183.03683294,
183.01936422, 183.00196856, 182.98464682, 182.96739984,
182.95022844, 182.93313345, 182.91611565, 182.89917582,
182.88231471, 182.86553307, 182.84883163, 182.83221094,
182.81566809, 182.79919663, 182.78279 , 182.76644177,
182.75014557, 182.73389514, 182.71768429, 182.70150693,
182.68535706, 182.66922874, 182.65311615, 182.63701353,
182.62091521, 182.60481561, 182.58870922, 182.57259062,
182.55645448, 182.54029553, 182.52410861, 182.50788861,
182.49163052, 182.47532939, 182.45898038, 182.4425787 ,
182.42611965, 182.40959872, 182.39301574, 182.3763761 ,
182.35968543, 182.34294934, 182.32617332, 182.3093628 ,
182.29252312, 182.27565956, 182.25877731, 182.24188148,
182.22497712, 182.20806919, 182.19116258, 182.1742621 ,
182.15737249, 182.14049841, 182.12364445, 182.10681513,
182.09001489, 182.0732481 , 182.05651905, 182.03983197,
182.02319101, 182.00660025, 181.99006371, 181.97358443,
181.95716127, 181.94079185, 181.92447382, 181.90820486,
181.8919827 , 181.87580506, 181.85966973, 181.84357449,
181.82751719, 181.81149566, 181.7955078 , 181.77955152,
181.76362476, 181.74772547, 181.73185167, 181.71600135,
181.70017259, 181.68436344, 181.66857201, 181.65279642,
181.63703484, 181.62128543, 181.60554641, 181.589816 ,
181.57409247, 181.55837406, 181.54265828, 181.52694172,
181.51122096, 181.49549266, 181.47975351, 181.46400028,
181.44822976, 181.43243883, 181.4166244 , 181.40078344,
181.38491298, 181.36901011, 181.35307194, 181.33709567,
181.32107853, 181.30501782, 181.28891088, 181.2727551 ,
181.25654793, 181.24028688, 181.22396949, 181.20759338,
181.19115619, 181.17465564, 181.15808949, 181.14145555,
181.12475331, 181.10798876, 181.09116943, 181.07430268,
181.05739579, 181.04045588, 181.02348997, 181.00650495,
180.98950756, 180.97250445, 180.95550213, 180.93850699,
180.92152529, 180.90456319, 180.8876267 , 180.87072172,
180.85385404, 180.8370293 , 180.82025306, 180.80353074,
180.78686763, 180.77026891, 180.75373966, 180.73728483,
180.72090924, 180.70461725, 180.68840633, 180.67226823,
180.65619464, 180.6401774 , 180.62420847, 180.60827997,
180.59238417, 180.57651346, 180.56066036, 180.54481757,
180.52897788, 180.51313424, 180.49727975, 180.48140762,
180.4655112 , 180.449584 , 180.43361963, 180.41761187,
180.4015546 , 180.38544186, 180.36926781, 180.35302676,
180.33671313, 180.32032151, 180.30384658, 180.28728325,
180.27063081, 180.25389595, 180.23708594, 180.22020794,
180.20326895, 180.18627582, 180.16923529, 180.15215395,
180.13503824, 180.11789447, 180.10072882, 180.08354732,
180.06635587, 180.04916023, 180.03196601, 180.01477872,
179.99760368, 179.98044613, 179.96331113, 179.94620363,
179.92912843, 179.91209022, 179.89509352, 179.87814276,
179.86124217, 179.84439292, 179.82759098, 179.81083186,
179.79411112, 179.77742445, 179.76076761, 179.74413641,
179.7275268 , 179.71093475, 179.69435636, 179.67778779,
179.66122528, 179.64466516, 179.62810383, 179.61153778,
179.59496356, 179.57837783, 179.5617773 , 179.54515878,
179.52851914, 179.51185535, 179.49516445, 179.47844355,
179.46168984, 179.44490059, 179.42807373, 179.41120912,
179.39430705, 179.3773678 , 179.36039166, 179.34337889,
179.32632976, 179.30924454, 179.2921235 , 179.27496689,
179.25777497, 179.24054799, 179.2232862 , 179.20598984,
179.18865916, 179.17129439, 179.15389577, 179.13646353,
179.11899788, 179.10149907, 179.08396729, 179.06640278,
179.04880574, 179.03117638, 179.01351494, 178.99582209,
178.97809874, 178.96034578, 178.9425641 , 178.92475451,
178.90691783, 178.88905484, 178.87116628, 178.85325289,
178.83531534, 178.81735431, 178.79937043, 178.78136431,
178.76333651, 178.7452876 , 178.72721808, 178.70912845,
178.69101917, 178.67289068, 178.65474338, 178.63657764,
178.61839382, 178.60019223, 178.58197287, 178.56373501,
178.5454778 , 178.52720042, 178.50890207, 178.49058198,
178.47223937, 178.4538735 , 178.43548366, 178.41706912,
178.39862922, 178.38016327, 178.36167064, 178.34315068,
178.3246028 , 178.30602639, 178.28742088, 178.26878573,
178.25012039, 178.23142435, 178.21269711, 178.19393819,
178.17514713, 178.15632369, 178.13746795, 178.11857998,
178.09965986, 178.08070765, 178.06172339, 178.04270713,
178.0236589 , 178.00457871, 177.98546659, 177.96632251,
177.94714649, 177.92793849, 177.90869848, 177.88942642,
177.87012227, 177.85078595, 177.83141739, 177.81201652,
177.79258324, 177.77311744, 177.75361901, 177.73408774,
177.71452325, 177.69492518, 177.67529317, 177.65562687,
177.63592595, 177.61619007, 177.59641892, 177.57661218,
177.55676955, 177.53689073, 177.51697545, 177.49702343,
177.4770344 , 177.45700811, 177.4369443 , 177.41684275,
177.39670321, 177.37652548, 177.35630934, 177.33605459,
177.31576113, 177.2954291 , 177.27505864, 177.25464985,
177.23420277, 177.21371743, 177.19319379, 177.1726318 ,
177.15203134, 177.13139227, 177.1107144 , 177.08999751,
177.06924133, 177.04844555, 177.02760982, 177.00673375,
176.98581692, 176.96485885, 176.94385905, 176.92281695,
176.90173195, 176.88060326, 176.85943008, 176.83821163,
176.81694721, 176.79563615, 176.77427785, 176.75287176,
176.73141736, 176.70991421, 176.68836189, 176.66676006,
176.64510842, 176.62340672, 176.60165476, 176.57985239,
176.55799952]
The trapezoidal method sums over
0.5*(y[k]+y[k+1])*(x[k+1]-x[k])
As for your given arrays the middle factor is always positive and the last factor always negative, the sum over it will also be negative.
This is also mathematically correct, an integral with a falling integration variable has the negative value of the same integral with the interval ends switched

Differential evolution in SciPy

I am trying to use differential_evolution from SciPy. I have three matrices: x, y and P - all of size (14,6). I have to use the following formula:
z= np.log10(g)+ np.log10(c)*np.log10(P)
to find the value of c (real number from 0 to 2) which minimize:
numpy.median(z**2)
this expression. What I try is this (I provide the data for the convenience):
import numpy as np
from scipy.optimize import differential_evolution
def func(c, args):
z = args[0] + np.log10(c)*np.log10(args[1])
return np.median(z**2)
if __name__ == '__main__':
bounds = [(0, 2)]
x = np.array([[126581.94951205, 97601.85624482, 59659.00330833,
27646.48551627, 9202.50377458, 4840.25789068],
[213571.84886437, 148750.52154776, 85979.81139937,
38757.37831212, 11775.99906427, 4619.32027948],
[195684.50299021, 131818.78542437, 74376.55189913,
32793.21715377, 10288.70838873, 4042.58093119],
[177598.13865746, 120942.50439911, 68866.09898276,
30819.5354775 , 10588.08746517, 5011.71808947],
[126433.18311483, 85863.57788065, 48923.64502157,
21828.60950911, 7907.37639781, 4410.61819399],
[103431.88029629, 67452.94418262, 37608.36861047,
16456.97701443, 6027.98704858, 3550.06927169],
[100689.06813945, 64380.21348052, 34764.02910376,
14849.85472635, 5607.19256065, 3605.5709208 ],
[ 96509.22946744, 63832.74512518, 36041.69174706,
15802.87650901, 6473.33232805, 4664.07058733],
[113078.63455882, 73227.02362359, 40861.09037499,
17385.89127848, 7074.98444924, 5136.84232454],
[121241.93118924, 78537.13681709, 44257.97654994,
18584.94999742, 7733.39219718, 5869.49536788],
[115948.06368262, 73995.07204278, 41536.21315507,
16851.59724901, 6736.25125909, 4851.5738275 ],
[115024.20359423, 72108.15245783, 40341.98473413,
15900.55422399, 6243.63777265, 4411.24859372],
[108754.83802899, 66210.25952459, 36485.42905112,
14577.73925124, 5553.23702141, 3736.5217322 ],
[ 95340.59125024, 58458.97552915, 32364.19705748,
13236.30114676, 4929.04023171, 3202.21731277]])
y = y=np.array([[118166.08 , 95784.692 , 68134.878 , 37119.959 , 17924.157 ,
7445.3083],
[ 99265.027 , 70679.135 , 43297.559 , 19822.017 , 8527.8497,
3404.7113],
[ 80227.797 , 50972.879 , 26648.604 , 11190.488 , 4836.6514,
2249.9172],
[ 68510.582 , 39288.19 , 19938.938 , 9312.6881, 4907.6661,
2681.2709],
[ 65193.15 , 36610.107 , 18612.181 , 9211.144 , 5416.1685,
3372.1282],
[ 67188.918 , 37227.699 , 20132.92 , 11663.275 , 7315.3472,
4648.1669],
[ 64802.06 , 38885.622 , 22008.537 , 13100.638 , 8043.0185,
5049.2097],
[ 68104.867 , 41212.89 , 23247.898 , 14134.707 , 8805.2547,
5526.1014],
[ 74180.595 , 41268.904 , 22868.016 , 13841.437 , 8660.1413,
5401.245 ],
[ 78920.685 , 42743.389 , 23932.305 , 13910.089 , 8439.3342,
5141.7051],
[ 91329.012 , 45733.772 , 25430.818 , 14144.185 , 8273.7953,
5016.5839],
[ 92217.594 , 44984.3 , 23353.596 , 13467.631 , 8099.728 ,
4948.26 ],
[ 94508.441 , 48114.879 , 24735.311 , 13358.097 , 7821.8587,
4806.7923],
[108211.73 , 53987.095 , 25872.772 , 13189.61 , 7552.7164,
4497.2611]])
P=10000*np.array([[0.6011,0.6011,0.6011,0.6011,0.6011,0.6011],
[0.9007,0.9007,0.9007,0.9007,0.9007,0.9007],
[1.1968,1.1968,1.1968,1.1968,1.1968,1.1968],
[1.4178,1.4178,1.4178,1.4178,1.4178,1.4178],
[1.5015,1.5015,1.5015,1.5015,1.5015,1.5015],
[1.439,1.439,1.439,1.439,1.439,1.439],
[1.2721,1.2721,1.2721,1.2721,1.2721,1.2721],
[1.0616,1.0616,1.0616,1.0616,1.0616,1.0616],
[0.8543,0.8543,0.8543,0.8543,0.8543,0.8543],
[0.6723,0.6723,0.6723,0.6723,0.6723,0.6723],
[0.5204,0.5204,0.5204,0.5204,0.5204,0.5204],
[0.3963,0.3963,0.3963,0.3963,0.3963,0.3963],
[0.2990,0.2990,0.2990,0.2990,0.2990,0.2990],
[0.2211,0.2211,0.2211,0.2211,0.2211,0.2211]])
g=np.log10(y) - np.log10(x)
args = (g,P)
result = differential_evolution(func, bounds, args=args)
print(func(bounds, args))
I get this error: TypeError: func() takes exactly 2 arguments (3 given) . Is there any way to fix this?
def func(c, g, P):
z = g + np.log10(c)*np.log10(P)
return np.median(z**2)
if __name__ == '__main__':
# Your arrays go here
g = np.log10(y) - np.log10(x)
args = (g, P)
result = differential_evolution(func, bounds, args=(g, P))
# will print the value of c and value of the optimized function
print (result.x, result.fun)

'int' object is not subscriptable (function calling error)

I'm attempting to break a vigenere cipher without knowing the key and I'm struggling to figure out my error since the caesar_break function seems to work perfectly fine on its own.
This is the code for the function i'm attempting to call:
def caesar_break( cipher, frequencies ): # help!
alpha = ["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" ]
dist_ls = []
for key in alpha:
poss_plaintxt = caesar_dec( cipher, key )
counts = letter_counts( poss_plaintxt )
observed = normalize( counts )
dist = distance( observed, frequencies )
dist_ls.append( (dist , poss_plaintxt , key) )
can = dist_ls[ 0 ][ 0 ]
can_t = 0
for t in dist_ls:
if t[ 0 ] < can:
can = t[ 0 ]
can_t = t
return [ can_t[ 2 ], can_t[ 1 ] ]
This is what I have so far for my current function, I am not completely done with it, but I just need to figure out this error in order to move forward:
def vig_break( c, maxlen, frequencies ):
from i in range( 1, maxlen ):
break_ls = list(vig_break_for_length( c, i, frequencies ))
print( break_ls ) #this print is unneeded, i just like to test my code as i go
For specificity this is the error code:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "ciphers.py", line 310, in vig_break
break_ls = list(vig_break_for_length( c, i, frequencies ))
File "ciphers.py", line 288, in vig_break_for_length
split_break = caesar_break( ciphertext, frequencies )
File "ciphers.py", line 192, in caesar_break
return [ can_t[ 2 ], can_t[ 1 ] ]
TypeError: 'int' object is not subscriptable

Solving a first order BVP with two boundary conditions with scipy's solve_bvp

I am using scipy's BVP solver:
http://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.solve_bvp.html
The problem I am running into is that you can only have as many boundary conditions as you have equations. I only have one equation but I have two boundary conditions. How can this be fixed?
MWE
>>> import numpy as np
>>> from scipy.integrate import solve_bvp
>>>
>>> x = np.linspace(0, 1, 100)
>>> dydx = lambda x,y: y*np.sin(x)
>>>
>>> result = solve_bvp(dydx,
... lambda ya,yb: np.array([ (ya[0]-1)**2 + (yb[0]-1)**2 ]),
... x, [np.ones(len(x))], max_nodes=100000, tol=1e-9)
>>>
>>> result
message: 'The algorithm converged to the desired accuracy.'
niter: 2
p: None
rms_residuals: array([ 3.48054730e-10, 3.47134800e-10, 3.46220750e-10,
3.45304147e-10, 3.44446495e-10, 3.43708535e-10,
3.42834209e-10, 3.41730399e-10, 3.40902853e-10,
3.40116511e-10, 3.39286663e-10, 3.38873550e-10,
3.37853506e-10, 3.36632825e-10, 3.35880059e-10,
3.35385717e-10, 3.35453551e-10, 3.34784891e-10,
3.32401725e-10, 3.34486867e-10, 3.35674629e-10,
3.37743169e-10, 3.34329677e-10, 3.29311236e-10,
3.27606354e-10, 3.28578369e-10, 3.27772742e-10,
3.26447666e-10, 3.24908674e-10, 3.24192402e-10,
3.25862692e-10, 3.28872815e-10, 3.22757465e-10,
3.21914926e-10, 3.20227078e-10, 3.23579897e-10,
3.28140843e-10, 3.18151515e-10, 3.21177949e-10,
3.16611117e-10, 3.45372059e-10, 3.18345626e-10,
3.24069081e-10, 3.32570305e-10, 3.19141250e-10,
3.14376144e-10, 3.18278959e-10, 3.11802424e-10,
3.15597596e-10, 3.22818017e-10, 3.15384028e-10,
3.17673241e-10, 3.08099021e-10, 3.11743210e-10,
3.28763320e-10, 3.24475197e-10, 3.28343741e-10,
3.25892534e-10, 3.12411478e-10, 3.37194926e-10,
3.20060651e-10, 3.03517565e-10, 3.00795182e-10,
3.06846379e-10, 3.00064770e-10, 3.05765788e-10,
2.99543196e-10, 2.98157661e-10, 2.97863071e-10,
2.96467397e-10, 3.74567928e-10, 3.24304178e-10,
3.16165056e-10, 3.02449962e-10, 2.93348900e-10,
3.08601600e-10, 2.93492038e-10, 3.11756310e-10,
2.97438508e-10, 3.17903029e-10, 3.05491804e-10,
3.02623385e-10, 3.06340149e-10, 2.94595579e-10,
2.87571373e-10, 3.03866639e-10, 3.42985927e-10,
3.21829601e-10, 3.70164964e-10, 3.53563487e-10,
3.00178404e-10, 2.83888849e-10, 2.82310753e-10,
2.85661232e-10, 3.11405296e-10, 2.80954237e-10,
2.79523163e-10, 2.80819968e-10, 2.94406497e-10,
3.19548071e-10, 2.95355340e-10, 2.77522541e-10,
2.76703591e-10, 2.88121141e-10, 2.75290617e-10,
2.84220379e-10, 2.89876300e-10, 3.14510031e-10,
3.11057911e-10, 2.72303350e-10, 2.79168046e-10,
2.90700062e-10, 2.78438999e-10, 2.68897634e-10,
2.69286657e-10, 2.90472537e-10, 2.78378707e-10,
2.97980086e-10, 2.97008148e-10, 2.65028623e-10,
2.64744165e-10, 2.69437313e-10, 2.63909411e-10,
2.62339786e-10, 2.71045386e-10, 2.65850861e-10,
2.78162780e-10, 2.61231989e-10, 2.70109868e-10,
2.61595375e-10, 2.59299272e-10, 2.65106316e-10,
2.74283076e-10, 2.86861196e-10, 3.03175803e-10,
2.58290170e-10, 3.61324845e-10, 3.39239278e-10,
2.91296094e-10, 2.83918017e-10, 4.52002829e-10,
2.52915179e-10, 3.13709791e-10, 3.72555078e-10,
2.48903834e-10, 2.58089690e-10, 2.86634265e-10,
2.60879823e-10, 2.64643448e-10, 3.03583577e-10,
5.12385186e-10, 2.42415186e-10, 3.47677749e-10,
2.41037177e-10, 2.91624837e-10, 2.88486833e-10,
2.97731066e-10, 3.46537042e-10, 2.44416103e-10,
4.29099468e-10, 4.71320607e-10, 2.97672164e-10,
3.26787171e-10, 2.34920240e-10, 2.64792458e-10,
2.91952218e-10, 2.47064463e-10, 2.34000456e-10,
4.10948830e-10, 2.36520479e-10, 3.42444147e-10,
2.76749245e-10, 2.51379106e-10, 2.40093828e-10,
2.72602754e-10, 3.94004751e-10, 2.84796018e-10,
3.72431030e-10, 2.23313796e-10, 3.32252341e-10,
3.34369044e-10, 2.63230702e-10, 2.17694780e-10,
3.25346854e-10, 2.64869219e-10, 3.51158895e-10,
3.60872478e-10, 3.09047143e-10, 2.22308395e-10,
2.43344334e-10, 2.16527726e-10, 2.98642975e-10,
2.77152047e-10, 2.66161092e-10, 2.91230604e-10,
2.37973344e-10, 2.95802884e-10, 2.78890213e-10,
2.19485810e-10, 3.53536609e-10, 2.16716319e-10,
2.51682560e-10, 2.04749227e-10, 4.31531575e-10,
3.47595602e-10, 2.38112586e-10, 1.92156254e-10,
2.46451083e-10, 2.99903096e-10, 1.90926751e-10,
2.05652721e-10, 2.33415220e-10, 2.43209873e-10,
1.85670073e-10, 2.02780645e-10, 1.89290313e-10,
1.81291292e-10, 1.77940599e-10, 3.60470288e-10,
3.28978503e-10, 1.74204497e-10, 1.95779041e-10,
2.50524362e-10, 2.49249184e-10, 1.67522152e-10,
1.68202192e-10, 1.82172067e-10, 1.77510490e-10,
1.62468247e-10, 1.75426885e-10, 3.24084379e-10,
2.21087707e-10, 1.88843987e-10, 2.57800867e-10,
1.53483353e-10, 1.80491618e-10, 2.28820880e-10,
2.32095332e-10, 1.90031952e-10, 1.46493968e-10,
2.00403717e-10, 3.23811210e-10, 1.90421082e-10,
1.45237509e-10, 1.67970046e-10, 1.49189288e-10,
1.39748871e-10, 1.40621758e-10, 1.33316350e-10,
2.22781676e-10, 1.31021647e-10, 2.12758988e-10,
1.38894682e-10, 1.75219768e-10, 1.78296709e-10,
3.67044064e-10, 2.04279379e-10, 2.11899286e-10,
1.59322174e-10, 1.21129350e-10, 1.18003803e-10,
1.42850831e-10, 1.33020880e-10, 1.27620814e-10,
1.48379719e-10, 3.35008994e-10, 3.31675208e-10,
2.49871984e-10, 1.06526186e-10, 1.57190187e-10,
9.38688508e-11, 2.16167913e-10, 1.12548066e-10,
1.98572296e-10, 2.12773340e-10, 3.09554965e-10,
2.32665662e-10, 8.05365861e-11, 2.71090303e-10,
1.60686511e-10, 1.20088934e-10, 3.23772391e-10,
2.01129249e-10, 3.04370308e-10, 6.75862037e-11,
7.60074235e-11, 1.55486106e-10, 2.24650749e-10,
2.10826836e-10, 3.75354523e-10, 1.48504437e-10,
1.65019969e-10, 7.52309342e-11, 3.59188285e-10,
1.55801401e-10, 1.52568581e-10, 5.38230045e-11])
sol: <scipy.interpolate.interpolate.PPoly object at 0x2ad860930d58>
status: 0
success: True
x: array([ 0. , 0.003367 , 0.00673401, 0.01010101, 0.01346801,
0.01683502, 0.02020202, 0.02356902, 0.02693603, 0.03030303,
0.03367003, 0.03703704, 0.04040404, 0.04377104, 0.04713805,
0.05050505, 0.05387205, 0.05723906, 0.06060606, 0.06397306,
0.06734007, 0.07070707, 0.07407407, 0.07744108, 0.08080808,
0.08417508, 0.08754209, 0.09090909, 0.09427609, 0.0976431 ,
0.1010101 , 0.1043771 , 0.10774411, 0.11111111, 0.11447811,
0.11784512, 0.12121212, 0.12457912, 0.12794613, 0.13131313,
0.13468013, 0.13804714, 0.14141414, 0.14478114, 0.14814815,
0.15151515, 0.15488215, 0.15824916, 0.16161616, 0.16498316,
0.16835017, 0.17171717, 0.17508418, 0.17845118, 0.18181818,
0.18518519, 0.18855219, 0.19191919, 0.1952862 , 0.1986532 ,
0.2020202 , 0.20538721, 0.20875421, 0.21212121, 0.21548822,
0.21885522, 0.22222222, 0.22558923, 0.22895623, 0.23232323,
0.23569024, 0.23905724, 0.24242424, 0.24579125, 0.24915825,
0.25252525, 0.25589226, 0.25925926, 0.26262626, 0.26599327,
0.26936027, 0.27272727, 0.27609428, 0.27946128, 0.28282828,
0.28619529, 0.28956229, 0.29292929, 0.2962963 , 0.2996633 ,
0.3030303 , 0.30639731, 0.30976431, 0.31313131, 0.31649832,
0.31986532, 0.32323232, 0.32659933, 0.32996633, 0.33333333,
0.33670034, 0.34006734, 0.34343434, 0.34680135, 0.35016835,
0.35353535, 0.35690236, 0.36026936, 0.36363636, 0.36700337,
0.37037037, 0.37373737, 0.37710438, 0.38047138, 0.38383838,
0.38720539, 0.39057239, 0.39393939, 0.3973064 , 0.4006734 ,
0.4040404 , 0.40740741, 0.41077441, 0.41414141, 0.41750842,
0.42087542, 0.42424242, 0.42760943, 0.43097643, 0.43434343,
0.43771044, 0.44107744, 0.44444444, 0.44781145, 0.45117845,
0.45454545, 0.45791246, 0.46127946, 0.46464646, 0.46801347,
0.47138047, 0.47474747, 0.47811448, 0.48148148, 0.48484848,
0.48821549, 0.49158249, 0.49494949, 0.4983165 , 0.5016835 ,
0.50505051, 0.50841751, 0.51178451, 0.51515152, 0.51851852,
0.52188552, 0.52525253, 0.52861953, 0.53198653, 0.53535354,
0.53872054, 0.54208754, 0.54545455, 0.54882155, 0.55218855,
0.55555556, 0.55892256, 0.56228956, 0.56565657, 0.56902357,
0.57239057, 0.57575758, 0.57912458, 0.58249158, 0.58585859,
0.58922559, 0.59259259, 0.5959596 , 0.5993266 , 0.6026936 ,
0.60606061, 0.60942761, 0.61279461, 0.61616162, 0.61952862,
0.62289562, 0.62626263, 0.62962963, 0.63299663, 0.63636364,
0.63973064, 0.64309764, 0.64646465, 0.64983165, 0.65319865,
0.65656566, 0.65993266, 0.66329966, 0.66666667, 0.67003367,
0.67340067, 0.67676768, 0.68013468, 0.68350168, 0.68686869,
0.69023569, 0.69360269, 0.6969697 , 0.7003367 , 0.7037037 ,
0.70707071, 0.71043771, 0.71380471, 0.71717172, 0.72053872,
0.72390572, 0.72727273, 0.73063973, 0.73400673, 0.73737374,
0.74074074, 0.74410774, 0.74747475, 0.75084175, 0.75420875,
0.75757576, 0.76094276, 0.76430976, 0.76767677, 0.77104377,
0.77441077, 0.77777778, 0.78114478, 0.78451178, 0.78787879,
0.79124579, 0.79461279, 0.7979798 , 0.8013468 , 0.8047138 ,
0.80808081, 0.81144781, 0.81481481, 0.81818182, 0.82154882,
0.82491582, 0.82828283, 0.83164983, 0.83501684, 0.83838384,
0.84175084, 0.84511785, 0.84848485, 0.85185185, 0.85521886,
0.85858586, 0.86195286, 0.86531987, 0.86868687, 0.87205387,
0.87542088, 0.87878788, 0.88215488, 0.88552189, 0.88888889,
0.89225589, 0.8956229 , 0.8989899 , 0.9023569 , 0.90572391,
0.90909091, 0.91245791, 0.91582492, 0.91919192, 0.92255892,
0.92592593, 0.92929293, 0.93265993, 0.93602694, 0.93939394,
0.94276094, 0.94612795, 0.94949495, 0.95286195, 0.95622896,
0.95959596, 0.96296296, 0.96632997, 0.96969697, 0.97306397,
0.97643098, 0.97979798, 0.98316498, 0.98653199, 0.98989899,
0.99326599, 0.996633 , 1. ])
y: array([[ 0.79388397, 0.79388847, 0.79390197, 0.79392447, 0.79395597,
0.79399647, 0.79404598, 0.79410449, 0.794172 , 0.79424853,
0.79433406, 0.7944286 , 0.79453215, 0.79464471, 0.7947663 ,
0.7948969 , 0.79503653, 0.79518518, 0.79534287, 0.79550958,
0.79568534, 0.79587013, 0.79606397, 0.79626686, 0.7964788 ,
0.7966998 , 0.79692987, 0.797169 , 0.79741721, 0.7976745 ,
0.79794087, 0.79821634, 0.7985009 , 0.79879457, 0.79909735,
0.79940925, 0.79973028, 0.80006043, 0.80039973, 0.80074817,
0.80110577, 0.80147253, 0.80184846, 0.80223358, 0.80262788,
0.80303138, 0.80344409, 0.80386601, 0.80429716, 0.80473755,
0.80518718, 0.80564606, 0.80611421, 0.80659164, 0.80707835,
0.80757437, 0.80807969, 0.80859433, 0.8091183 , 0.80965162,
0.8101943 , 0.81074634, 0.81130776, 0.81187857, 0.81245879,
0.81304843, 0.8136475 , 0.81425602, 0.814874 , 0.81550144,
0.81613838, 0.81678482, 0.81744077, 0.81810625, 0.81878128,
0.81946586, 0.82016002, 0.82086378, 0.82157714, 0.82230012,
0.82303274, 0.82377501, 0.82452696, 0.8252886 , 0.82605994,
0.826841 , 0.8276318 , 0.82843236, 0.82924269, 0.83006282,
0.83089275, 0.83173252, 0.83258213, 0.83344161, 0.83431098,
0.83519025, 0.83607944, 0.83697858, 0.83788768, 0.83880677,
0.83973586, 0.84067497, 0.84162413, 0.84258336, 0.84355267,
0.84453209, 0.84552164, 0.84652134, 0.84753122, 0.84855129,
0.84958158, 0.85062211, 0.8516729 , 0.85273397, 0.85380536,
0.85488708, 0.85597915, 0.85708161, 0.85819447, 0.85931775,
0.86045149, 0.86159571, 0.86275043, 0.86391567, 0.86509147,
0.86627784, 0.86747482, 0.86868242, 0.86990068, 0.87112962,
0.87236927, 0.87361965, 0.8748808 , 0.87615273, 0.87743548,
0.87872907, 0.88003353, 0.88134889, 0.88267518, 0.88401242,
0.88536065, 0.88671989, 0.88809017, 0.88947152, 0.89086397,
0.89226754, 0.89368228, 0.89510821, 0.89654536, 0.89799375,
0.89945343, 0.90092442, 0.90240675, 0.90390045, 0.90540555,
0.9069221 , 0.90845011, 0.90998962, 0.91154066, 0.91310327,
0.91467748, 0.91626331, 0.91786081, 0.91947001, 0.92109093,
0.92272362, 0.92436811, 0.92602442, 0.9276926 , 0.92937269,
0.9310647 , 0.93276869, 0.93448468, 0.93621271, 0.93795282,
0.93970504, 0.9414694 , 0.94324595, 0.94503471, 0.94683573,
0.94864904, 0.95047469, 0.95231269, 0.9541631 , 0.95602595,
0.95790128, 0.95978913, 0.96168953, 0.96360252, 0.96552814,
0.96746643, 0.96941743, 0.97138117, 0.9733577 , 0.97534706,
0.97734928, 0.97936441, 0.98139248, 0.98343353, 0.98548761,
0.98755476, 0.98963501, 0.99172841, 0.993835 , 0.99595481,
0.9980879 , 1.0002343 , 1.00239405, 1.0045672 , 1.00675379,
1.00895385, 1.01116744, 1.0133946 , 1.01563536, 1.01788978,
1.02015789, 1.02243974, 1.02473537, 1.02704483, 1.02936815,
1.03170539, 1.03405659, 1.03642179, 1.03880103, 1.04119437,
1.04360185, 1.0460235 , 1.04845939, 1.05090954, 1.05337402,
1.05585286, 1.05834611, 1.06085381, 1.06337602, 1.06591277,
1.06846412, 1.07103012, 1.0736108 , 1.07620622, 1.07881642,
1.08144145, 1.08408136, 1.0867362 , 1.08940601, 1.09209084,
1.09479074, 1.09750576, 1.10023595, 1.10298135, 1.10574202,
1.108518 , 1.11130934, 1.11411609, 1.1169383 , 1.11977602,
1.1226293 , 1.12549819, 1.12838274, 1.13128299, 1.13419901,
1.13713083, 1.14007851, 1.14304211, 1.14602166, 1.14901722,
1.15202884, 1.15505658, 1.15810048, 1.1611606 , 1.16423698,
1.16732967, 1.17043874, 1.17356423, 1.17670619, 1.17986467,
1.18303973, 1.18623141, 1.18943978, 1.19266488, 1.19590676,
1.19916548, 1.20244108, 1.20573363, 1.20904318, 1.21236977,
1.21571346, 1.2190743 , 1.22245235, 1.22584765, 1.22926027,
1.23269025, 1.23613766, 1.23960253, 1.24308492, 1.2465849 ,
1.25010251, 1.2536378 , 1.25719083]])
yp: array([[ 0. , 0.00267302, 0.0053461 , 0.0080193 , 0.01069269,
0.01336631, 0.01604024, 0.01871453, 0.02138925, 0.02406445,
0.0267402 , 0.02941655, 0.03209358, 0.03477132, 0.03744986,
0.04012924, 0.04280954, 0.0454908 , 0.04817309, 0.05085648,
0.05354102, 0.05622677, 0.05891379, 0.06160215, 0.0642919 ,
0.06698311, 0.06967583, 0.07237013, 0.07506607, 0.0777637 ,
0.0804631 , 0.08316431, 0.08586741, 0.08857244, 0.09127948,
0.09398858, 0.0966998 , 0.09941321, 0.10212887, 0.10484683,
0.10756715, 0.11028991, 0.11301515, 0.11574295, 0.11847335,
0.12120642, 0.12394223, 0.12668083, 0.12942228, 0.13216665,
0.134914 , 0.13766438, 0.14041786, 0.1431745 , 0.14593436,
0.1486975 , 0.15146398, 0.15423387, 0.15700722, 0.1597841 ,
0.16256456, 0.16534867, 0.16813649, 0.17092808, 0.1737235 ,
0.17652281, 0.17932607, 0.18213335, 0.18494471, 0.1877602 ,
0.1905799 , 0.19340385, 0.19623212, 0.19906478, 0.20190187,
0.20474348, 0.20758965, 0.21044044, 0.21329593, 0.21615617,
0.21902122, 0.22189114, 0.22476599, 0.22764585, 0.23053076,
0.23342079, 0.236316 , 0.23921645, 0.2421222 , 0.24503332,
0.24794987, 0.2508719 , 0.25379948, 0.25673268, 0.25967155,
0.26261615, 0.26556655, 0.2685228 , 0.27148497, 0.27445313,
0.27742732, 0.28040762, 0.28339409, 0.28638678, 0.28938576,
0.29239109, 0.29540283, 0.29842105, 0.3014458 , 0.30447715,
0.30751515, 0.31055988, 0.31361139, 0.31666974, 0.31973499,
0.32280722, 0.32588647, 0.32897281, 0.3320663 , 0.33516701,
0.33827498, 0.3413903 , 0.34451301, 0.34764319, 0.35078088,
0.35392616, 0.35707908, 0.3602397 , 0.3634081 , 0.36658432,
0.36976843, 0.37296049, 0.37616057, 0.37936872, 0.382585 ,
0.38580948, 0.38904223, 0.39228329, 0.39553273, 0.39879061,
0.402057 , 0.40533195, 0.40861553, 0.4119078 , 0.41520881,
0.41851863, 0.42183733, 0.42516495, 0.42850157, 0.43184723,
0.43520202, 0.43856597, 0.44193917, 0.44532166, 0.4487135 ,
0.45211476, 0.45552551, 0.45894578, 0.46237566, 0.4658152 ,
0.46926446, 0.47272349, 0.47619237, 0.47967114, 0.48315988,
0.48665863, 0.49016747, 0.49368644, 0.49721562, 0.50075505,
0.5043048 , 0.50786493, 0.5114355 , 0.51501656, 0.51860818,
0.52221041, 0.52582331, 0.52944695, 0.53308138, 0.53672666,
0.54038285, 0.54405001, 0.54772819, 0.55141745, 0.55511786,
0.55882946, 0.56255232, 0.5662865 , 0.57003205, 0.57378903,
0.5775575 , 0.58133751, 0.58512912, 0.58893239, 0.59274738,
0.59657414, 0.60041272, 0.60426319, 0.6081256 , 0.61200001,
0.61588646, 0.61978503, 0.62369576, 0.6276187 , 0.63155392,
0.63550147, 0.6394614 , 0.64343376, 0.64741862, 0.65141602,
0.65542602, 0.65944867, 0.66348403, 0.66753215, 0.67159308,
0.67566687, 0.67975358, 0.68385327, 0.68796597, 0.69209174,
0.69623064, 0.70038272, 0.70454802, 0.7087266 , 0.7129185 ,
0.71712379, 0.7213425 , 0.72557469, 0.72982041, 0.7340797 ,
0.73835262, 0.74263921, 0.74693953, 0.75125361, 0.75558151,
0.75992327, 0.76427895, 0.76864858, 0.77303222, 0.7774299 ,
0.78184168, 0.7862676 , 0.79070771, 0.79516204, 0.79963065,
0.80411358, 0.80861086, 0.81312256, 0.81764869, 0.82218932,
0.82674447, 0.8313142 , 0.83589854, 0.84049753, 0.84511122,
0.84973964, 0.85438283, 0.85904083, 0.86371368, 0.86840142,
0.87310408, 0.8778217 , 0.88255432, 0.88730198, 0.89206471,
0.89684254, 0.90163551, 0.90644365, 0.911267 , 0.91610559,
0.92095945, 0.92582862, 0.93071312, 0.93561298, 0.94052825,
0.94545894, 0.95040508, 0.95536671, 0.96034386, 0.96533654,
0.97034479, 0.97536863, 0.98040809, 0.98546319, 0.99053396,
0.99562042, 1.0007226 , 1.00584051, 1.01097418, 1.01612363,
1.02128888, 1.02646995, 1.03166686, 1.03687962, 1.04210827,
1.0473528 , 1.05261324, 1.0578896 ]])
As you can see, y is very far from the boundary conditions of y(x=0) = y(x=1) = 1.
If you specify two boundary conditions y(0)=1 and y(1)=1 for a first order ODE, then in general the problem is overdetermined and there is no solution. If you specify just the initial condition y(0)=y0, you have a first order initial value problem. In fact, in this case, you can derive the exact solution: y(x) = y0*exp(-cos(x)).

Categories

Resources