Finding all roots of an equation in Python

2024/10/5 15:12:41

I have a function that I want to find its roots. I could write a program to figure out its roots but the point is, each time that I want to find the other root I should give it an initial value manually which I do not want to do that. I want to have all the roots in a list since I want to do some operations on the roots after finding them. This is my code:

import math
import scipy
import scipy.optimize
c = 5
alambda = 1
rho = 0.8
b = rho * c / alambda
def f(zeta):y = ((zeta**c)*(math.exp((alambda*b)*(1-zeta)))) - 1return yprint scipy.optimize.newton(f,  -1)
Answer

For integer c, the roots of the function are given by the Lambert W function, https://en.wikipedia.org/wiki/Lambert_W_function

from numpy import exp, pi
from scipy.special import lambertw
c = 3
alambda = 1.234
rho = 0.8
b = rho * c / alambda
def f(zeta):y = ((zeta**c)*(exp((alambda*b)*(1-zeta)))) - 1return y
def zeta_root(k, n):a = alambdareturn -c/(a*b) * lambertw(-a*b/c * exp(-(a*b+2j*pi*n)/c), k=k)
for k in range(-20, 20):# also n can be any integer; probably reproduces the same root set# as varying kzeta = zeta_root(k, 3)print("k={0}, zeta={1}, error={2}".format(k, zeta, abs(f(zeta))))

The equation has an infinite number of complex-valued roots. The k=0 and k=-1 roots which correspond to real branches of the Lambert W function may be real-valued.

For non-integer c the situation seems a bit more complicated due to the extra branch cut, although at least the real-valued positive roots should be captured.

https://en.xdnf.cn/q/120578.html

Related Q&A

defining matrix class in python

Define a class that abstracts the matrix that satisfies the following examples of practice

Why do round() and math.ceil() give different values for negative numbers in Python 3? [duplicate]

This question already has an answer here:What is the algorithmic difference between math.ceil() and round() when trailing decimal points are >= 0.5 in Python 3?(1 answer)Closed 6 years ago.Why do r…

getting the value from text file after the colon or before the colon in python

I only need the last number 25 so i can change it to int(). I have this text file:{"members": [{"name": "John", "location": "QC", "age": 25}…

How to remove the background of an object using OpenCV (Python) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

How to move zeros to the end of a list [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

My program only appends one user input to list when main is looped

This is part of my code for a guessing game. I want to count the guesses of a player, and then append their name and number of guesses to a list that is later written or appended to file. As of now, it…

How to split a python dictionary for its values on matching a key

my_dict1 = {a:1, chk:{b:2, c:3}, e:{chk:{f:5, g:6}} }I would like to loop through the dict recursively and if the key is chk, split it. Expected output:{a:1, b:2, e:{f:5}} {a:1, c:3, e:{f:5}} {a:1, b:2…

PyException: ImportError: No module named domreg

I am getting the below error while running this script ("from xml.dom import minidom") from chaquopy androi application python console. PyException: ImportError: No module named domreg

Plotting polynomial with given coefficients

Im trying to plot a polynomial with coefficients given in array:input: [an,a(n-1),...,a0] output: plot of polynomial anx^n + a(n-1)x^(n-1) + ... + a0I would like to use matplotlib polt() function so I…

grouping data using unique combinations

n my below data set, I need to find unique sequences and assign them a serial no ..DataSet :user age maritalstatus product A Young married 111 B young married 222 C young Single 111 D…