Recently I got a long equation to solve that looks like that.
I've tried to solve this using sympy.solveset(), but it returned ConditionSet which means it couldn't handle this equation. How can I solve this equation using simpy library and if not at least in python? The code that I used:
import sympy as sp
t = sp.symbols('t')
a = 1.46
b = 1.2042 * 10**-4 * ((1.2275 * 10**-5 + t) * sp.ln(1.2275 * 10**-5 + t) - t)
result = sp.solveset(sp.Eq(a, b), t)
print(result)
This is a transcendental equation. It possibly has an analytic solution in terms of the Lambert W function but I'm not sure. I'll assume that you just want a numerical solution which you can get using nsolve:
In [42]: nsolve(a - b, t, 1)
Out[42]: 1857.54700584719
Related
I've just started trying out SymPy. Unfortunately, I am already stumped. Behold this:
from sympy import *
t, G, M = symbols('t G M', real = True)
x = Function('x')
y = Function('y')
print(Eq(Derivative(x, t, 2), G * M * x(t) / (x(t) * x(t) + y(t) * y(t))**1.5))
...it simply prints False. The documentation says that this means the relationship can be proven to be false.
I know that I can prevent evaluation by using evaluation = False, but eventually I want to solve my system of differential equations, and then this assumption will come into play again.
So, can anyone see what I did wrong here?
Addendum:
What I am trying to do is play around with the two-body-problem and orbital mechanics. With the gravitational constant G and the mass of the primary M at the origin, this and the symmetric equation for y(t) describe the gravitational acceleration on the secondary.
The solution, Kepler tells us, should be an ellipse for reasonable starting conditions.
I found it now, the solution is rather simple: SymPy needs to be told the x is a function of t, so
print(Eq(Derivative(x(t), t, 2), G * M * x(t) / (x(t) * x(t) + y(t) * y(t))**1.5))
does the trick.
I am trying to solve the function below. I've attempted to use a symbolic solver and fsolve. Both are causing me trouble. First time posting, I apologize in advance if I'm missing something in my question.
Does anyone have a suggestion on how to solve this? I am solving for y, everything else is a known variable.
cos(y) + ((xi - tdd) / y) * sin(y)) - exp(xi - tii)
I attempted this in python using two ways, both did not work. The first is:
import numpy as np
from scipy.optimize import fsolve
import sympy as sym
from sympy import *
def fi(y):
return ((cos(y) + ((xi - tdd) / y) * sin(y)) - exp(xi - tii))
y = fsolve(fi,0.01)
With this code I get this error:
AttributeError: 'ImmutableDenseNDimArray' object has no attribute 'could_extract_minus_sign'
I also tried this:
y = symbols('y')
init_printing(use_unicode=True)
yi = solve(cos(y) + ((xi - tdd) / y) * sin(y)) - exp(xi - tii))
And got this error:
NotImplementedError: multiple generators [y, tan(y/2)] No algorithms are implemented to solve equation y*(10000000000000000*(-tan(y/2)**2 + 1)/(tan(y/2)**2 + 1) - 9849605264665270) - 300789470669454*tan(y/2)/(tan(y/2)**2 + 1)
This is how I solved it in Matlab (i and j because I have x values in a matrix that need to be solve):
fi = #(y,x) (cos(y) + (((x-tdd)/y)*sin (y))) - exp((x - tii));
yi(i) = fzero(#(y) fi(y,xi(i,j)),.01);
As I already addressed in a comment, the solve function isn't geared to solve such equations. More information can be found here.
Regarding fsolve, it appears that the problem is caused because you are using the sin, cos and exp functions from sympy. If you replace them with the functions from the math module the code should work.
Specifically, your code should look like this:
import math
from scipy.optimize import fsolve
def fi(y):
return ((math.cos(y) + ((xi - tdd) / y) * math.sin(y)) - math.exp(xi - tii))
y = fsolve(fi, 0.01)
So pretty much, I am aiming to achieve a function f(x)
My problem is that my function has an integral in it, and I only know how to construct definite integrals, so my question is how does one create an indefinite integral in a function (or there may be some other method I am currently unaware of)
My function is defined as :
(G is gravitational constant, although you can leave G out of your answer for simplicity, I'll add it in my code)
Here is the starting point, but I don't know how to do the integral portion
import numpy as np
def f(x):
rho = 5*(1/(1+((x**2)/(3**2))))
function_result = rho * 4 * np.pi * x**2
return function_result
Please let me know if I need to elaborate on something.
EDIT-----------------------------------------------------
I made some major progress, but I still have one little error.
Pretty much, I did this:
from sympy import *
x = Symbol('x')
rho = p0()*(1/(1+((x**2)/(rc()**2))))* 4 * np.pi * x**2
fooply = integrate(rho,x)
def f(rx):
function_result = fooply.subs({x:rx})
return function_result
Which works fine when I plug in one number for f; however, when I plug in an array (as I need to later), I get the error:
raise SympifyError(a)
sympy.core.sympify.SympifyError: SympifyError: [3, 3, 3, 3, 3]
(Here, I did print(f([3,3,3,3,3]))). Usually, the function returns an array of values. So if I did f([3,2]) it should return [f(3),f(2)]. Yet, for some reason, it doesn't for my function....
Thanks in advance
how about:
from sympy import *
x, p0, rc = symbols('x p0 rc', real=True, positive=True)
rho = p0*(1/(1+((x**2)/(rc))))* 4 * pi * x**2
fooply = integrate(rho,x)/x
rho, fooply
(4*pi*p0*x**2/(1 + x**2/rc),
4*pi*p0*rc*(-sqrt(rc)*atan(x/sqrt(rc)) + x)/x)
fooply = fooply.subs({p0: 2.0, rc: 3.0})
np_fooply = lambdify(x, fooply, 'numpy')
print(np_fooply(np.array([3,3,3,3,3])))
[ 29.81247362 29.81247362 29.81247362 29.81247362 29.81247362]
To plug in an array to a SymPy expression, you need to use lambdify to convert it to a NumPy function (f = lambdify(x, fooply)). Just using def and subs as you have done will not work.
Also, in general, when using symbolic computations, it's better to use sympy.pi instead of np.pi, as the former is symbolic and can simplify. It will automatically be converted to the numeric pi by lambdify.
Let's suppose that I have two transcendental functions f(x, y) = 0 and g(a, b) = 0.
a and b depend on y, so if I could solve the first equation analytically for y, y = f(x), I could have the second function depending only on x and thus solving it numerically.
I prefer to use python, but if matlab is able to handle this is ok for me.
Is there a way to solve analytically trascendent functions for a variable with python/matlab? Taylor is fine too, as long as I can choose the order of approximation.
I tried running this through Sympy like so:
import sympy
j, k, m, x, y = sympy.symbols("j k m x y")
eq = sympy.Eq(k * sympy.tan(y) + j * sympy.tan(sympy.asin(sympy.sin(y) / x)), m)
eq.simplify()
which turned your equation into
Eq(m, j*sin(y)/(x*sqrt(1 - sin(y)**2/x**2)) + k*tan(y))
which, after a bit more poking, gives us
k * tan(y) + j * sin(y) / sqrt(x**2 - sin(y)**2) == m
We can find an expression for x(y) like
sympy.solve(eq, x)
which returns
[-sqrt(j**2*sin(y)**2/(k*tan(y) - m)**2 + sin(y)**2),
sqrt(j**2*sin(y)**2/(k*tan(y) - m)**2 + sin(y)**2)]
but an analytic solution for y(x) fails.
from sympy import *
K, T, s = symbols('K T s')
G = K/(1+s*T)
Eq1 =Eq(G+1,0)
I want to rewrite equation Eq1 with sympy as polynomial: 1+K+T*s==0
How would I do this?
I spent some hours of searching and trying simplifications methods but could not find a elegant, short solution.
The actual problem in SymPy:
from IPython.display import display
import sympy as sp
sp.init_printing(use_unicode=True,use_latex=True,euler=True)
Kf,Td0s,Ke,Te,Tv,Kv,s= sp.symbols("K_f,T_d0^',K_e,T_e,T_v,K_v,s")
Ga= Kf/(1+s*Tv)
Gb= Ke/(1+s*Te)
Gc= Kf/(1+s*Td0s)
G0=Ga*Gb*Gc
G1=sp.Eq(G0+1,0)
display(G1)
How to tell Sympy to rewrite equation G1 as polynomial in shape s^3*(...)+s^2*(...)+s*(...)+(...)=... ?
The actual problem from textbook: http://i.imgur.com/J1MYo9H.png
How it should look like: http://i.imgur.com/RqEDo7H.png
The two equations are equivalent.
Here's what you can do.
import sympy as sp
Kf,Td0s,Ke,Te,Tv,Kv,s= sp.symbols("K_f,T_d0^',K_e,T_e,T_v,K_v,s")
Ga= Kf/(1+s*Tv)
Gb= Ke/(1+s*Te)
Gc= Kf/(1+s*Td0s)
G0=Ga*Gb*Gc
Throw away the denominator
eq = (G0 + 1).as_numer_denom()[0]
Expand the equation and collect terms with powers of s.
eq = eq.expand().collect(s)
Final Equation
Eq(eq, 0)
Eq(K_e*K_f**2 + T_d0^'*T_e*T_v*s**3 + s**2*(T_d0^'*T_e + T_d0^'*T_v + T_e*T_v) + s*(T_d0^' + T_e + T_v) + 1, 0)