Dear Oscar Really you are saving me in my project. I have one important issue to ask. Please let me know. My problem in fitting has been done, based on your brilliant suggestion. While
import numpy as np import sympy as sp from scipy.optimize import minimize, curve_fit L_vals = np.array([0.299, 0.295, 0.290, 0.284, 0.279, 0.273, 0.268, 0.262, 0.256, 0.250]) K_vals = np.array([2.954, 3.056, 3.119, 3.163, 3.215, 3.274, 3.351, 3.410, 3.446, 3.416]) VA_vals = np.array([0.919, 0.727, 0.928, 0.629, 0.656, 0.854, 0.955, 0.981, 0.908, 0.794]) gamma, alpha, beta, eta, L, K, VA = sp.symbols('gamma, alpha, beta, eta, L, K, VA') Vi_est = gamma - (1 / eta) * sp.log(alpha * L ** -eta + beta * K ** -eta) # Note that the first entry in the args tuple is a 4 tuple # That is needed to unpack the arguments from an array f_Vi_est = sp.lambdify(((gamma, alpha, beta, eta), L, K, VA), Vi_est) def f(param): Vi_est_vals = f_Vi_est(param, L_vals, K_vals, VA_vals) return np.sum((np.log(VA_vals) - Vi_est_vals) ** 2) bnds = [(1, np.inf), (0, 1), (0, 1), (-1, np.inf)] x0 = (1, 0.01, 0.98, 1) result = minimize(f, x0, bounds=bnds) print(result.message) print(result.x[0], result.x[1], result.x[2], result.x[3]) 1. f_Vi_est = sp.lambdify(((gamma, alpha, beta, eta), L, K, VA), Vi_est) 2. Vi_est_vals = f_Vi_est(param, L_vals, K_vals, VA_vals) These two lines really make my project impossible. Since based on what the user is sending in the code I must make these two lines dynamically. I mean how can I make them independent. I just have Vi_est(as any string resulted from sympy computation) and a list or dict of [gamma, alpha, beta, eat] and [L, K, VA] that are changing in each iteration. How these lines can be written to make it dynamic. It is impossible to do the job this way. Regards, Zohreh Karimzadeh Skype Name 49a52224a8b6b38b Twitter Account @zohrehkarimzad1 +989102116325 ((((((((((((((((Value Water))))))))))))))) On Mon, Aug 22, 2022 at 6:53 PM Oscar <oscar.j.benja...@gmail.com> wrote: > On Saturday, 20 August 2022 at 16:09:14 UTC+1 peter.st...@gmail.com wrote: > >> Most welcome! >> Like I said, passing args = (L, K , VA) to your function F may take a >> little 'playing around'. >> > > This is one way to do it: > > import numpy as np > import sympy as sp > from scipy.optimize import minimize, curve_fit > > L_vals = np.array([0.299, 0.295, 0.290, 0.284, 0.279, 0.273, 0.268, 0.262, > 0.256, 0.250]) > K_vals = np.array([2.954, 3.056, 3.119, 3.163, 3.215, 3.274, 3.351, 3.410, > 3.446, 3.416]) > VA_vals = np.array([0.919, 0.727, 0.928, 0.629, 0.656, 0.854, 0.955, > 0.981, 0.908, 0.794]) > > gamma, alpha, beta, eta, L, K, VA = sp.symbols('gamma, alpha, beta, eta, > L, K, VA') > > Vi_est = gamma - (1 / eta) * sp.log(alpha * L ** -eta + beta * K ** -eta) > > # Note that the first entry in the args tuple is a 4 tuple > # That is needed to unpack the arguments from an array > f_Vi_est = sp.lambdify(((gamma, alpha, beta, eta), L, K, VA), Vi_est) > > def f(param): > Vi_est_vals = f_Vi_est(param, L_vals, K_vals, VA_vals) > return np.sum((np.log(VA_vals) - Vi_est_vals) ** 2) > > > bnds = [(1, np.inf), (0, 1), (0, 1), (-1, np.inf)] > x0 = (1, 0.01, 0.98, 1) > > result = minimize(f, x0, bounds=bnds) > > print(result.message) > print(result.x[0], result.x[1], result.x[2], result.x[3]) > # Output: > # CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL > # 1.0 0.5587146900213987 0.9371431014439708 5.87304177555323 > > -- > Oscar > > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sympy+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/0d41fe02-9bba-41e8-83d0-5971f646475dn%40googlegroups.com > <https://groups.google.com/d/msgid/sympy/0d41fe02-9bba-41e8-83d0-5971f646475dn%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CA%2B1XYLNSi9GkZE-YxjOAMBr10i1cRiTHRvAiNrvcGPw%2BxyEc-Q%40mail.gmail.com.