Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Can't find a way to solve this nonlinear equation
#1
I want to solve this nonlinear equation. I've tried to use sympy and scipy methods. But I thing something wrong with syntax.
So how to write this code correctly?
import sympy
import numpy as np
import math
from scipy.optimize import fsolve
def PoissonEqs(p,m,alpha):
    if m==0:
        return math.exp(-p)-alpha
    else:
        return math.exp(m*math.log(p)-p-math.log(math.gamma(m+1)))+PoissonEqs(p,m-1,alpha)
#p=fsolve(PoissonEqs(p,0,0.05,1),1)
    p=sympy.Symbol('p')
p1=sympy.nsolve(math.exp(-p)-0.05,p,1)
print(p1)
Quote
#2
Well seems fsolve is not a good option for this equation as I need to set a p0 guess with a certain precision.
More over I have the same problem as with vba in excel when m, that is factorial, more than 170. it dosent work.
It there some data types kinda long long in c++ for python?
Can it be solved with nsolve?
import sympy
import numpy as np
import math
import scipy.optimize 
def PoissonEqs(p,m,alpha):
    if m==0:
        return math.exp(-p)-alpha
    else:
        return math.exp(m*math.log(p)-p-math.log(math.gamma(m+1)))+PoissonEqs(p,m-1,alpha)
def func2(p):
    m=1; alpha=0.05
    return PoissonEqs(p,m,alpha)
#sol = scipy.optimize.bisect(func2, 0.0000000001, 1000)
#sol2=scipy.optimize.fsolve(func2,10)
from sympy.abc import p
sol2=sympy.nsolve(math.exp(-p)-0.05,1)
print(sol2)
Quote
#3
Not sure about how to interpret this, but at least it is better to drop using recursion in such cases; It might be quite slow, and even cause sys/recursion/limit exception. Take a look at slightly modified code, it should help you to get what you want. It seems that bisect method works as expected.

import sympy
import numpy as np
import math
from scipy.optimize import fsolve,bisect
from functools import partial

@np.vectorize
def PoissonEqs(p, m, alpha):
    result = list()
    for j in range(m + 1):
        if j == 0:
            result.append(math.exp(-p)-alpha)
            continue
        result.append(math.exp(m*math.log(p)-p-math.log(math.gamma(m+1)))+result[-1])
    return result.pop()
    
    
def func2(p):
    m=1; alpha=0.05
    return PoissonEqs(p,m,alpha)


from pylab import *
plot(np.linspace(0.1,10,100), func2(np.linspace(0.1,10,100)), 'r')
show()


bisect(func2, 0.0000000001, 10)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [pandas]How to liner fit time series data and get linear fit equation and r square Sri 5 472 Apr-04-2019, 12:00 PM
Last Post: Sri
  odeint to solve Schrodinger equation kiyoshi7 14 1,890 Nov-23-2018, 11:49 AM
Last Post: kiyoshi7
  To put a solution of an equation into a function pianistseb 6 535 Nov-11-2018, 08:22 PM
Last Post: pianistseb
  How implement parametric ODE equation in python meesmaeili 1 417 Oct-16-2018, 01:39 AM
Last Post: scidam
  are these equation to PyOpenCV API function conversions correct? jasper13 0 458 Oct-06-2018, 03:03 PM
Last Post: jasper13
  Solve a system of non-linear equations in Python (scipy.optimize.fsolve) drudox 7 7,008 Aug-18-2018, 02:27 AM
Last Post: scidam
  Euler class for solve System of ODE drudox 0 637 Jul-08-2018, 09:31 PM
Last Post: drudox
  I need a help to solve equations system alizo_as 1 533 May-04-2018, 04:51 PM
Last Post: Gribouillis
  How to make interations untill a certain value of equation is reached? Alberto 3 684 Feb-06-2018, 04:57 PM
Last Post: DeaD_EyE

Forum Jump:


Users browsing this thread: 1 Guest(s)