Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Solve system of equations
#17
I see,
this looks like as if the constrained optimization would probably do better (since L,C and R cannot be negative)
import math
from scipy.optimize import least_squares


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 = [0, 0, 0]
    out = [0, 0, 0]
    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 = [0, 0, 0]
    Aj = [0, 0, 0]
    Br = [0, 0, 0]
    Bj = [0, 0, 0]
    equ = [0, 0, 0]

    # 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 = [0.3e-6, 1e-12, 1]
res = least_squares(f, (LCR0), bounds=((0, 0, 0), (1.0e-6, 1.0e-10, 2)))
print(res)
Let me know if this is closer to the expected solution.
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,838 Dec-26-2021, 07:04 PM
Last Post: HoangF
Question Help with code to solve polynomials equations hiviera 1 1,816 Jul-31-2021, 01:56 AM
Last Post: hiviera
  Difference between os.system("clear") and os.system("cls") chmsrohit 7 16,731 Jan-11-2021, 06:30 PM
Last Post: ykumar34
  How to solve equations, with groups of variables and or constraints? ThemePark 0 1,699 Oct-05-2020, 07:22 PM
Last Post: ThemePark
  Solve a system of linear equations with binary variables lopeslimagabriel 3 2,536 Sep-24-2020, 07:09 AM
Last Post: scidam
  How to solve difficult non-linear equations? shreeniket 3 2,413 Apr-23-2020, 01:36 AM
Last Post: shreeniket
Question Difference between Python's os.system and Perl's system command Agile741 13 6,928 Dec-02-2019, 04:41 PM
Last Post: Agile741
  System of 3 non-linear equations in 3 unknowns (how-to-solve?) samsonite 2 3,594 Mar-23-2019, 10:14 AM
Last Post: samsonite
  Beginner: System of Equations Mahdi1994 2 2,546 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