curve fitting with scipy - python

I am trying to fit a curve with curve_fit from SciPy. But it doesn't work as intended, and I don't know why. Here is my code:
xdata = np.asarray(std_ex_90degree[5050:5150,0])
ydata = np.asarray(std_ex_90degree[5050:5150,1])
print(xdata,ydata)
def Gauss(x, A, B):
y = A*np.exp(-1*B*x**2)
return y
popt, covariance = curve_fit(Gauss, xdata, ydata)
fit_A, fit_B = popt
fit_y = Gauss(xdata, fit_A, fit_B)
plt.scatter(xdata, ydata, label='data',s=5)
plt.plot(xdata, fit_y, '-', label='fit')
plt.legend()
As you can see the Gaussian Fit didn't work and I only got a straight line.
Here is the data:
[2834.486 2834.968 2835.45 2835.932 2836.414 2836.896 2837.378 2837.861
2838.343 2838.825 2839.307 2839.789 2840.271 2840.753 2841.235 2841.718
2842.2 2842.682 2843.164 2843.646 2844.128 2844.61 2845.093 2845.575
2846.057 2846.539 2847.021 2847.503 2847.985 2848.468 2848.95 2849.432
2849.914 2850.396 2850.878 2851.36 2851.843 2852.325 2852.807 2853.289
2853.771 2854.253 2854.735 2855.218 2855.699 2856.182 2856.664 2857.146
2857.628 2858.11 2858.592 2859.074 2859.557 2860.039 2860.521 2861.003
2861.485 2861.967 2862.449 2862.932 2863.414 2863.896 2864.378 2864.86
2865.342 2865.824 2866.307 2866.789 2867.271 2867.753 2868.235 2868.717
2869.199 2869.682 2870.164 2870.646 2871.128 2871.61 2872.092 2872.574
2873.056 2873.539 2874.021 2874.503 2874.985 2875.467 2875.949 2876.431
2876.914 2877.396 2877.878 2878.36 2878.842 2879.324 2879.806 2880.289
2880.771 2881.253 2881.735 2882.217]
[0.5027119 0.5155925 0.5296563 0.5450429 0.5619112 0.5804411 0.6008373
0.6233361 0.6482099 0.67577 0.7063611 0.7403504 0.7781109 0.8200049
0.8663718 0.9175249 0.9737514 1.035319 1.102472 1.175419 1.254304
1.339163 1.429889 1.526202 1.627649 1.733603 1.84322 1.955248
2.067605 2.176702 2.276757 2.359875 2.417753 2.445059 2.441798
2.41245 2.362954 2.298523 2.223243 2.14052 2.05336 1.964326
1.87539 1.787885 1.702644 1.620191 1.540921 1.465193 1.393333
1.325607 1.262171 1.203057 1.148185 1.097403 1.050529 1.007382
0.9678 0.9316369 0.8987471 0.8689752 0.8421496 0.8180863 0.7965991
0.7775094 0.76065 0.7458642 0.732995 0.7218768 0.7123291 0.7041584
0.6971676 0.6911709 0.6860058 0.6815417 0.6776828 0.674363 0.6715436
0.6692089 0.6673671 0.6660498 0.6653103 0.6652156 0.6658351 0.6672268
0.6694273 0.6724483 0.676279 0.6808962 0.686272 0.6923797 0.699192
0.7066767 0.7147906 0.7234787 0.7326793 0.7423348 0.7524015 0.7628553
0.7736901 0.7849081]

