I would like to evaluate the solution of a differential equation against some x_test
array
from sympy import *
init_printing()
from __future__ import division
from sympy import *
x, y, z, t = symbols('x y z t')
# Constants
C, R, u_rest = symbols('C R u_rest')
f, g, h = symbols('f g h', cls=Function)
solution = dsolve(C*Derivative(f(x), x) + (1/R)*(f(x) - u_rest ),f(x))
x_text = np.array(range(0,100))
but I fail
# 1. attempt with evalf(Not working)
solution.args[1].evalf(subs={x: 3.14})
# 2. attempt with lambdify(Not working)
lambdify(x,solution.args[1])(3.14)
What is the right way to do it?
If you look at your solution.args[1] value you will see that it is an expression of several variables, including x. It will not evaluate to a number until you supply values for all variables. Your first attempt doesn't fail, but you don't explain why it is not giving you what you hoped for:
>>> solution.args[1].evalf(subs={x: 3.14})
u_rest*(1.0 - exp((C1 - 3.14/R)/C))
Related
Suppose I have an eq1 such that
from sympy import symbols, solve, plot, Eq, diff
a, b, X, Y, U = symbols('a b X Y U')
eq1 = Eq(U, X**a*Y**b)
$U=(X^a)(Y^b)$
but When I run diff(eq1 , X)
the differential does not evaluate I merele just get the DU/DX symbol but not evaluated
I know I could defined the function as
U = X**a * Y**b
and easily compute diff(U)
but printing the U expression will not look nice.
I am surprised that you even get what you got. In general, algebraic operations on Eq are not supported.
>>> from sympy import Derivative
>>> ediff=lambda e, *x: e.func(Derivative(e.lhs,*x), e.rhs.diff(*x))
>>> ediff(eq1, X)
Eq(Derivative(U, X), X**a*Y**b*a/X)
I am trying to use Sympy for this problem, however I'm having some trouble with the code. Here's what I have so far. The error message is that "g is not a callable function".
from sympy import *
x, y, z, λ, m = symbols('x y z λ m',real=True)
g = Function('g')(x,y,z)
LHS = g(λ*x,λ*y,z)
RHS = (λ**m)*g(x,y,z)
expr = Eq(LHS,RHS)
display(expr)
I want to work with generic functions as long as possible, and only substitute functions at the end.
I'd like to define a function as the derivative of another one, define a generic expression with the function and its derivative, and substitute the function at the end.
Right now my attempts is as follows, but I get the error 'Derivative' object is not callable:
from sympy import Function
x, y, z = symbols('x y z')
f = Function('f')
df = f(x).diff(x) # <<< I'd like this to be a function of dummy variable x
expr = f(x) * df(z) + df(y) + df(0) # df is unfortunately not callable
# At the end, substitute with example function
expr.replace(f, Lambda(X, cos(X))) # should return: -cos(x)*sin(z) - sin(y) - sin(0)
I think I got it to work with integrals as follows:
I= Lambda( x, integrate( f(y), (y, 0, x))) but that won't work for derivatives.
If that helps, I'm fine restricting myself to functions of a single variable for now.
As a bonus, I'd like to get this to work with any combination (products, derivatives, integrals) of the original function.
It's pretty disappointing that f.diff(x) doesn't work, as you say. Maybe someone will create support it sometime in the future. In the mean time, there are 2 ways to go about it: either substitute x for your y, z, ... OR lambdify df.
I think the first option will work more consistently in the long run (for example, if you decide to extend to multivariate calculus). But the expr in second option is far more natural.
Using substitution:
from sympy import *
x, y, z = symbols('x y z')
X = Symbol('X')
f = Function('f')
df = f(x).diff(x)
expr = f(x) * df.subs(x, z) + df.subs(x, y) + df.subs(x, 0)
print(expr.replace(f, Lambda(X, cos(X))).doit())
Lambdifying df:
from sympy import *
x, y, z = symbols('x y z')
X = Symbol('X')
f = Function('f')
df = lambda t: f(t).diff(t) if isinstance(t, Symbol) else f(X).diff(X).subs(X, t)
expr = f(x) * df(z) + df(y) + df(0)
print(expr.replace(f, Lambda(X, cos(X))).doit())
Both give the desired output.
I'm quite new to programming with python.
I was wondering, if there is a smart way to solve a function, which includes a gamma function with a certain shape and scale.
I already created a function G(x), which is the cdf of a gamma function up to a variable x. Now I want to solve another function including G(x). It should look like: 0=x+2*G(x)-b. Where b is a constant.
My code looks like that:
b= 10
def G(x):
return gamma.cdf(x,a=4,scale=25)
f = solve(x+2*G(x)-b,x,dict=True)
How is it possible to get a real value for G(x) in my solve function?
Thanks in advance!
To get roots from a function there are several tools in the scipy module.
Here is a solution with the method fsolve()
from scipy.stats import gamma
from scipy.optimize import fsolve
def G(x):
return gamma.cdf(x,a=4,scale=25)
# we define the function to solve
def f(x,b):
return x+2*G(x)-b
b = 10
init = 0. # The starting estimate for the roots of f(x) = 0.
roots = fsolve(f,init,args=(b))
print roots
Gives output :
[9.99844838]
Given that G(10) is close to zero this solution seems likely
Sorry, I didn't take into account your dict=True option but I guess you are able to put the result in whatever structure you want without my help.
rom sympy import *
# from scipy.stats import gamma
# from sympy.stats import Arcsin, density, cdf
x, y, z, t, gamma, cdf = symbols('x y z t gamma cdf')
#sol = solve([x - 3, y - 1], dict=True)
from sympy.stats import Cauchy, density
from sympy import Symbol
x0 = Symbol("x0")
gamma = Symbol("gamma", positive=True)
z = Symbol("z")
X = Cauchy("x", x0, gamma)
density(X)(z)
print(density(X)(z))
sol = solve([x+2*density(X)(z)-10, y ], dict=True)
print(sol)
Or:
from scipy.stats import gamma
from sympy import solve, Poly, Eq, Function, exp
from sympy.abc import x, y, z, a, b
def G(x):
return gamma.cdf(x,a=4,scale=25)
b= 10
f = solve(x+2*G(x)-b,x,dict=True)
stats cdf gamma solve sympy
I want to carry out the following partial integration of a 2-D gaussian function of four variables (x, y, alpha and beta), with respect to only x and y, as follows. In the end I want the answer to be a function of alpha and beta only.
I wrote the following code in python to execute the above mentioned integral.
from sympy import Symbol
from sympy import integrate
from math import e
alpha = Symbol('alpha')
beta = Symbol('beta')
x = Symbol('x')
y = Symbol('y')
n = 2
value = integrate( e**( -(x - alpha)**n - (y - beta)**n ), (x, -1, 1), (y, -1, 1) )
However I get the following error:
sympy.polys.polyerrors.DomainError: there is no ring associated with RR
The above mentioned integrate function works fine for n=1. However it breaks down for n>1.
Am I doing something wrong?
Welcome to SO!
Interestingly it works when you substitute alpha and beta into the integral bounds. Try:
from IPython.display import display
import sympy as sy
sy.init_printing() # LaTeX like pretty printing forIPython
alpha, beta, x, y = sy.symbols("alpha, beta, x, y", real=True)
f = sy.exp(-x**2 - y**2) # sy.exp() is better than the numeric constant
val = sy.integrate(f, (x, -1+alpha, 1+alpha), (y, -1+beta, 1+beta))
display(val)