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)
#5
In case of system of ordinary differential equations you will faced with necessity to solve
algebraic system of size m*s , where m -- the number of differential equations, s -- the number of stages in rk-method.

I slightly modified the code above to be able to handle systems of ODEs, but it still includes hardcoded
components (e.g. the number of stages, s=2), so improvements definitely needed:


import numpy as np
from scipy.optimize import fsolve , newton_krylov
import matplotlib.pyplot as plt



class ImpRK4 :
    def __init__(self, functions, t0, tf, dt, y0):
        self.functions = functions
        self.t0 = t0
        self.tf = tf
        self.dt = dt
        self.u0 = y0
        self.n = round((tf - t0) / dt)
        self.time  = np.linspace(self.t0, self.tf, self.n+1)
        self.u = np.array([self.u0  for i in range(self.n+1)])
        self.m = len(functions) # the number of equations (you can estimate it)
        
    def ode(self, t, u):
        return  np.array([func(t, u) for func in self.functions]) # u is an array of size `m`!
    
    def implicit_equations(self, variables, ti, ui):
        k1, k2 = variables[:self.m], variables[self.m:]
        f1 = -k1 + self.ode(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.ode(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 np.hstack([f1,f2]).ravel()
 
    def solve(self): 
        s = 2
        for i in range(len(self.time) - 1):
            res = fsolve(self.implicit_equations, np.random.rand(self.m * s), args=(self.time[i], self.u[i])) # s =2, the number of stadies of the num method
            k1, k2 = res[:self.m], res[self.m:]
            self.u[i + 1] = self.u[i] + self.dt / 2 * (k1 + k2)
        print('The system is solved.')



def main():
    func00 = lambda t,u : -10*(t-1)*u[0]
    func01 = lambda t,u : u[1] 
    func02 = lambda t,u : (1-u[0]**2)*u[1] - u[0]
    func0x = np.array([func00])
    func0 = np.array([func01,func02])
    t0 = 0. 
    tf = 2.      
    y01 = np.array([1., 1.])
    dt = 0.008 
    diffeq = ImpRK4(func0,t0,tf,dt,y01)    
    diffeq.solve()
    plt.plot(diffeq.u[:,1], diffeq.u[:,0])
    plt.show()
 
 
if __name__ == '__main__': 
    main()
Reply


Messages In This Thread
RE: Solve a system of non-linear equations in Python (scipy.optimize.fsolve) - by scidam - Aug-15-2018, 02:38 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to run a linear model by group in Python? Betty775522 0 590 Oct-18-2023, 07:09 PM
Last Post: Betty775522
  How to optimize analog gauge reader? kadink 0 805 May-19-2023, 08:58 PM
Last Post: kadink
  Using scipy.optimize: curve_fit ju21878436312 0 1,002 Sep-24-2022, 01:15 PM
Last Post: ju21878436312
  SOLVED: scipy.optimize.least_squares problem Skytter13 2 2,959 Mar-06-2022, 10:17 AM
Last Post: Skytter13
  Help with Scipy optimize for numerical problem Jbjbjb1 0 1,580 Jun-22-2021, 05:03 AM
Last Post: Jbjbjb1
  how to solve the 'NO SUCH DIRECTORY OR FILE' in pandas, python MohammedSohail 10 15,783 May-08-2020, 07:45 AM
Last Post: nnk
  scipy.optimize.basinhopping generates unstable output bb19x11 0 1,696 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,244 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,406 Aug-16-2018, 05:12 PM
Last Post: drudox
  Euler class for solve System of ODE drudox 0 2,980 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