How to improve speed of sympy.solvers for an exponential equation? - python

I am using sympy as part of a graph drawing program and it works pretty much perfectly for anything I throw at it, but there is one particular equation that seemed to cause it to struggle. Other exponential equations work fine, but this particular one just seems to make my code load indefinitely.
My code is as follows:
import math,sympy
x=sympy.Symbol("x")
print (sympy.solvers.solve(19.9*sympy.exp(-1.257*x)-275.0),x)
Thanks for any and all help!

Related

Computing log of integral in terms of log of integrand

This question may be half computational math, half programming.
I'm trying to estimate log[\int_0^\infty\int_0^\infty f(x,y)dxdy] [actually thousands of such integrals] in Python. The function f(x,y) involves some very large/very small numbers that are bound to cause overflow/underflow errors; so I'd really prefer to work with log[f(x,y)] instead of f(x,y).
Thus my question is two parts:
1) Is there a way to estimate log[\int_0^\infty\int_0^\infty f(x,y)dxdy] using the log of the function instead of the function itself?
2) Is there an implementation of this in Python?
Thanks
I would be surprised if the math and/or numpy libraries or perhaps some more specific third party libraries would not be able to solve a problem like this. Here are some of their log functions:
math.log(x[, base]), math.log1p(x), math.log2(x), math.log10(x) (https://docs.python.org/3.3/library/math.html)
numpy.log, numpy.log10, numpy.log2, numpy.log1p, numpy.logaddexp, numpy.logaddexp2 (https://numpy.org/doc/stable/reference/routines.math.html#exponents-and-logarithms)
Generally, Just google: "logarithm python library" and try to identify similar stackoverflow problems, which will allow you to find the right libraries and functions to try out. Once you do that, then you can follow this guide, so that someone can try to help you get from input to expected output: How to make good reproducible pandas examples

Python pde solution using fipy gives value error

I am trying to solve a pde using the fipy package in python. The code that I have written for the same has been attached.
!pip install fipy
from fipy import *
mesh= Grid2D(nx=0.001,dx=100,ny=0.0005,dy=100)
phil=CellVariable(name='Sol variable',mesh=mesh)
phil.constrain(0,mesh.facesBottom)
phil.constrain(1,mesh.facesTop)
n=1.7*10**(-6)*((0.026*numerix.exp(phil/0.026)+phil-0.026)+2.25*10**(-10)*(0.026*numerix.exp(phil/0.026)-phil-0.026))**(0.5)
eq=(PowerLawConvectionTerm(coeff=(0.,1.))+ImplicitSourceTerm(coeff=n))
eq.solve(var=phil)
When I try to run the code, I get an error in the last line: k exceeds matrix dimensions.
Any help regarding this would be appreciated.
The obvious issue with the above code is that dx and nx are mixed up. dx is the mesh spacing so is a float while nx are the number of mesh cells, which is an int. So, the third line should be,
mesh= Grid2D(dx=0.001,nx=100,dy=0.0005,ny=100)
That at least makes the problem run without an error. However the solution isn't very interesting as the source term and the initial condition appear to be zero everywhere so the result is zero everywhere.
It's also worth considering the following
FiPy isn't really designed for hyperbolic problems, which require high order discretizations to solve accurately. It will solve, but may not be as accurate (it might be ok in equilibrium).
The source term is non-linear so will require many iterations to reach a solution.
It's a good idea to have a transient term in this type of equation and iterate towards equilibrium.

Python: How to integrate an initial value problem backwards AND forwards in time, using initialscipy.integrate.solve_ivp?

I am using solve_ivp from scipy.integrate to solve an initial value problem y'(t) = fun(t,y) with y(t0)=y0. The help is here, from which one can see that the most basic call of the function is of the form:
solve_ivp(fun,[t0,tf],y0)
As stated, here y0=y(t0). I already figured out, that one can integrate backwards in time by entering a tf which is smaller than t0. However I don't know how I can integrate both, backwards and forwards in time in one go.
I.e., I want to integrate over an interval [ti,tf], where ti<t0<tf and where still y0=y(t0) as before. Is that possible? Or do I have to solve twice, and then piece the solutions together afterwards?
Thanks for suggestions!

Python: Find the equation of my line, Logit

I have some data that, when plotted, is looking like it is showing similarities to the LOGIT function. However, I need the equation of this line and I am struggling to find a package/function in Python to help me with this! Below is a screenshot of the graph. I have a load of X,Y data and I need the full equation of this line.
Every time I try and research this I am just pointed towards using logistic regression, however i'm convinced there must be a function in which I can input my data and comfortably receive an equation/coefficients for an equation in the output.
Sorry guys as I'm asking for whereabouts of a package I can't really provide my workings so far, because there are no workings thus far unfortunately.
Thanks all

scipy.integrate.odeint fails depending on time steps

I use python for scientific applications, esp. for solving differential equations. I´ve already used the odeint function successfully on simple equation systems.
Right now my goal is to solve a rather complex system of over 300 equations. At this point the odeint functions give me reasonable results as long as the time steps in the t-array are equal or smaller than 1e-3. But I need bigger time steps since the system has to be integrated over several thousand seconds. Bigger time steps yield the "excess work done.." error.
Does anyone have experience with odeint and can tell me, why this is the case, although the odeint function seems to choose its time steps automatically and then displays the results that match the time steps given by me?
I simply don´t understand why this happens. I think I can work around the problem by integrating multiple times, but maybe someone knows a better solution. I apologize in advance in the case there already is a solution elsewhere and I haven´t seen it.

Categories

Resources