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)
Related
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
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)
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'))
I am trying to define a function for Fibonacci series but the code is not working. I can't resolve the issues and need help to fix the problem. Whenever I am trying to call this function, last value of the series comes always greater than n, which I don't want
def fib(n):
Series = [0,1]
if n>1:
while Series[-1]<=n:
c=Series[-2]+Series[-1]
Series.append(c)
if Series[-1]>n:
break
return Series
Your code is really good, just the indentation of the return is wrong. Just align it properly.
def fib(n):
Series = [0,1]
if n>1:
while Series[-1]<=n:
c=Series[-2]+Series[-1]
Series.append(c)
return Series
do you need something like this:
def fibo(n):
l = [0,1]
for i in range(2,n+1):
l += [l[i-1] + l[i-2]]
return l
If you want to get the Fibonacci sequence up to n:
def fib(n):
series = [0,1]
if n > 1:
c = 1
while c <= n:
series.append(c)
c = series[-2] + series[-1]
return series
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]
})