Python Forum
Solve a system of non-linear equations in Python (scipy.optimize.fsolve)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Solve a system of non-linear equations in Python (scipy.optimize.fsolve)
#1
I'm trying to solve this system of non linear equations using scipy.optimize.fsolve , I took this from an example in one other post [here][1]

my system of equation is the follow :

    for i in range(len(self.time)-1):
            
                def equations(variable):
                    k1,k2 = variable 
                    f1 = -k1 + self.f(self.time[i]+ (0.5+np.sqrt(3)/6)* self.dt , self.u[i]+0.25*self.dt* k1+ (0.25+ np.sqrt(3)/6)*self.dt*k2) 
                    f2 = -k2 + self.f(self.time[i]+ (0.5-np.sqrt(3)/6)* self.dt , self.u[i]+(0.25-np.sqrt(3)/6)*self.dt *k1 + 0.25*self.dt* k2)
                    return (f1,f2)
                            
                    
                k1,k2 = fsolve(equations,(5,5))
when I run the code I got :

    TypeError: fsolve: there is a mismatch between the input and output shape of the 'func' argument 'equations'.Shape should be (2,) but it is (2, 1).
I don't know why this mismatch and how to fix it ..
I tried :

 for i in range(len(self.time)-1):
                ui = self.u[i]
                ti = self.time[i]
    
                def equations(variable):
                    k1,k2 = variable 
                    f1 = -k1 + self.f(ti+ (0.5+np.sqrt(3)/6)* self.dt , ui+0.25*self.dt* k1+ (0.25+ np.sqrt(3)/6)*self.dt*k2) 
                    f2 = -k2 + self.f(ti+ (0.5-np.sqrt(3)/6)* self.dt , ui+(0.25-np.sqrt(3)/6)*self.dt *k1 + 0.25*self.dt* k2)
                    return (f1,f2)
                            
                    
                k1,k2 = fsolve(equations,(1,1))
how can I fix this mismatch ?
Reply


Messages In This Thread
Solve a system of non-linear equations in Python (scipy.optimize.fsolve) - by drudox - Aug-13-2018, 04:44 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to run a linear model by group in Python? Betty775522 0 574 Oct-18-2023, 07:09 PM
Last Post: Betty775522
  How to optimize analog gauge reader? kadink 0 781 May-19-2023, 08:58 PM
Last Post: kadink
  Using scipy.optimize: curve_fit ju21878436312 0 997 Sep-24-2022, 01:15 PM
Last Post: ju21878436312
  SOLVED: scipy.optimize.least_squares problem Skytter13 2 2,905 Mar-06-2022, 10:17 AM
Last Post: Skytter13
  Help with Scipy optimize for numerical problem Jbjbjb1 0 1,577 Jun-22-2021, 05:03 AM
Last Post: Jbjbjb1
  how to solve the 'NO SUCH DIRECTORY OR FILE' in pandas, python MohammedSohail 10 15,648 May-08-2020, 07:45 AM
Last Post: nnk
  scipy.optimize.basinhopping generates unstable output bb19x11 0 1,684 Mar-09-2020, 04:07 PM
Last Post: bb19x11
  How to build linear regression by implementing Gradient Descent using only linear alg PythonSpeaker 1 2,233 Dec-01-2019, 05:35 PM
Last Post: Larz60+
  class for ODE, scipy how to switch from fsolve to newton or newton_krylov drudox 0 2,386 Aug-16-2018, 05:12 PM
Last Post: drudox
  Euler class for solve System of ODE drudox 0 2,968 Jul-08-2018, 09:31 PM
Last Post: drudox

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020