Python Forum
Finding all roots of a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding all roots of a function
#1
I have written the following snippet of code:

import numpy as np
import math as m
import cmath as cm
from scipy.optimize import minimize, root_scalar

def omega(args,x):
    
    N,p=args
    w=new_target(N,p) #an array
    
    old_spectrum=np.zeros(N)
    for i in range(N-1):
        old_spectrum[i]=N
    old_spectrum[N-1]=0
    
    sigma=0
    for i in range(N):
        sigma+=(w[i]*w[i])/(old_spectrum[i]-x)
    return 1+sigma

def omega_prime(args,x):
    
    N,p=args
    w=new_target(N,p)
    
    old_spectrum=np.zeros(N)
    for i in range(N-1):
        old_spectrum[i]=N
    old_spectrum[N-1]=0
    
    sigma=0
    for i in range(N):
        sigma+=(w[i]*w[i])/(old_spectrum[i]-x)*(old_spectrum[i]-x)
    return sigma

def new_spectrum(N,p,x0):

    roots=root_scalar(omega,fprime=omega_prime,args=(N,p),method='newton',x0=x0)
    return roots
that is meant to find the roots of a function omega(x) (there should be exactly N of them). However, calling for example

new_spectrum(3,1,0.1)
returns the error
Error:
TypeError: omega() takes 2 positional arguments but 3 were given
which I do not know how to fix. Any tips? Huh
Reply


Messages In This Thread
Finding all roots of a function - by thonpy - Apr-15-2021, 07:30 PM
RE: Finding all roots of a function - by bowlofred - Apr-15-2021, 08:33 PM
RE: Finding all roots of a function - by thonpy - Apr-15-2021, 10:51 PM
RE: Finding all roots of a function - by bowlofred - Apr-16-2021, 03:02 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding global extrema of oscillating function JoeRogan 0 1,661 Dec-22-2020, 01:49 AM
Last Post: JoeRogan
  pip unistall in alternate roots confminn 0 1,449 Aug-21-2020, 08:03 PM
Last Post: confminn
  Square and Cube roots. jarrod0987 2 5,465 Apr-13-2018, 09:30 PM
Last Post: casevh

Forum Jump:

User Panel Messages

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