converting dsolve output to solve it for a value in sympy - python-3.x

I have
import sympy as sm
x = sm.symbols('x', cls=sm.Function)
t = sm.symbols('t')
expr = x(t).diff(t) + 0.05*x(t)
sol = sm.dsolve(expr,x(t), ics = {x(0):25})
Now i have the solution as an relational equality. Now i want to solve t for x = 1. I can't do
s = sm.Eq(x,-1) to do sm.solve(s,t) as s returns False

figured it out. It's simply easy. x.rhs can be used to create an equation to use solve for t.
equation = sm.Eq(sol.rhs,1)
sm.solve(equation,t)
gives the result t ~ 64.38

Related

Is there a way to pass an equation symbolically into a function for sympy?

From this
import sympy as sp
x,y,z = sp.symbols("x y z")
sp.Ep(x,y/z)
To this
#varibles = array
#equation = ????
def solver(variables,equation):
#Looping through variables array and converting variables to sympy objects
for var in variables:
var = sp.symbols(var)
#Generate sympy Equation
equation = sp.Ep(equation)
variables = [x,y,z]
equation = x,y/z #invalid code
solver(variables,equation)
I'm creating a function that is able to take in an equation with x amount of variables and x-1 number of values then solve for the missing variable symbolically then return a numerical answer using the values provided.
I only included a small portion of code where I'm having trouble understanding how to pass through an equation. Any solutions or pointers would be greatly appericated. Thanks.
There are several layers of potential confusion here concerning Python variables and SymPy objects (Symbols) used for variables.
Here is an example of what you are saying:
# 3 variables
syms = x, y, z = var('x:z')
# 2 values
vals = {x:1, y:2}
# an equations
eq = Eq(x, y/z)
# solve for the missing value symbolically
missing = set(syms) - set(vals) # == {z}
solve(eq, missing)
[y/x]
# solve for the missing value after substituting in the known values
solve(eq.subs(vals))
[2]
You could make a solver to accept an equation and then specified values and figure out the missing one and return that value by doing something like this:
>>> def solver(eq, **vals):
... from sympy.core.containers import Dict
... from sympy.solvers.solvers import solve
... free = eq.free_symbols
... vals = Dict(vals)
... x = free - set(vals)
... if len(x) != 1:
... raise ValueError('specify all but one of the values for %s' % free)
... x = x.pop()
... return solve(eq.subs(vals), x, dict=True)
...
>>> solver(eq, x=1, z=2)
[{y: 2}]
Does that give you some ideas of how to continue?

Minimizing a Composite function

I'm kinda new to python, and I'm trying to minimize the function "fun" that is a composite of "Velo" (also a composite of "area"), but it is giving me de error: TypeError: fun() missing 3 required positional arguments: 'q', 'ro', and 'mi'
from math import pi
import scipy.optimize
def area (r):
return pi*r**2
def velo(a,q):
a = area(r)
return q/a
def fun (r, q, ro, mi):
v = velo (q,a)
fun = (ro*v*r)/mi
return fun
x0 = [2,3,5,6]
res = scipy.optimize.minimize(fun, x0)
print(res)
Can someone help out here?
Many things to say here, I will try to answer properly.
Environment
Which IDE are you using? I advise you VSCode for example, as it underlines some mistakes it can detect, great for typos aswell.
Local variables
In python, variables are defined locally in functions. It means that the variable r in a = area(r) is not defined in your velo function.
The same mistake occurs in v = velo(q,a), as a is not defined in the fun function. I advise you to check these mistakes first.
Answer to your specific question
The error is raised because the array is considered one argument. I will transform your code (with is not working because of the previous points) but solves your error
def fun(array):
r, q, ro, mi = array
v = velo (q,a)
res = (ro*v*r)/mi
return res
x0 = np.array([2,3,4,5])
res = scipy.optimize.minimize(fun, x0)

Failed to apply unicode-escape in pandas

Cleaning tweet datasets by removing annoying character in bytecode (exp : \xf0\x9f\x99\x82)
Here's the code without using function :
b = data_tweet['Tweet']
b.head()
for i in b:
x = i.encode('utf=8')
y = x.decode('unicode-escape')
print(y)
It worked. The character became : 🙄, 🥰, etc.
But when I implemented it using function, in order to convert it in csv file. it failed. The byte character stays the same (exp : \xf0\x9f\x99\x82)
Here's the code :
def convert(text):
for i in text:
x = i.encode('utf=8')
y = x.decode('unicode-escape')
return text
convert(data_tweet['Tweet'])
Does anyone know why?
Problem is that you actually didn't assign the result to data_tweet['Tweet']. You can use apply() on Series.
def convert(text):
x = text.encode('utf=8')
y = x.decode('unicode-escape')
return y
data_tweet['Tweet'] = data_tweet['Tweet'].apply(convert)
Or
data_tweet['Tweet'] = data_tweet['Tweet'].apply(lambda text: text.encode('utf=8').decode('unicode-escape'))

How to evaluate sympy symbolic function with multiple inputs

I am trying to use sympy to solve a function symbolically, then input the values in and solve it numerically. I can do this with just one variable, but can't figure out how to do it with multiple. Here is what I have so far.
v,v0,a,t = sp.var('v v0 a t')
args = [v0,a,t]
arg_vals = [1,-9.81,2]
def get_function():
v = v0 + a*t
return v
def get_derivative(fun,var):
derivative = sp.diff(fun,var)
return derivative
def get_integral(fun,var):
integral = sp.integrate(fun,var)
return integral
def eval_function(fun, args, arg_vals):
i=0
for i in range(len(arg_vals)):
args[i] = arg_vals[i]
return fun.evalf(subs={args})
v = get_function()
a = get_derivative(v,t)
x = get_integral(v,t)
x_eval = eval_function(v,args,arg_vals)
The code runs fine until it hits the eval_function return fun.evalf(subs={args}). Then I get
>>>TypeError: unhashable type: 'list'
I've been trying to figure this out for a while, and guess that someone can just take a few seconds and tell me I'm dumb lol. Thanks for the help!!
(using anaconda, spyder, python 3)

how do i write multiple function outputs to single csv file

i am scraping multiple websites so i am using one function for each website script, so each function returns 4 values, i want to print them in dataframe and write them in csv but i am facing this problem, i may be asking something too odd or basic but please help
Either i will have to write whole script in one block and that will look very nasty to handle so if i could find a way around, this is just a sample of problem i am facing..
def a1(x):
z=x+1
r = x+2
print(z, r)
def a2(x):
y=x+4
t=x+3
print(y, t)
x = 2
a1(x)
a2(x)
3 4
6 5
data = pd.Dataframe({'first' : [z],
'second' : [r],
'third' : [y],
'fourth' : [t]
})`
data
*error 'z' is not defined*
You may find it convenient to write functions that return a list of dicts.
For example:
rows = [dict(a=1, b=2, c=3),
dict(a=4, b=5, c=6)]
df = pd.DataFrame(rows)
The variables are only defined in the local scope of your functions, you'd either need to declare them globally or - the better way - return them so you can use them outside of the function by assigning the return values to new variables
import pandas as pd
def a1(x):
z = x+1
r = x+2
return (z, r)
def a2(x):
y = x+4
t = x+3
return (y, t)
x = 2
z, r = a1(x)
y, t = a2(x)
data = pd.DataFrame({'first' : [z],
'second' : [r],
'third' : [y],
'fourth' : [t]
})

Resources