SymPy - Solving for variable in equation - python

Is it possible to define an equation and solve a variable in that equation?
D_PWM, Rsense, A = symbols('D_PWM, Rsense, A')
i_out = D_PWM * (A/Rsense)
print i_out
solve(i_out, Rsense)
Result:
A*D_PWM/Rsense
[]

i_out has not been declared as a symbol.
>>> from sympy import *
>>> var('D_PWM, Rsense, A i_out')
(D_PWM, Rsense, A, i_out)
>>> eqn=Eq(i_out,D_PWM * (A/Rsense))
>>> solve(eqn,Rsense)
[A*D_PWM/i_out]

Related

How to factorize fraction by a given symbol?

I have the following fraction:
import sympy as sp
a = sp.Symbol("a")
b = sp.Symbol("b")
a/(a+b)
And would like to print it as
1/(1+b/a)
I saw sympy had a factor function but I couldn't obtain the expected behaviour.
I thought I could maybe do something like:
sp.factor((a/(a+b)), a)
I would call this "distributing the numerator in the denominator":
>>> a/(a + b)
>>> 1/expand(1/_)
1/(1 + b/a)
You can use expand and collect
import sympy as sp
a = sp.Symbol("a")
b = sp.Symbol("b")
expanded = sp.expand(a/(a+b))
collected = sp.collect(expanded, a)
print(collected)

Is there a way to do fraction in sympy

I am trying to do fraction using sympy, I know sympy is supports symbolic but can it be done with old fraction, please know that I want it to be display it as unevaluatedexpr here is my code.
from sympy import *
s = (3)/(2) + (4) / (6)
display(s)
init_printing()
>>> import sympy
>>> a = sympy.Rational(3, 2)
>>> b = sympy.Rational(4, 6)
>>> a
3/2
>>> b
2/3
Alhamdulilaah I found it how to do it this is how you do it
from sympy import *
r = Rational(3,2)+Rational(4,2)
display(r)

Why does SymPy solve give empty set for high-order polynomial?

I have to find the equilibrium points where the nullclines intersect. My code is as below.
>>> from sympy import symbols, Eq, solve
>>> A,M = symbols('A M')
>>> dMdt = Eq(1.05 - (1/(1 + pow(A,5))) - M)
>>> dAdt = Eq(M*1 - 0.5*A - M*A/(2 + A))
>>> solve((dMdt,dAdt), (M,A))
[]
Why is it not giving a solution?
You will see why as I work to get the solution.
I'm going to write the equations as e1 and e2 -- use of Eq without a second arg no longer works (or does so with a warning in the latest versions of SymPy):
>>> from sympy import solve, nsimplify, factor, real_roots
>>> from sympy.abc import A, M
>>> e1 = (1.05 - (1/(1 + pow(A,5))) - M)
>>> e2 = (M*1 - 0.5*A - M*A/(2 + A))
Solve for M using e1
>>> eM = solve(e1, M)[0]
Substitute into e2
>>> e22 = e2.subs(M, eM); e22
-0.5*A - 0.05*A*(21.0*A**5 + 1.0)/((A + 2)*(A**5 + 1.0)) + 0.05*(21.0*A**5 + 1.0)/(A**5 + 1.0)
Get the numerator and denominator
>>> n,d=e22.as_numer_denom()
Find the real roots for this expression (which depends only on A)
>>> rA = real_roots(n)
Find the corresponding values of M by substituting each into eM:
>>> [(a.n(2), eM.subs(A, a).n(2)) for a in rA]
[(-3.3, 1.1), (-1.0, zoo), (-0.74, -0.23), (0.095, 0.050)]
That root of A = -1 is spurious -- if you look at your denominator of e1 you will see that such a value causes division by zero. So that root can be ignored. The others can be verified graphically.
Why didn't solve give the solution? It couldn't give the solution for this high-order polynomial in closed form. Even if you factor the numerator described above (and make floats into Rationals with nsimplify) you have a factor of degree 7:
>>> factor(nsimplify(n))
-(A + 1)*(A**4 - A**3 + A**2 - A + 1)*(5*A**7 + 10*A**6 - 21*A**5 + 5*A**2 + 10*A - 1)/10

Assuming that a symbol has an integer value in SymPy

I believe that the following should evaluate to 1, but it doesn't. Any hints on how to make it work?
n = Symbol('n')
with assuming(Q.integer(n)):
print(cos(2*pi*n))
>>> from sympy import *
>>> n = Symbol('n', integer=True)
>>> cos(2*pi*n)
1
>>> sin(2*pi*n)
0

How do you substitute the values of sympy.core.add.add objects when they are in a list?

I have a list of sympy objects that I would like to evaluate at a certain point (x0 = 1 , x1=1), I have tried using evalf and subs in a for loop but have had no luck. Here is my code:
from sympy import *
b = [3*x0**2 + 4*x1, 4*x0]
for i in b:
i = i.subs({x0:1, x1:1})
print(b)
It returns the same list as before.
In your loop you're not operating on b at all.
You could do this.
>>> from sympy import *
>>> var('x0 x1')
(x0, x1)
>>> b = [3*x0**2 + 4*x1, 4*x0]
>>> [_.subs({x0:1, x1:1}) for _ in b]
[7, 4]
Notice that this code operates on each expression within b individually because b as a whole is not a sympy expression.

Categories

Resources