Inappropriate model. Minor adjustments to model yield a rough fit:
import numpy as np
from matplotlib import pyplot as plt
from scipy.optimize import curve_fit
xdata = np.array((
2834.486, 2834.968, 2835.45 , 2835.932, 2836.414, 2836.896, 2837.378, 2837.861,
2838.343, 2838.825, 2839.307, 2839.789, 2840.271, 2840.753, 2841.235, 2841.718,
2842.2 , 2842.682, 2843.164, 2843.646, 2844.128, 2844.61 , 2845.093, 2845.575,
2846.057, 2846.539, 2847.021, 2847.503, 2847.985, 2848.468, 2848.95 , 2849.432,
2849.914, 2850.396, 2850.878, 2851.36 , 2851.843, 2852.325, 2852.807, 2853.289,
2853.771, 2854.253, 2854.735, 2855.218, 2855.699, 2856.182, 2856.664, 2857.146,
2857.628, 2858.11 , 2858.592, 2859.074, 2859.557, 2860.039, 2860.521, 2861.003,
2861.485, 2861.967, 2862.449, 2862.932, 2863.414, 2863.896, 2864.378, 2864.86 ,
2865.342, 2865.824, 2866.307, 2866.789, 2867.271, 2867.753, 2868.235, 2868.717,
2869.199, 2869.682, 2870.164, 2870.646, 2871.128, 2871.61 , 2872.092, 2872.574,
2873.056, 2873.539, 2874.021, 2874.503, 2874.985, 2875.467, 2875.949, 2876.431,
2876.914, 2877.396, 2877.878, 2878.36 , 2878.842, 2879.324, 2879.806, 2880.289,
2880.771, 2881.253, 2881.735, 2882.217,
))
ydata = np.array((
0.5027119, 0.5155925, 0.5296563, 0.5450429, 0.5619112, 0.5804411, 0.6008373,
0.6233361, 0.6482099, 0.67577 , 0.7063611, 0.7403504, 0.7781109, 0.8200049,
0.8663718, 0.9175249, 0.9737514, 1.035319 , 1.102472 , 1.175419 , 1.254304 ,
1.339163 , 1.429889 , 1.526202 , 1.627649 , 1.733603 , 1.84322 , 1.955248 ,
2.067605 , 2.176702 , 2.276757 , 2.359875 , 2.417753 , 2.445059 , 2.441798 ,
2.41245 , 2.362954 , 2.298523 , 2.223243 , 2.14052 , 2.05336 , 1.964326 ,
1.87539 , 1.787885 , 1.702644 , 1.620191 , 1.540921 , 1.465193 , 1.393333 ,
1.325607 , 1.262171 , 1.203057 , 1.148185 , 1.097403 , 1.050529 , 1.007382 ,
0.9678 , 0.9316369, 0.8987471, 0.8689752, 0.8421496, 0.8180863, 0.7965991,
0.7775094, 0.76065 , 0.7458642, 0.732995 , 0.7218768, 0.7123291, 0.7041584,
0.6971676, 0.6911709, 0.6860058, 0.6815417, 0.6776828, 0.674363 , 0.6715436,
0.6692089, 0.6673671, 0.6660498, 0.6653103, 0.6652156, 0.6658351, 0.6672268,
0.6694273, 0.6724483, 0.676279 , 0.6808962, 0.686272 , 0.6923797, 0.699192 ,
0.7066767, 0.7147906, 0.7234787, 0.7326793, 0.7423348, 0.7524015, 0.7628553,
0.7736901, 0.7849081,
))
def gauss(x: np.ndarray, *args: float) -> np.ndarray:
a, b, c, d = args
return a*np.exp(-b*(x - c)**2) + d
popt, _ = curve_fit(
gauss, xdata, ydata,
p0=(1.7, 0.02, 2851, 0.7),
maxfev=100_000,
)
print(popt)
fit_y = gauss(xdata, *popt)
plt.scatter(xdata, ydata, label='data', s=5)
plt.plot(xdata, fit_y, '-', label='fit')
plt.legend()
plt.show()
[1.68927347e+00 2.10977276e-02 2.85117456e+03 6.81806648e-01]
To do any better, your model needs to change more.

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" ]

How to calculate histogram intersection?

