How to find the difference of x -y using sympy - python

As you see in the code, I want to find the difference of x-y using the resulting R of solve. But, the code keeps returning x-y as value. Please help me. I am a 10 year old kid that just started coding.
import sympy as sp
x, y = sp.symbols ('x, y')
eq1 = sp.Eq(7 * x, 12 * y)
eq2 = sp.Eq(x+y, 9500)
R = sp.solve ((eq1, eq2), (x, y))
print (x-y)

The result R of sp.solve is a Python dictionary with values for x and for y:
import sympy as sp
x, y = sp.symbols('x, y')
eq1 = sp.Eq(7 * x, 12 * y)
eq2 = sp.Eq(x + y, 9500)
R = sp.solve((eq1, eq2), (x, y))
Result: {x: 6000, y: 3500}
To apply the resulting dictionary to an expression, use subs(R):
print((x - y).subs(R))
Result: 2500

Related

Speeding up Sympy solve() on a particular equation

I am trying to solve an equation but the solve() function is taking over 10 minutes even on a high RAM colab notebook. Are there any simplifications to the problem that I can take to speed this along? Here is the code:
x, y, x_0, y_0, x_new, y_new, t, f = symbols('x y x_0 y_0 x_new y_new t f')
D = (2 * (1 - t) * sqrt(x * y) + t * (x + y)) / (2 * (x + y) * sqrt(x * y))
D_old = D.subs([(x, x_0), (y, y_0)])
D_new = D.subs([(x, x_new), (y, y_new)])
delta_D = D_new - D_old
target = Eq(delta_D, f)
answer = solve(target, x_new)
If it is taking a long time you must be trying to solve for one of the x or y values. This will require solving a messy cubic equation in many variables. It would be better if you just substituted in the values of interest and then used nsolve to find the roots of interest. Otherwise, you can get a symbolic solution to the generic cubic g3 = solve(a*x**3 + b*x**2 + c*x + d, x) and then substitute in the corresponding expressions for the coefficients of collect(sympy.solvers.solvers.unrad(target.rewrite(Add))[0], v) where v is the variable of interest. But I won't bog this down with more details until it is clear what you really want to do.

python curve fit setting an array element with a sequence

I am trying to use curve_fit to solve the two parameters k1, E1, but it keeps giving me the same error: setting an array element with a sequence. When I only have two arrays x and y, it works fine. Could someone tell me how to fix this problem? Thank you!
x = np.array([5,5,5,5,5,5,5,5,
12,12,12,12,12,12])
y = np.array([5,1,2,10,20,40,60,80,
6,6,6,6,6,6])
z = np.array([330,330,330,330,330,330,330,330,
330,350,370,390,410,430])
r = np.array([1.199,1.303,1.58,1.81,2.24,2.35,2.49,2.71,
4.3,8.0,1.4,2.32,3.4,6.24])
R = 2.5
def func(X, k1, E1):
x, y, z = X
return k1 * np.exp(-E1/R/z) * x / y
#initial guess
init_guess = [1, 10000]
fittedParameters, pcov = curve_fit(func, (x, y, z), r, init_guess)
print('Parameters', fittedParameters)

How to substitute variable in multiple equations in Sympy

for example, if I want to use solve a set of linear equations
eq1: x + y + 8z = 2
eq2: 2x + 6y + z = 5
suppose I already know the value of z, is there any way I can subsitute the z in eq1 and eq2 in one operation such as
linear_equations([eq1, eq2]).subs({z: 100})
You can use the Sympy library to achieve what exactly you are looking to solve.
Below is the code which will perform the substitution of the Z value and then solve the linear equation to find the values of x & Y values
from sympy import symbols
from sympy.solvers import solve
x,y,z = symbols('x y z')
expression1 = x + y + 8*z - 2
expression2 = 2*x + 6*y + z - 5
expression1 = expression1.subs(z,100)
expression2 = expression2.subs(z,100)
solution = solve([expression1, expression2], [x, y])
print(solution)

I got an empty list when solving two equation

I tried to solve two simple equations but I got nothing.
from sympy import *
x, y = symbols('x y')
eq1=Function('eq1')
eq2=Function('eq2')
eq1 = Eq(x + y , 1) # x + y = 1
eq2 = Eq(x + y ,3) # x + y = 3
ans = solve([eq1, eq2] , [x, y])
print(ans)
I got
[]
You set everything up ok. The empty list is the way that solve tells you that it could not find a solution for x and y that satisfied the equations. And, indeed, there are no values which, when added, will give two different results (as others have noted).

How to make user-input for Symbols case insensitive

I want to avoid caps lock errors in a scypy base script that I made.
The script calculate partial derivatives
from sympy import *
from sympy.parsing.sympy_parser import parse_expr
from sympy.parsing.sympy_parser import standard_transformations, \
x, y, z = symbols('x y z ', real=True)
transformations = (standard_transformations + (implicit_multiplication_application,)
self.eq1 = parse_expr(self.entry_5.get(), locals(), transformations=transformations)
self.dfx = diff(self.eq1, x)
self.dfy = diff(self.eq1, y)
self.dfz = diff(self.eq1, z)
It work fine for this example entry_5 is x ** 2 + y ** 2 + z ** 2
'Partial derivatives calculation:
Partial der by X: 2*x
Partial der by Y: 2*y
Partial der by Z: 2*z
but now, I'm trying to add capital X,Y,Z with the same code:
.
.
.
x, y, z, X, Y, Z = symbols('x y z X Y Z', real=True)
.
.
.
now, entry_5 is- X ** 2 + y ** 2 + z ** 2 (capital x)
and the output is:
'Partial derivatives calculation:
Partial der by X: 0
Partial der by Y: 2*y
Partial der by Z: 2*z
If you are wanting your input to be recast to lowercase so the derivatives that you hard coded always work you could either convert the input to lower case or, more safely, provide locals that remap the uppercase symbols of interest to lower case symbols. For clarity in the following I use S to sympify rather than the parser:
>>> S('x', {'x':'upper'}) # example showing you can replace 'x' with 'upper'
upper
>>> S('X', dict([(str(i), str(i).lower()) for i in symbols('X:Z')]))
x
In your code you will have to update your locals with the dict() that is being used in my example: loc = locals(); loc.update(dict([(str(i), str(i).lower()) for i in symbols('X:Z')])) and use loc instead of locals() in your code.

Categories