Related
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.
I have file test.txt (given below)
When I tried to print I get this error.
>>> print(content['mass'])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: string indices must be integers
But when
>>> print(content)
works ok, and show whole content (test.txt)
I need to print each list from the dict (like mass, temperature, etc.) and plot any 2 of them.
The content of the file is as follows:
{'mass': <Quantity [0.01066337, 0.05677534, 0.01377424, 0.06982872, 0.02665362] solar_mass>, 'temperature': <[values ... ] unit>}
full content file:
{'mass': <Quantity [0.01066337, 0.05677534, 0.01377424, 0.06982872, 0.02665362,
0.05278109, 0.01705602, 0.01321568, 0.08957726, 0.01409967,
0.02680093, 0.04329349, 0.04019967, 0.01633035, 0.09329267,
0.09963128, 0.04351358, 0.07735598, 0.02380135, 0.02242677,
0.03210654, 0.08636837, 0.02273623, 0.08528297, 0.07963256,
0.01457712, 0.07067075, 0.03672347, 0.08830624, 0.0152433 ,
0.02692899, 0.06610553, 0.09237312, 0.01470838, 0.06389854,
0.0367586 , 0.01463151, 0.05156612, 0.09061456, 0.02097923,
0.09724939, 0.01442857, 0.04258596, 0.09389821, 0.04595303,
0.01288873, 0.09977905, 0.08107244, 0.07191315, 0.01770304,
0.080393 , 0.02020766, 0.07458504, 0.01695185, 0.07393091,
0.03053152, 0.06589851, 0.02035323, 0.07618899, 0.03820138,
0.02377403, 0.07995724, 0.04570385, 0.02277475, 0.01927913,
0.02415907, 0.01820019, 0.04897082, 0.05023911, 0.02736103,
0.05118501, 0.05433449, 0.05497384, 0.0278658 , 0.0109119 ,
0.04103063, 0.05278137, 0.08110729, 0.02117512, 0.02299667,
0.02712591, 0.09289156, 0.07933846, 0.07723336, 0.07550681,
0.09140791, 0.05044324, 0.04048182, 0.0540212 , 0.06348042,
0.09851312, 0.06866901, 0.04383336, 0.06000141, 0.06026228,
0.09245798, 0.06652555, 0.08638328, 0.02669351, 0.03291363,
0.09498578, 0.07123481, 0.01734788, 0.06394011, 0.09080883,
0.0100093 , 0.05614301, 0.08463198, 0.08348169, 0.0695272 ,
0.012756 , 0.08004025, 0.042433 , 0.04228984, 0.05852984,
0.09786057, 0.09222483, 0.05554798, 0.02833528, 0.05775946,
0.04866883, 0.08642554, 0.05625195, 0.04145988, 0.03118205,
0.06150846, 0.03043129, 0.08140125, 0.0371213 , 0.08422178,
0.04447278, 0.05010288, 0.03013005, 0.07293837, 0.04287462,
0.07925233, 0.03597007, 0.06064241, 0.01506324, 0.01661757,
0.09065556, 0.01877721, 0.06181287, 0.01797611, 0.08751541,
0.06245063, 0.05643477, 0.09667603, 0.02816646, 0.05963242,
0.09114687, 0.08555436, 0.03389574, 0.03619514, 0.09107117,
0.08513269, 0.02986811, 0.06354284, 0.03119169, 0.02294978,
0.08086929, 0.07718417, 0.03123856, 0.03509633, 0.05837078,
0.02374362, 0.06490901, 0.05722849, 0.05868046, 0.01302157,
0.01879107, 0.07831512, 0.04077195, 0.02944109, 0.06633705,
0.09150163, 0.03014493, 0.07650354, 0.05166767, 0.0630273 ,
0.04772543, 0.0354305 , 0.02018854, 0.09507716, 0.02150221,
0.01615494, 0.06707219, 0.06575294, 0.05319418, 0.06806339,
0.084564 , 0.06717039, 0.07972018, 0.06372022, 0.03449891,
0.01062518, 0.0362224 , 0.06631852, 0.06794747, 0.07164968] solMass>,
'age': <Quantity [2.1892995 , 0.17371445, 6.37350567, 3.65552583, 0.26690645,
9.01274312, 9.5712514 , 4.12433204, 1.46197905, 6.69672667,
2.60662951, 0.07088385, 5.68504611, 2.14170039, 8.25031197,
1.05353222, 6.38848764, 4.91127832, 4.83363973, 1.06507071,
8.91887495, 9.04766237, 6.77889849, 9.2907544 , 9.85479148,
2.02304717, 3.2270554 , 9.36069897, 8.1881205 , 8.26326698,
4.24937976, 5.43265177, 1.23439319, 0.66178519, 7.30850369,
8.53579481, 0.58917658, 9.52785688, 8.78128074, 7.49955993,
6.07008997, 2.43334746, 9.15192905, 5.34271923, 1.03902797,
7.85382611, 9.07929136, 2.34516092, 4.0336831 , 6.9754011 ,
1.65513417, 5.78941649, 6.28545569, 7.14168201, 4.5552691 ,
6.21132475, 6.81369173, 1.74626285, 9.87199907, 3.68096459,
5.70725541, 8.50758903, 4.5958371 , 9.26271041, 9.21219332,
1.12404491, 8.46718892, 2.41106556, 9.5383536 , 2.845751 ,
5.48186263, 3.75960932, 9.46408245, 3.070541 , 5.14349759,
6.72742338, 2.056287 , 8.26982877, 3.23218309, 4.17451665,
5.77088167, 0.44298759, 9.65296989, 2.61752721, 9.13504304,
7.98373538, 5.71220366, 1.56916865, 1.97688569, 1.91507324,
9.17263502, 1.77701376, 4.55412645, 3.2627102 , 7.35668915,
2.50479129, 2.61109961, 6.37928278, 4.95950159, 3.05026315,
4.05514457, 0.08672433, 2.86609243, 1.35325359, 3.42583719,
4.26484596, 5.1162449 , 4.85971091, 4.44955176, 2.23475404,
3.31668938, 8.43803181, 0.72360653, 4.15507274, 0.87411426,
7.75213716, 4.6793922 , 7.48000041, 5.72125433, 1.31397429,
5.95206492, 8.58636847, 5.89774647, 3.26018283, 8.27834433,
4.00586172, 5.53028026, 4.79525327, 3.29217389, 4.87280459,
3.7335077 , 3.30432489, 3.60355157, 9.34549498, 6.49931121,
0.02732216, 6.94381144, 5.90679801, 3.16357294, 7.82961274,
0.0888311 , 7.9029332 , 4.33901607, 5.51927395, 6.53654137,
7.64875188, 3.09088845, 6.17206639, 4.5635939 , 1.12640628,
6.55772591, 0.8635738 , 8.86321633, 6.58032887, 0.57264661,
9.99113685, 0.83339148, 6.32905049, 9.21722392, 1.65153675,
4.29805491, 4.29669901, 5.58498365, 9.08169993, 6.42132207,
2.0412622 , 4.7042499 , 8.45610063, 5.53653754, 4.27634556,
0.98283569, 4.70564712, 4.85030912, 4.43987977, 2.20253426,
5.61759252, 7.5305766 , 9.28187195, 0.06004595, 0.8131256 ,
0.9932211 , 1.47332778, 1.44624897, 9.23842621, 5.34899458,
8.43612797, 9.4825792 , 5.94245213, 6.74007551, 6.20404917,
9.53766677, 4.60019672, 1.59700463, 5.52334977, 0.48772997,
0.59965089, 1.75955369, 9.56138032, 1.04756037, 3.91171088] Gyr>, 'temperature': <Quantity [ 416.26187575, 2386.00147856, 356.96775964, 1611.48635911,
1386.90459789, 843.45377056, 359.56621029, 397.98698621,
2615.60719944, 356.35902942, 696.73157636, 2452.07321025,
738.22537858, 532.80565595, 2677.89432543, 2778.16848831,
769.31944774, 2151.4339527 , 533.16952309, 812.50564763,
548.1938236 , 2513.85620097, 471.13610308, 2481.15025828,
2297.11805108, 508.33419459, 1725.63923115, 602.54069179,
2572.14755174, 351.02698579, 599.78662322, 1332.30425907,
2664.27485445, 715.27835447, 1157.7390599 , 617.72498514,
742.04731718, 811.52591929, 2633.86501557, 434.9693978 ,
2741.4568123 , 480.34396454, 686.744989 , 2686.61464287,
1416.43680536, 325.36847056, 2782.35910147, 2382.15547751,
1761.66710638, 401.58797383, 2375.15628236, 456.84485622,
1960.34206079, 389.02690439, 1909.10788514, 582.38959148,
1253.47061676, 655.9560554 , 2071.71727096, 807.75308367,
508.31655569, 2318.63547143, 880.95939104, 431.89009998,
389.83994036, 840.95773606, 386.29226905, 1148.60436432,
789.50516397, 686.72298902, 930.60237286, 1107.69977761,
869.80628035, 678.32474609, 337.22647863, 719.64135682,
1299.92452458, 2354.81635642, 560.35816002, 546.26491365,
550.48207955, 2728.27478396, 2277.28716021, 2166.3338627 ,
2028.72650099, 2646.76330972, 907.07025528, 1112.10342809,
1347.40455825, 1567.69512142, 2762.24673578, 1792.33516425,
850.53825842, 1270.41170997, 1025.53432709, 2663.39115409,
1561.35583681, 2513.28960355, 568.35332647, 764.20122094,
2704.05935833, 2789.71414994, 508.66726781, 1748.71166657,
2636.13556182, 335.90296155, 1040.7441679 , 2460.87911061,
2429.70209659, 1757.52438765, 416.53092797, 2322.72218894,
1472.63533413, 847.29923108, 1827.91530672, 2751.58852168,
2658.86923054, 932.96214473, 567.10026795, 1608.78268184,
867.7687031 , 2515.42004061, 1004.56537506, 898.84784151,
546.83257922, 1228.64851226, 599.80749478, 2363.87785858,
818.03791184, 2448.3783556 , 917.2780149 , 1062.48713029,
678.01980883, 1699.94999111, 755.65919285, 2918.00441238,
640.80652735, 1097.67818411, 455.0697186 , 374.90100478,
2919.23401673, 400.97259608, 1221.76007185, 432.21978007,
2547.58809003, 1092.25936545, 1224.11062078, 2732.25980845,
603.32307638, 1729.78873919, 2641.86202827, 2529.80624052,
573.36519488, 653.1220142 , 2687.00147158, 2476.83545148,
1067.09489353, 1186.50146867, 531.10784434, 723.71093433,
2352.11261182, 2145.45144949, 610.27363595, 585.76505168,
1021.59377921, 695.08881158, 1326.15404201, 933.61651678,
1066.31164386, 391.34211341, 737.96295138, 2213.58596372,
780.74166508, 625.66321926, 1604.59456116, 2647.29238788,
546.95277285, 2091.59457771, 2636.78384296, 1950.52489595,
1484.64961828, 1018.81913319, 690.23845654, 2707.05845721,
485.87384115, 361.21491645, 1197.21167289, 1289.88457879,
916.51388128, 1367.72546396, 2459.51962535, 1426.2466385 ,
2345.90602429, 1233.03426045, 1420.10028775, 603.3961445 ,
979.86162211, 1167.48046769, 1996.95643785, 1759.6332142 ] K>, 'gravity': <Dex [4.43710182, 4.965187 , 4.5960181 , 5.43935524, 4.77599392,
5.36645319, 4.71685808, 4.5631087 , 5.28746289, 4.60912155,
4.92292931, 4.73522324, 5.1859695 , 4.65413841, 5.27461287,
5.24749696, 5.23839782, 5.38287012, 4.88123687, 4.7978376 ,
5.06587143, 5.31138048, 4.86481419, 5.31825235, 5.35742248,
4.59283847, 5.42541489, 5.14719842, 5.29932345, 4.65409788,
4.94470474, 5.45260548, 5.27588952, 4.558575 , 5.45716882,
5.14497198, 4.55072716, 5.35471333, 5.28628722, 4.8221192 ,
5.25810337, 4.59295229, 5.23605709, 5.27209735, 5.17053945,
4.56689609, 5.24704209, 5.33165214, 5.43725938, 4.72922773,
5.32972958, 4.79516682, 5.41802867, 4.70686194, 5.42131465,
5.02688942, 5.46371964, 4.76247288, 5.39988976, 5.13962788,
4.8853597 , 5.35351361, 5.25370809, 4.87296808, 4.78071025,
4.8384147 , 4.7483346 , 5.25428576, 5.33904289, 4.93784239,
5.32980831, 5.34280863, 5.39371378, 4.95096254, 4.47277291,
5.20372326, 5.28786486, 5.34553068, 4.80229852, 4.85641286,
4.95883889, 5.21125042, 5.36097707, 5.37256552, 5.40842553,
5.28281229, 5.32288574, 5.12740498, 5.29872025, 5.35763516,
5.25223846, 5.37453344, 5.22803649, 5.3843215 , 5.43732776,
5.27730831, 5.39646086, 5.31128667, 4.94587991, 5.04578314,
5.26737666, 4.83639934, 4.69458324, 5.33220018, 5.28474856,
4.42491336, 5.38056442, 5.32188551, 5.32767791, 5.39734114,
4.53849927, 5.35272672, 5.0955432 , 5.20069748, 5.2635995 ,
5.25519359, 5.27922436, 5.3892347 , 4.98281909, 5.29584768,
5.30308228, 5.31102091, 5.38680781, 5.17573485, 5.04666149,
5.4074127 , 5.02195334, 5.34278556, 5.11902518, 5.32457575,
5.22414132, 5.28793297, 5.00070561, 5.45733048, 5.22986539,
4.50000751, 5.12582843, 5.42935598, 4.62285578, 4.69846184,
4.85026837, 4.76323393, 5.41555175, 4.7319106 , 5.3042116 ,
5.45138916, 5.35223793, 5.26047562, 4.97242888, 5.29769635,
5.28395234, 5.29203278, 5.09791617, 5.12790729, 5.24523198,
5.31921069, 4.93450972, 5.44844625, 5.04968515, 4.82351901,
5.34430774, 5.38237638, 5.03749854, 5.11934352, 5.41263898,
4.84788074, 5.43744925, 5.41320623, 5.40904518, 4.55653996,
4.70295737, 5.3709984 , 5.18855237, 4.9959135 , 5.38151548,
5.28240382, 5.02444936, 5.39593188, 4.7121672 , 5.27456645,
5.18684626, 5.05459057, 4.7523688 , 5.26671687, 4.8279413 ,
4.68526605, 5.48585731, 5.45572803, 5.35943445, 5.46878134,
5.32285266, 5.44626453, 5.33487488, 5.44204577, 4.96778534,
4.39178975, 5.07501785, 5.4823541 , 5.32855791, 5.43496633] dex(cm / s2)>, 'luminosity': <Dex [-6.54711661, -3.31607959, -6.86248067, -4.381449 , -4.39742816,
-5.55510124, -6.87861382, -6.65725929, -3.28000489, -6.8685772 ,
-5.73806679, -3.15642613, -5.72497634, -6.15085019, -3.20879829,
-3.08971297, -5.67130648, -3.77869081, -6.21263909, -5.42361876,
-6.21936147, -3.38884433, -6.43139063, -3.42402442, -3.62712937,
-6.22047683, -4.24385183, -6.07733083, -3.3271961 , -6.90594949,
-6.01797205, -4.74899511, -3.22321687, -5.58886949, -5.01236152,
-6.03170168, -5.51942472, -5.62053792, -3.26178624, -6.56311215,
-3.13354733, -6.32337469, -5.87454898, -3.19763512, -4.51896506,
-7.0225412 , -3.08617018, -3.53016605, -4.21292318, -6.68194926,
-3.53714714, -6.46649762, -3.99162757, -6.733638 , -4.04494859,
-6.09690661, -4.8674517 , -5.80211144, -3.8682314 , -5.54462774,
-6.30025681, -3.60514918, -5.42935759, -6.59095371, -6.74941265,
-5.37202798, -6.75730693, -4.93888066, -5.66396037, -5.76911782,
-5.36049636, -5.04504737, -5.51129412, -5.79566146, -6.9386513 ,
-5.77776669, -4.72492306, -3.56399378, -6.09817542, -6.16062514,
-6.17801414, -3.11465238, -3.64734012, -3.75730692, -3.91705852,
-3.24603817, -5.40442467, -4.95138337, -4.66342371, -4.38901353,
-3.10942375, -4.13912096, -5.4831754 , -4.80545391, -5.22876444,
-3.22468225, -4.41449109, -3.38886772, -6.11645229, -5.61137395,
-3.17674537, -2.8170494 , -6.24545807, -4.17064934, -3.25764594,
-6.93694802, -5.17684481, -3.44491303, -3.47885876, -4.19060971,
-6.56817022, -3.60084444, -4.41079121, -5.4783194 , -4.06357302,
-3.12177758, -3.23049961, -5.38037703, -6.13137589, -4.32345767,
-5.47733405, -3.38707704, -5.24384505, -5.35946433, -6.21719022,
-4.87579054, -6.04212794, -3.55267083, -5.51445944, -3.45857056,
-5.34174391, -5.09771833, -5.81223993, -4.28874553, -5.70026866,
-2.35593355, -5.95892139, -5.09979207, -6.42928691, -6.79814261,
-2.6467564 , -6.69380454, -4.89155381, -6.55031118, -3.35257284,
-5.11776702, -4.86454502, -3.14429899, -6.01595667, -4.18544193,
-3.25153371, -3.36267718, -6.14961411, -5.92533787, -3.18368679,
-3.42883108, -4.96202439, -4.9633597 , -6.27078878, -5.64019033,
-3.56576012, -3.78405704, -6.01633689, -6.11851715, -5.22451544,
-5.71979944, -4.74979851, -5.39028779, -5.14409858, -6.68613937,
-5.57280601, -3.71194278, -5.62435596, -5.95705735, -4.35332226,
-3.24467801, -6.20912901, -3.84584977, -2.93013877, -3.92950563,
-4.43707326, -5.08864745, -5.70709839, -3.17410724, -6.36487176,
-6.86190098, -4.96181435, -4.81065936, -5.40014859, -4.70692963,
-3.44756316, -4.61734128, -3.56751066, -4.88890729, -4.43576264,
-5.8584977 , -5.16727488, -5.00693922, -3.9099617 , -4.21418168] dex(solLum)>, 'radius': <Quantity [0.10367149, 0.12970977, 0.09806314, 0.08334739, 0.11069133,
0.0785519 , 0.0947761 , 0.09972202, 0.11253834, 0.09770918,
0.09378137, 0.14773965, 0.08453269, 0.09968327, 0.11695712,
0.12455847, 0.08275767, 0.0936908 , 0.09294707, 0.0986552 ,
0.08713584, 0.10789239, 0.09248596, 0.10637093, 0.09825089,
0.10104164, 0.08504679, 0.08464685, 0.11061464, 0.09637859,
0.09190295, 0.07988967, 0.1158526 , 0.10563037, 0.07816749,
0.08489901, 0.10637899, 0.07869662, 0.11373903, 0.0932158 ,
0.12170452, 0.1005596 , 0.0821066 , 0.11768421, 0.09217953,
0.09805614, 0.12473543, 0.10191273, 0.08447081, 0.09530858,
0.10160875, 0.09444041, 0.08815973, 0.09569271, 0.08732745,
0.08905792, 0.07887032, 0.09806598, 0.09133991, 0.08690757,
0.0924379 , 0.09891271, 0.08369776, 0.09158276, 0.09361303,
0.0977509 , 0.09445411, 0.08667936, 0.07912382, 0.0931945 ,
0.08120554, 0.08227846, 0.07766363, 0.09268461, 0.10043724,
0.08366547, 0.0864166 , 0.1005401 , 0.095778 , 0.09392486,
0.0907937 , 0.12508328, 0.09765291, 0.09454306, 0.08999446,
0.11469276, 0.08126817, 0.09063489, 0.08625252, 0.08719552,
0.12321906, 0.08885533, 0.08431779, 0.08225772, 0.07737114,
0.11595455, 0.08530861, 0.10791331, 0.09144224, 0.09012625,
0.11898958, 0.16845583, 0.09814612, 0.09019986, 0.11397261,
0.10157242, 0.07998144, 0.10551523, 0.10407616, 0.08716712,
0.10071855, 0.09905593, 0.09645754, 0.08536378, 0.09373547,
0.12243711, 0.11567444, 0.07851749, 0.09029243, 0.08965263,
0.08158881, 0.1079726 , 0.07934098, 0.08690498, 0.08786349,
0.08096173, 0.0894523 , 0.1010277 , 0.08772868, 0.10493585,
0.08532489, 0.0844292 , 0.09111147, 0.0836555 , 0.08295352,
0.26205706, 0.08588099, 0.0784086 , 0.09935163, 0.09563672,
0.1877203 , 0.09432879, 0.08043013, 0.09581965, 0.10950281,
0.07766852, 0.0827943 , 0.1210171 , 0.09109304, 0.09096926,
0.11437898, 0.10932089, 0.08619882, 0.0859307 , 0.11914385,
0.10616047, 0.09732112, 0.07872167, 0.08755052, 0.09706002,
0.10049042, 0.09357949, 0.08895979, 0.08551933, 0.07836048,
0.09607737, 0.08046654, 0.07748946, 0.07892331, 0.09974402,
0.10073852, 0.09569109, 0.08492312, 0.09065687, 0.08662187,
0.11480539, 0.08872791, 0.09196292, 0.16573688, 0.09601582,
0.09229225, 0.09223402, 0.09873764, 0.11909924, 0.09388137,
0.09571571, 0.07767059, 0.07943153, 0.07967868, 0.07969055,
0.10536442, 0.08108227, 0.10053525, 0.07934334, 0.10084415,
0.10893685, 0.09110087, 0.077487 , 0.09319972, 0.08454794] solRad>}
Try this:
import json
json_data = json.loads(content)
print(json_data["mass"])
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)
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)).
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