I have two datasets:
data1 = np.array([11.59877469, 12.52525819, 11.30371594, 9.09666023, 9.69178239,
15.50122531, 16.73578474, 11.59361365, 9.33284219, 10.24323511,
12.39877469, 14.22419388, 11.18491025, 9.38917885, 9.56598452,
13.40122531, 13.58126828, 11.66021816, 9.90587666, 10.09661371,
12.69877469, 13.36333954, 11.40602325, 9.04926015, 9.58116652,
14.30122531, 13.41946488, 11.50330911, 9.70846369, 11.57496747,
13.09877469, 15.59128195, 12.38077981, 9.24187201, 9.4187133 ,
14.50122531, 14.00030896, 10.91137571, 10.09368741, 10.59655608,
12.19877469, 12.66493842, 11.151608 , 10.04291564, 10.75044566,
13.90122531, 13.80879138, 12.01213962, 9.77266764, 10.6047969 ,
11.29877469, 11.91469995, 10.49547065, 9.54128679, 10.70684913,
15.80122531, 15.69100383, 11.00648136, 10.26949539, 11.05984311,
13.29877469, 14.36322429, 10.39796445, 9.15126197, 10.19866594,
13.90122531, 13.22035631, 11.47720851, 10.43120555, 11.41316408,
13.09877469, 12.39251915, 11.18971136, 9.9574122 , 10.25744447,
14.20122531, 13.27219366, 11.60801697, 9.72976318, 12.07620948,
13.69877469, 16.08363333, 12.54008941, 9.38917885, 10.24985347,
14.20122531, 13.84544674, 11.29659942, 10.29799654, 10.78807365,
12.39877469, 13.80385357, 11.79124697, 9.85270434, 10.47867621,
15.30122531, 12.68375859, 11.88853282, 10.36460104, 11.52377995,
12.09877469, 12.36280497, 11.03280231, 8.76603826, 11.57464465,
15.70122531, 16.69782975, 11.86483278, 9.75586377, 11.00865558,
14.39877469, 14.31138694, 10.65958136, 10.03571398, 9.75056092,
13.30122531, 13.52878112, 12.46156728, 10.58571404, 12.04584549,
12.69877469, 13.2230095 , 11.41802602, 9.82180264, 9.27339153,
14.50122531, 13.09520827, 10.91617682, 9.59895472, 12.33344676,
13.29877469, 16.05391916, 12.32137696, 9.41767999, 9.83146261,
14.00122531, 14.13174836, 11.67702203, 9.43724456, 11.17869979,
12.19877469, 13.48024677, 10.98060112, 10.22832584, 10.64047961,
14.90122531, 13.34485454, 11.90053559, 10.23379258, 11.59774047,
13.19877469, 12.95059023, 10.88549547, 9.31057157, 11.71497469,
15.40122531, 16.38875513, 12.68748139, 10.10058357, 12.50913252,
14.09877469, 14.37710664, 11.30162088, 10.18782192, 10.10388188,
14.30122531, 13.33032237, 11.20389433, 10.76632313, 12.05343649,
13.09877469, 12.8918117 , 11.89595483, 9.50078288, 10.53745474,
14.70122531, 13.50665794, 11.46071013, 9.62505531, 12.11286483,
14.19877469, 16.84775416, 13.15122724, 10.29252979, 10.48626721,
14.60122531, 14.15322172, 11.65332199, 10.40960057, 12.80367496,
13.99877469, 13.09027045, 10.75948812, 9.74799647, 11.09682545,
14.70122531, 14.32261612, 12.15495084, 11.38675768, 12.20135753])
data2 = np.array([ 8.29102276, 8.81896403, 8.34976878, 6.42352427, 7.60717448,
11.70897724, 12.33303488, 8.46800077, 5.99812922, 7.01110385,
9.19102276, 10.2088251 , 7.81963878, 5.4700672 , 5.9739585 ,
8.70897724, 8.96487767, 7.99967417, 6.66146823, 6.83476828,
7.69102276, 8.39992335, 8.16675914, 6.29511636, 6.39364899,
10.30897724, 8.45040312, 8.19228603, 6.36654906, 8.26193451,
8.59102276, 11.18658667, 8.97980554, 5.79348751, 6.00367267,
9.60897724, 8.72976357, 7.91417073, 6.37615127, 6.71721123,
7.49102276, 8.6868748 , 7.18480092, 6.0359 , 5.92277097,
9.60897724, 8.545837 , 8.47760298, 5.90062301, 6.21032767,
7.39102276, 7.81907928, 7.17999982, 5.40106214, 6.25461858,
10.80897724, 11.00889351, 7.81186342, 5.67230835, 5.96003158,
8.19102276, 8.73112115, 6.52866357, 5.52706949, 4.4671902 ,
8.10897724, 8.19316585, 6.86320746, 6.08603321, 6.16608132,
6.79102276, 7.77483293, 7.52712017, 5.8504898 , 6.12882072,
8.50897724, 8.6267387 , 6.7990035 , 6.30474566, 7.22539441,
8.19102276, 10.96600474, 7.90754278, 5.22765471, 4.40776186,
8.50897724, 8.12679632, 6.83230576, 5.4844976 , 5.91578523,
6.69102276, 8.20840578, 7.03269298, 5.21565194, 5.05562527,
7.30897724, 7.55289343, 7.27693232, 5.92672361, 5.96003158,
6.99102276, 6.87038206, 6.68557263, 4.45240618, 5.20354631,
9.70897724, 10.90586864, 7.84036457, 5.95522475, 6.16543151,
6.89102276, 8.53201258, 5.92952852, 5.11574518, 4.783206 ,
7.70897724, 6.74452625, 6.08106276, 5.64620776, 5.95244058,
6.99102276, 7.40697979, 6.33365116, 4.83522934, 4.65111677,
8.50897724, 7.28871498, 6.29977521, 5.09927389, 6.87966444,
6.99102276, 10.09820922, 7.30360661, 5.13735016, 4.28326363,
8.40897724, 7.5750166 , 6.05496217, 5.00656879, 4.58470269,
5.59102276, 6.14985776, 6.17674211, 4.13858807, 4.790797 ,
6.60897724, 7.14838493, 6.35917805, 5.61290551, 5.48221238,
6.79102276, 6.94369276, 6.13863875, 4.45480673, 5.46078358,
8.90897724, 9.97235342, 7.59315097, 4.81395693, 4.91590048,
6.39102276, 7.53212783, 5.79631951, 4.70922198, 4.02602635,
7.50897724, 7.39109003, 6.52568932, 5.16827895, 5.08529488,
6.09102276, 7.06884081, 7.31560938, 4.59041629, 3.87181395,
7.40897724, 6.81783696, 6.42578256, 4.81875804, 5.69585312,
6.69102276, 10.16457875, 6.46205907, 4.9708389 , 3.51849298,
6.50897724, 7.2665918 , 5.5437311 , 4.37173093, 5.07011288,
5.19102276, 7.60543854, 6.30755057, 4.07198357, 3.76878908,
7.30897724, 6.88420648, 6.77290291, 5.13017559, 6.2691062 ])
and I'd like to plot two histograms:
x1,y1,_ = plt.hist(data1, alpha=0.4, bins=20)
x2,y2,_ = plt.hist(data2, alpha=0.4, bins=20)
the result is the following:
How can I calculate the area of intersecting region?
I tried to find similar bins centers (to find zero-crossing), but I wasn't succeeded, I also tried to find the minimum between two bins with the same index, but it doesn't work for negative data.
Make sure you use the same range and number of bins in both histograms:
>>> rng = min(data1.min(), data2.min()), max(data1.max(), data2.max())
>>> n1, bins1, _ = plt.hist(data1, alpha=0.4, bins=30, range=rng)
>>> n2, bins2, _ = plt.hist(data2, alpha=0.4, bins=30, range=rng)
Then the intersection can be easily calculated:
>>> intersection = np.minimum(n1, n2)
>>> area = intersection.sum()
>>> area
20.0
>>> plt.bar(bins1[:-1], intersection, width=bins1[1]- bins1[0])
You can use:
bin_edges = np.histogram_bin_edges(np.concatenate([data1, data2]))
hist1, _ = np.histogram(data1, bins=bin_edges)
hist2, _ = np.histogram(data2, bins=bin_edges)
np.minimum(hist1, hist2).sum()
Output:
20

