Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Solve system of equations
#11
I've tried with fsolve.
Here is my code:
import math
import numpy as np
from scipy.optimize import fsolve

def f(LCR):
    #variables
    L  = LCR[0]
    Cs = LCR[1]
    Rs = LCR[2]
    
    # global constants
    Pi = math.pi
    Rin = 10.0
    Cl = 3.0e-12
    
    # measurement-related constants
    f0 = 130e6
    f1 = 140e6
    f2 = 150e6
    omg = [None]*3
    out = [None]*3
    omg[0] = 2*Pi*f0
    omg[1] = 2*Pi*f1
    omg[2] = 2*Pi*f2
    out[0] = 6.658482971121064
    out[1] = 25.89226207742419
    out[2] = 7.013950018010094

    # A and B arrays
    Ar = [None]*3
    Aj = [None]*3
    Br = [None]*3
    Bj = [None]*3
    equ = [None]*3
    
    # equation building
    for i in range(3):
        Ar[i] = 1 - omg[i]**2*Cl*L
        Aj[i] = omg[i]*Cs*Rs
        Br[i] = 1 - omg[i]**2*(L*(Cs + Cl) + Cl*Cs*Rin*Rs)
        Bj[i] = omg[i]*(Cs*Rs + Cl*Rin + Cl*Rs - omg[i]**2*Cl*Cs*Rin*L)

        equ[i] = out[i] - math.sqrt(Ar[i]*Ar[i] + Aj[i]*Aj[i])/math.sqrt(Br[i]*Br[i] + Bj[i]*Bj[i])

    return equ

LCR0 = np.array([0.3e-6, 1e-12, 1])
LCR = fsolve(f, LCR0)
LCR
Here is output:
Output:
Warning (from warnings module): File "/home/pavel47/.local/lib/python3.8/site-packages/scipy/optimize/minpack.py", line 175 warnings.warn(msg, RuntimeWarning) RuntimeWarning: The iteration is not making good progress, as measured by the improvement from the last five Jacobian evaluations. >>> LCR array([ 3.10756682e-07, 1.02921301e-12, -4.22520679e+00]) >>>
While the first two values are correct, the third is completely wrong.
Any suggestions ?
Thanks.
Reply


Messages In This Thread
Solve system of equations - by Sancho_Pansa - Oct-22-2020, 06:53 AM
RE: Solve system of equations - by Gribouillis - Oct-22-2020, 09:41 AM
RE: Solve system of equations - by metulburr - Oct-22-2020, 09:44 AM
RE: Solve system of equations - by Sancho_Pansa - Oct-22-2020, 09:57 AM
RE: Solve system of equations - by Gribouillis - Oct-22-2020, 11:37 AM
RE: Solve system of equations - by Sancho_Pansa - Oct-22-2020, 12:02 PM
RE: Solve system of equations - by Sancho_Pansa - Oct-22-2020, 12:17 PM
RE: Solve system of equations - by Gribouillis - Oct-22-2020, 12:09 PM
RE: Solve system of equations - by Gribouillis - Oct-22-2020, 12:55 PM
RE: Solve system of equations - by Sancho_Pansa - Oct-22-2020, 01:12 PM
RE: Solve system of equations - by Sancho_Pansa - Oct-23-2020, 09:21 AM
RE: Solve system of equations - by Sancho_Pansa - Oct-23-2020, 03:25 PM
RE: Solve system of equations - by Gribouillis - Oct-24-2020, 08:55 PM
RE: Solve system of equations - by Sancho_Pansa - Oct-26-2020, 08:22 AM
RE: Solve system of equations - by Askic - Oct-26-2020, 10:05 AM
RE: Solve system of equations - by Sancho_Pansa - Oct-26-2020, 10:22 AM
RE: Solve system of equations - by Askic - Oct-26-2020, 10:35 AM
RE: Solve system of equations - by Sancho_Pansa - Oct-26-2020, 10:55 AM
RE: Solve system of equations - by Askic - Oct-26-2020, 05:28 PM
RE: Solve system of equations - by Sancho_Pansa - Oct-27-2020, 08:15 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Heart how to solve complex equations in python HoangF 3 2,801 Dec-26-2021, 07:04 PM
Last Post: HoangF
Question Help with code to solve polynomials equations hiviera 1 1,792 Jul-31-2021, 01:56 AM
Last Post: hiviera
  Difference between os.system("clear") and os.system("cls") chmsrohit 7 16,633 Jan-11-2021, 06:30 PM
Last Post: ykumar34
  How to solve equations, with groups of variables and or constraints? ThemePark 0 1,684 Oct-05-2020, 07:22 PM
Last Post: ThemePark
  Solve a system of linear equations with binary variables lopeslimagabriel 3 2,501 Sep-24-2020, 07:09 AM
Last Post: scidam
  How to solve difficult non-linear equations? shreeniket 3 2,389 Apr-23-2020, 01:36 AM
Last Post: shreeniket
Question Difference between Python's os.system and Perl's system command Agile741 13 6,829 Dec-02-2019, 04:41 PM
Last Post: Agile741
  System of 3 non-linear equations in 3 unknowns (how-to-solve?) samsonite 2 3,577 Mar-23-2019, 10:14 AM
Last Post: samsonite
  Beginner: System of Equations Mahdi1994 2 2,514 Mar-19-2018, 12:37 AM
Last Post: Tiskolin

Forum Jump:

User Panel Messages

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