![]() |
Error: _vhstack_dispatcher() takes 1 positional argument but 9 were given - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Error: _vhstack_dispatcher() takes 1 positional argument but 9 were given (/thread-37177.html) |
Error: _vhstack_dispatcher() takes 1 positional argument but 9 were given - alexfrol86 - May-08-2022 Hi! I have a system of 10 nonlinear ODE with 10 variables (y[0], y[1], ... y[9]), there are 10 initial conditions for y[i] for x=0 and 4 conditions for x = 6.804. I try to solve a boundary value problem using scipy.integrate.solve_bvp according to the following algorithm: https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.solve_bvp.html I get the error "_vhstack_dispatcher() takes 1 positional argument but 9 were given" though I tried to use np.dstack instead of np.hstack. Please help to fix the error. My code is below: import numpy as np from scipy.integrate import solve_bvp import matplotlib.pyplot as plt from scipy.constants import g def fun(x, y): return np.vstack((ptb / y[4] * y[5] / np.sqrt(y[5]**2 + y[6]**2) * function_deo(y[4], y[5], y[6], y[9], ptb, mqb, pes) - 1 / y[2]**2 + y[1]**2 / y[2], ptb / y[4] * y[6] / np.sqrt(y[5]**2 + y[6]**2) * function_deo(y[4], y[5], y[6], y[9], ptb, mqb, pes) - y[0] * y[1] / y[2]), y[0], y[1] / y[2], -mqb * function_deo(y[4], y[5], y[6], y[9], ptb, mqb, pes), y[6] * y[1] / y[2] - y[7], -y[5] * 2.0 * y[1] / y[2] + y[6] * y[0] / y[2] - y[8] * 1.0 / y[2], y[5] * (-2.0 / y[2]**3 + y[1]**2 / y[2]**2) + y[6] * (-y[0] * y[1]) / y[2] + y[8] * y[1] / y[2]**2, 0, ptb / y[4]**2 * (np.sqrt(y[5]**2 + y[6]**2)) * function_deo(y[4], y[5], y[6], y[9], ptb, mqb, pes)) #Implement evaluation of the boundary condition residuals: def bc(ya, yb): return np.array([ya[-0.03899746484, 1.05800280714, -0.30648856279, -0.605427, 1, -0.065851, 1.361805, 1.419320, -8.219418e-5, 0.6], yb[3.230021179060866e-17, 0.5519654178809713, 2.221001221001221, np.pi]]) #Define the initial mesh with 20 nodes: x = np.linspace(0.0, 6.804, 20) #To obtain solutions, different initial guesses for y are used. They are denoted by subscripts a and b. y_a = np.zeros((10, x.size)) y_b = np.zeros((10, x.size)) y_b[0] = 3.230021179060866e-17 res_a = solve_bvp(fun, bc, x, y_a) res_b = solve_bvp(fun, bc, x, y_b) x_plot = np.linspace(0, 6.804, 100) y_plot_a = res_a.sol(x_plot)[0] y_plot_b = res_b.sol(x_plot)[0] #plt.plot(x_plot, y_plot_a, label='y_a') #plt.plot(x_plot, y_plot_b, label='y_b') plt.legend() plt.xlabel("x") plt.ylabel("y") plt.show() RE: Error: _vhstack_dispatcher() takes 1 positional argument but 9 were given - deanhystad - May-09-2022 Please post error message and trace. The documentation for vstack says it takes one tuple. https://numpy.org/doc/stable/reference/generated/numpy.vstack.html You are providing 9 arguments. Let's count. (ptb / y[4] * y[5] / np.sqrt(y[5]**2 + y[6]**2) * function_deo(y[4], y[5], y[6], y[9], ptb, mqb, pes) - 1 / y[2]**2 + y[1]**2 / y[2], ptb / y[4] * y[6] / np.sqrt(y[5]**2 + y[6]**2) * function_deo(y[4], y[5], y[6], y[9], ptb, mqb, pes) - y[0] * y[1] / y[2]), #1 y[0], #2 y[1] / y[2], #3 -mqb * function_deo(y[4], y[5], y[6], y[9], ptb, mqb, pes), #4 y[6] * y[1] / y[2] - y[7], #5 -y[5] * 2.0 * y[1] / y[2] + y[6] * y[0] / y[2] - y[8] * 1.0 / y[2], #6 y[5] * (-2.0 / y[2]**3 + y[1]**2 / y[2]**2) + y[6] * (-y[0] * y[1]) / y[2] + y[8] * y[1] / y[2]**2, #7 0, #8 ptb / y[4]**2 * (np.sqrt(y[5]**2 + y[6]**2)) * function_deo(y[4], y[5], y[6], y[9], ptb, mqb, pes) #9 )I count 9. Number 1 is a doozy. Unlike the others, it is a tuple all by iteself, and it does this twice: ptb / y[4] * y[5] / np.sqrt(y[5]**2 + y[6]**2) * function_deo(y[4], y[5], y[6], y[9], ptb, mqb, pes) RE: Error: _vhstack_dispatcher() takes 1 positional argument but 9 were given - alexfrol86 - May-09-2022 Thanks, I corrected equations. Now, there are 10 arguments: def fun(x, y): return np.vstack(ptb / y[4] * y[5] / np.sqrt(y[5]**2 + y[6]**2) * function_deo(y[4], y[5], y[6], y[9], ptb, mqb, pes) - 1 / y[2]**2 + y[1]**2 / y[2], ptb / y[4] * y[6] / np.sqrt(y[5]**2 + y[6]**2) * function_deo(y[4], y[5], y[6], y[9], ptb, mqb, pes) - y[0] * y[1] / y[2], y[0], y[1] / y[2], -mqb * function_deo(y[4], y[5], y[6], y[9], ptb, mqb, pes), y[6] * y[1] / y[2] - y[7], -y[5] * 2.0 * y[1] / y[2] + y[6] * y[0] / y[2] - y[8] * 1.0 / y[2], y[5] * (-2.0 / y[2]**3 + y[1]**2 / y[2]**2) + y[6] * (-y[0] * y[1]) / y[2] + y[8] * y[1] / y[2]**2, 0, ptb / y[4]**2 * (np.sqrt(y[5]**2 + y[6]**2)) * function_deo(y[4], y[5], y[6], y[9], ptb, mqb, pes))Here are error message and trace: TypeError Traceback (most recent call last) <ipython-input-7-5b67a8d7cfc5> in <module>() 19 # y = myFn(R, x, cc) 20 ---> 21 res_a = solve_bvp(fun, bc, x, y_a) 22 res_b = solve_bvp(fun, bc, x, y_b) 23 2 frames <ipython-input-6-6f15cb472e1d> in fun(x, y) 5 -mqb * function_deo(y[4], y[5], y[6], y[9], ptb, mqb, pes), y[6] * y[1] / y[2] - y[7], -y[5] * 2.0 * y[1] / y[2] + y[6] * y[0] / y[2] - y[8] * 1.0 / y[2], 6 y[5] * (-2.0 / y[2]**3 + y[1]**2 / y[2]**2) + y[6] * (-y[0] * y[1]) / y[2] + y[8] * y[1] / y[2]**2, 0, ----> 7 ptb / y[4]**2 * (np.sqrt(y[5]**2 + y[6]**2)) * function_deo(y[4], y[5], y[6], y[9], ptb, mqb, pes)) <__array_function__ internals> in vstack(*args, **kwargs) TypeError: _vhstack_dispatcher() takes 1 positional argument but 10 were givenHow can I change the code to fix the error? Is there something I could use instead of vstack? RE: Error: _vhstack_dispatcher() takes 1 positional argument but 9 were given - deanhystad - May-09-2022 As far as vstack is concerned you are passing 10 numbers. It wants a tuple. How can you change 10 numbers into a tuple (list is acceptable too)? |