How to sum all the pixels of a PIL image?

i'm doing a project that involves getting every pixel from an image than averaging it out, i'm having some problems
here's the code
for i in range(0, 3):
for j in range(0, 3):
img = Image.open("Row " + str(i + 1) + " Col " + str(j + 1) + ".png", "r")
width, height = img.size
pixel_values = list(img.getdata())
pixel_values = np.array(pixel_values).reshape((width, height, 3))
total = list(sum(pixel_values) / len(pixel_values))
print("TOTAL: " + str(total))
and the output
TOTAL: [array([150.73267327, 147.4950495 , 135.68316832]), array([157.04950495, 153.71287129, 137.23762376]), array([162.94059406, 160.89108911, 142.02970297]), array([138.05940594, 137.72277228, 129.43564356]), array([124.86138614, 126.15841584, 124.0990099 ]), array([138.1980198 , 139.33663366, 138.45544554]), array([119.24752475, 120.17821782, 119.96039604]), array([65.2970297 , 61.94059406, 61. ]), array([42.84158416, 33.3960396 , 31.3960396 ]), array([55.82178218, 38.18811881, 33.5049505 ]), array([93.87128713, 58.76237624, 45.26732673]), array([152.16831683, 90.93069307, 58.16831683]), array([187.72277228, 100.9009901 , 45.12871287]), array([191.84158416, 89.82178218, 22.6039604 ]), array([194.38613861, 87.99009901, 19.77227723]), array([196.00990099, 87.94059406, 19.2970297 ]), array([198.16831683, 88.9009901 , 19.36633663]), array([199.15841584, 88.64356436, 18.6039604 ]), array([198.68316832, 87.83168317, 17.48514851]), array([199.31683168, 87.86138614, 17.32673267]), array([199.44554455, 87.54455446, 17.21782178]), array([199.67326733, 87.93069307, 17.34653465]), array([199.78217822, 88.26732673, 17.26732673]), array([199.99009901, 88.14851485, 16.94059406]), array([199.74257426, 88.10891089, 17.05940594]), array([200.30693069, 88.66336634, 18.17821782]), array([200.37623762, 88.47524752, 17.74257426]), array([201.2970297 , 88.92079208, 17.79207921]), array([200.33663366, 87.71287129, 16.41584158]), array([201.11881188, 88.2970297 , 16.9009901 ]), array([201.4950495 , 88.72277228, 17. ]), array([200.65346535, 88.12871287, 16.45544554]), array([200.11881188, 87.73267327, 16.33663366]), array([200.10891089, 87.79207921, 16.28712871]), array([200.36633663, 87.91089109, 16.16831683]), array([200.58415842, 88.10891089, 16.30693069]), array([199.65346535, 87.17821782, 15.42574257]), array([200.14851485, 87.42574257, 16.12871287]), array([200.2970297 , 87.16831683, 15.88118812]), array([201.00990099, 88.0990099 , 16.85148515]), array([200.11881188, 87.55445545, 16.7029703 ]), array([200.37623762, 87.87128713, 16.21782178]), array([200.55445545, 87.89108911, 16.30693069]), array([200.48514851, 87.7029703 , 15.93069307]), array([200.77227723, 87.79207921, 16.33663366]), array([200.1980198 , 87.59405941, 16.94059406]), array([201.27722772, 88.76237624, 17.65346535]), array([200.51485149, 88.28712871, 16.61386139]), array([201.43564356, 88.33663366, 16.69306931]), array([200.52475248, 87.2970297 , 15.38613861]), array([200.6039604 , 87.75247525, 15.93069307]), array([200.67326733, 87.95049505, 16.04950495]), array([201.16831683, 88.75247525, 16.6039604 ]), array([200.43564356, 88.18811881, 16.17821782]), array([201.00990099, 88.14851485, 16.35643564]), array([200.83168317, 87.85148515, 15.98019802]), array([200.66336634, 87.73267327, 15.52475248]), array([201.13861386, 88.24752475, 16. ]), array([200.56435644, 87.63366337, 15.56435644]), array([200.89108911, 87.48514851, 15.79207921]), array([201.12871287, 87.66336634, 16.32673267]), array([201.64356436, 87.95049505, 16.78217822]), array([200.93069307, 87.33663366, 15.4950495 ]), array([201.12871287, 87.83168317, 15.97029703]), array([201.23762376, 88.01980198, 15.98019802]), array([200.55445545, 87.68316832, 15.92079208]), array([201.0990099 , 87.92079208, 15.97029703]), array([201.10891089, 87.91089109, 15.82178218]), array([201.40594059, 87.6039604 , 15.13861386]), array([201.44554455, 87.63366337, 15.55445545]), array([201.6039604 , 87.88118812, 16.18811881]), array([201.97029703, 87.65346535, 15.93069307]), array([200.94059406, 86.33663366, 14.52475248]), array([201.03960396, 86.63366337, 13.75247525]), array([200.74257426, 86.97029703, 13.8019802 ]), array([200.10891089, 86.5049505 , 13.52475248]), array([198.92079208, 85.92079208, 12.77227723]), array([197.98019802, 85.63366337, 13.38613861]), array([198.4950495 , 85.79207921, 13.55445545]), array([197.54455446, 85.46534653, 13.30693069]), array([197.69306931, 85.95049505, 13.67326733]), array([197.51485149, 85.92079208, 13.47524752]), array([197.56435644, 86.12871287, 13.81188119]), array([197. , 86.06930693, 14.08910891]), array([196.88118812, 85.97029703, 13.5049505 ]), array([196.35643564, 85.11881188, 12.71287129]), array([195.86138614, 85.0990099 , 12.48514851]), array([195.15841584, 84.81188119, 12.78217822]), array([194. , 84.83168317, 13.35643564]), array([190.63366337, 83.66336634, 13.41584158]), array([187.96039604, 82.63366337, 13.78217822]), array([180.31683168, 80.66336634, 15.93069307]), array([166.03960396, 77.46534653, 25.25742574]), array([88.46534653, 45.98019802, 27. ]), array([42.02970297, 31.15841584, 29.57425743]), array([33.92079208, 28.25742574, 27.22772277]), array([30.18811881, 25.94059406, 25.51485149]), array([29.85148515, 25.75247525, 25.22772277]), array([30.10891089, 25.84158416, 25.44554455]), array([30.15841584, 26. , 25.95049505]), array([27.71287129, 23.79207921, 23.67326733])]
TOTAL: [array([22.31683168, 19.92079208, 20.45544554]), array([23.45544554, 20.98019802, 21.76237624]), array([22.24752475, 19.87128713, 20.61386139]), array([31.42574257, 30.44554455, 31.05940594]), array([104.45544554, 104.33663366, 105.20792079]), array([32.20792079, 28.98019802, 29.34653465]), array([33.30693069, 28.3960396 , 28.79207921])...74257426]), array([ 18.63366337, 87.43564356,
TOTAL: [array([20.5049505 , 18.83168317, 19.06930693]), array([17.16831683, 16.03960396, 16.71287129]), array([20.77227723, 20.27722772, 21.65346535]), array([69.94059406, 69.6039604 , 69.75247525]), array([57.92079208, 56.02970297, 55.43564356]), array([36.75247525, 33.00990099, 30.84158416]), array([34.95049505, 30.06930693, 27.92079208]), array([32.78217822, 28.04950495, 25.94059406]), array([33.76237624, 28.66336634, 26.42574257]), array([35.14851485, 30.08910891, 26.65346535]), array([37.86138614, 32.0990099 , 25.94059406]), array([53.77227723, 48.00990099, 31.6039604 ]), array([155.0990099 , 148.82178218, 68.36633663]), array([155.71287129, 146.25742574, 20.42574257]), , 17.66336634]), array([159.64356436, 150.41584158, 16.73267327]), array([159.03960396, 149.76237624, 17.07920792]), array([159.0990099 , 149.61386139, 17.82178218]), array([158.32673267, 148.7029703 , 18.94059406]), array([156.68316832, 146.3960396 , 19.59405941]), array([154.52475248, 143.94059406, 19.91089109]), array([152.69306931, 142.43564356, 22.25742574]), array([148.95049505, 138.34653465, 23.66336634]), array([142.87128713, 132.27722772, 33.37623762]), array([117.61386139, 107.61386139, 39.94059406]), array([69.86138614, 61.52475248, 31.41584158]), array([33.42574257, 28.27722772, 21.08910891]), array([29.16831683, 26.42574257, 23.12871287]), array([29.93069307, 27.23762376, 23.78217822]), array([32.76237624, 30.36633663, 26.8019802 ]), array([38.38613861, 35.74257426, 32.25742574]), array([62.72277228, 58.68316832, 55.11881188])]
TOTAL: [array([148.99009901, 148.12871287, 140.7029703 ]), array([154.83168317, 158.13861386, 155.98019802]), array([163.48514851, 167.21782178, 167.51485149]), array([149.57425743, 151.25742574, 151.6039604 ]), array([80.68316832, 79.28712871, 80.31683168]), array([34.94059406, 27.83168317, 27.55445545]), array([32.51485149, 21.89108911, 20.56435644]), array([39.78217822, 23.83168317, 20.47524752]), array([63.4950495 , 36.03960396, 27.16831683]), array([120.8019802 , 64.12871287, 38.28712871]), array([170.3960396 , 82.43564356, 30.26732673]), array([180.21782178, 73.99009901, 13.34653465]), array([182.35643564, 70.14851485, 9.53465347]), array([184.56435644, 70.37623762, 9.03960396]), array([186.47524752, 71.05940594, 8.67326733]), array([186.48514851, 70.51485149, 8.56435644]), array([187.76237624, 71.68316832, 9.28712871]), array([188.55445545, 72.38613861, 9.0990099 ]), array([189.63366337, 73.26732673, 10.33663366]), array([189.06930693, 72.35643564, 10. ]), array([190.1980198 , 72.52475248, 9.23762376]), array([190.23762376, 72.07920792, 8.74257426]), array([189.91089109, 71.68316832, 8.30693069]), array([190.20792079, 71.94059406, 8.26732673]), array([190.00990099, 71.9009901 , 8.27722772]), array([190.69306931, 72.20792079, 8.25742574]), array([191.82178218, 72.5049505 , 7.87128713]), array([193.12871287, 72.78217822, 7.66336634]), array([193.41584158, 72.63366337, 7.40594059]), array([193.66336634, 72.75247525, 7.72277228]), array([193.95049505, 73. , 8.08910891]), array([192.89108911, 71.86138614, 6.61386139]), array([194.11881188, 72.52475248, 7.0990099 ]), array([194.5049505 , 72.37623762, 7.44554455]), array([194.73267327, 72.10891089, 7.51485149]), array([194.36633663, 71.99009901, 7.14851485]), array([194.32673267, 72. , 7.30693069]), array([193.76237624, 71.62376238, 6.75247525]), array([194.41584158, 72.14851485, 7.24752475]), array([195.05940594, 72.88118812, 7.73267327]), array([194.30693069, 72.16831683, 7.34653465]), array([194.8019802 , 72.67326733, 7.38613861]), array([194.31683168, 72.27722772, 6.99009901]), array([194.17821782, 72.2970297 , 6.76237624]), array([195.23762376, 72.97029703, 7.06930693]), array([195.34653465, 72.79207921, 7.25742574]), array([194.46534653, 72.27722772, 7.02970297]), array([195.38613861, 72.83168317, 7.65346535]), array([195.10891089, 72.69306931, 7.55445545]), array([195.23762376, 72.79207921, 6.82178218]), array([195.12871287, 72.87128713, 7.18811881]), array([195.12871287, 72.64356436, 7.18811881]), array([195.56435644, 72.71287129, 7.22772277]), array([195.45544554, 72.56435644, 7.22772277]), array([196.41584158, 73.76237624, 8.1980198 ]), array([195.59405941, 73. , 7.82178218]), array([195.77227723, 72.8019802 , 7.79207921]), array([196.48514851, 73.22772277,
TOTAL: [array([12.8019802 , 7.52475248, 7.75247525]), array([12.17821782, 6.92079208, 7.22772277]), array([12.38613861, 7.08910891, 7.34653465]), array([15.82178218, 10.1980198 , 10.43564356]), 129, 9.89108911, 13.16831683]), array([183.18811881, 10.23762376, 13.47524752]), array([184.86138614, 9.55445545, 12.99009901]), array([186.4950495 , 8.62376238, 12.28712871]), array([189.04950495, 9.26732673, 12.66336634]), array([190.5049505 , 8.68316832, 11.96039604]), array([191.40594059, 7.86138614, 11.26732673]), array([193.27722772, 7.78217822, 11.05940594]), array([193.8019802 , 7.20792079, 10.36633663]), array([194.83168317, 6.91089109, 10.24752475]), array([195.91089109, 6.8019802 , 10.46534653]), array([196.57425743, 6.77227723, 10.71287129]), array([197.5049505 , 6.84158416, 10.97029703]), array([197.44554455, 6.44554455, 10.65346535]), array([197.58415842, 6.46534653, 10.38613861]), array([197.2970297 , 5.7029703 , 9.33663366]), array([197.67326733, 5.51485149, 9.16831683]), array([198.08910891, 5.17821782, 9.04950495]), array([197.73267327, 4.78217822, 8.62376238]), array([198.51485149, 5.41584158, 9.24752475]), array([198.55445545, 5.2970297 , 8.99009901]), array([198.35643564, 5. , 8.76237624]), array([198.58415842, 5.03960396, 9.0990099 ]), array([198.18811881, 4.74257426, 8.69306931]), array([197.89108911, 4.89108911, 8.89108911]), array([197.40594059, 4.72277228, 8.62376238]), array([196.2970297 ,
TOTAL: [array([20.8019802 , 15.78217822, 15.25742574]), array([19.00990099, 14.95049505, 14.59405941]), array([18.24752475, 14.4950495 , 14.27722772]), array([16.6039604 , 12.84158416, 12.94059406]), array([10.55445545, 6.87128713, 7.68316832]), array([11.15841584, 7.43564356, 8.13861386]), array([11.69306931, 7.75247525, 8.47524752]), array([14.51485149, 10.54455446, 10.68316832]), array([23.2970297 , 18.97029703, 19.53465347]), array([29.82178218, 25.06930693, 26.03960396]), array([29.98019802, 24.91089109, 25.78217822]), array([32.20792079, 26.9009901 , 27.1980198 ]), array([35.47524752, 29.57425743, 29.79207921]), array([39.61386139, 31.83168317, 31.25742574]), array([42.83168317, 32.54455446, 30.44554455]), array([83.9009901 , 32.84158416, 32.33663366]), array([105.07920792, 25.48514851, 27.12871287]), array([120.76237624, 19.66336634, 22.62376238]), array([133.52475248, 17.25742574, 21.4950495 ]), array([144.63366337, 15.64356436, 21.24752475]), array([157.11881188, 15.71287129, 22.59405941]), array([163.94059406, 13.07920792, 20.86138614]), array([170.74257426, 13.66336634, 20.8019802 ]), array([176.43564356, 12.62376238, 20.15841584]), array([181.28712871, 11.5049505 , 19.5049505 ]), array([184.12871287, 10.20792079, 18.43564356]), array([187.16831683, 9.63366337, 18.22772277]), array([188.2970297 , 8.02970297, 16.79207921]), array([188.81188119, 7.63366337, 16.28712871]), array([189.22772277, 7.63366337, 16.18811881]), array([189.43564356, 7.41584158, 16.2970297 ]), array([189.98019802, 7.82178218, 16.9009901 ]), array([189.27722772, 7. , 16.00990099]), array([189.51485149, 7.25742574, 16.31683168]), array([189.15841584, 7.22772277, 16.28712871]), array([189.06930693, 7.13861386, 16.44554455]), array([189.27722772, 7.27722772, 16.4950495 ]), array([189.34653465, 7.28712871, 16.25742574]), array([189.64356436, 7.73267327, 16.64356436]), array([189.05940594, 7.20792079, 16.47524752]), array([189.59405941, 7.61386139, 16.83168317]), array([189.41584158, 7.47524752, 16.71287129]), array([189.45544554, 7.47524752, 16.78217822]), array([188.85148515, 7.17821782, 16.79207921]), array([189.06930693, 7.26732673, 16.75247525]), array([188.9009901 , 7.2970297 , 16.56435644]), array([189.46534653, 7.81188119, 17.12871287]), array([189.26732673, 7.68316832, 17.11881188]), array([188.69306931, 7.12871287, 16.20792079]), array([188.6039604 , 7.04950495, 16.22772277]), array([189.12871287, 7.44554455, 16.7029703 ]), array([188.73267327, 7.52475248, 16.68316832]), array([188.81188119, 7.57425743, 16.79207921]), array([188.89108911, 7.4950495 , 16.5049505 ]), array([188.88118812, 7.38613861, 16.26732673]), array([189.01980198, 7.62376238, 16.54455446]), array([188.86138614, 7.47524752, 16.71287129]), array([188.58415842, 7.38613861, 16.51485149]), array([188.51485149, 7.27722772, 16.51485149]), array([188.35643564, 7.22772277, 16.36633663]), array([188.61386139, 7.66336634, 16.72277228]), array([187.95049505, 7.31683168, 16.28712871]), array([188.53465347, 7.86138614, 16.81188119]), array([188.24752475, 7.58415842, 16.7029703 ]), array([187.95049505, 7.18811881, 16.3960396 ]), array([188.23762376, 7.92079208, 16.9009901 ]), array([188.55445545, 8.18811881, 17.34653465]), array([188.17821782, 7.75247525, 16.84158416]), array([188.01980198, 8.15841584, 17.22772277]), array([187.64356436, 7.82178218, 17.20792079]), 38614, 101.67326733, 149.53465347]), array([ 8.52475248, 101.11881188, 149.21782178]), array([ 8.45544554, 101.13861386, 149.27722772]), array([ 8.17821782, 100.71287129, 148.55445545]), array([ 8.89108911, 101.56435644, 149.43564356]), array([ 8.08910891, 101.05940594, 149.07920792]), array([ 8.32673267, 101.57425743, 149.20792079]), array([ 7.85148515, 100.93069307, 148.62376238]), array([ 8.43564356, 100.61386139, 148.34653465]), array([ 7.99009901, 100.55445545, 148.13861386]), array([ 8.01980198, 100.20792079, 147.93069307]), array([ 8.63366337, 99.72277228, 146.8019802 ]), array([ 8.25742574, 98.2970297 , 145.32673267]), array([ 9.5049505 , 98.53465347, 144.51485149]), array([ 8.98019802, 96.21782178, 141.11881188]), array([ 10.01980198, 94.2970297 , 137.24752475]), array([ 13.14851485, 88.84158416, 125.86138614]), array([19.30693069, 62.61386139, 87.16831683]), array([24.57425743, 35.27722772, 42.64356436])]
i wanted the sum of all of the numbers, but i'm not getting that... what am i doing wrong here?
You can use PIL (Python Imaging Library) to load the image, convert it to an array with numpy.asarray() then use numpy.sum():
from PIL import Image
import numpy as np
image_path = '/Users/xxx/Desktop/test.png'
img = Image.open(image_path)
img.load()
data = np.asarray(img, dtype="int32")
data.sum()
Output:
1715779623
References:
numpy.asarray
numpy.sum

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)

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