Using Python 3.9.7 in VS code. I'm doing some stress analysis in python:
from math import sin, cos, tan, degrees, radians
from operator import eq
from sympy import *
x, y, z, a = symbols("x y z a")
vm_crit = ((x - y)**2 + (y - z)**2 + (z - x)**2)**(1/2) -(2**(1/2))*a
vm_crit_sub = vm_crit.subs(x, z)
vm_crit_solve = solveset(vm_crit_sub,y)
print(vm_crit_solve)
I get:
ConditionSet(y, Eq(-1.4142135623731*a + ((-y + z)**2 + (y - z)**2)**0.5, 0), Complexes)
The second argument is correct, but for some reason the function isn't solving for 'y'. If I get rid of the squares:
x, y, z, a = symbols("x y z a")
vm_crit = (x + y + z) - a
vm_crit_sub = vm_crit.subs(x, z)
vm_crit_solve = solveset(vm_crit_sub,y)
print(vm_crit_solve)
I get:
{a - 2*z}
This is correct.
In sympy do you have to expand before solving? If so, is there a way around it?
Thanks for any help.
Related
My goal is to find u as function of x and y using Sympy module.
the equations are:
The answer should be:
from sympy import cosh, sinh, symbols, sin, cos, Eq
u, v, x, y = symbols('u, v, x, y')
eqq1 = Eq(x, sin(u)*cosh(v))
eqq2 = Eq(y, cos(u)*sinh(v))
What's next?
I tried
result = solve((Eqq1, Eqq2), u, v)
Obviously it's not the right way.
Probably not the answer that you expect, but you can rework the problem by eliminating v, as follows:
(x cos u)² - (y sin u)² = cos²u sin²u
Then with t = sin²u, the equation is quadratic in t:
x² (1 - t) - y² t = (1 - t) t
Divide each side of x equation by cosh(v) and each side of y equation by sinh(v). Square all sides. Replace sinh(v)**2 with z and cosh(v)**2 with 1 + z. The sum of the lhs of the equations is 1. The difference is cos(2u). You can solve for cos(2u) and z and work out something close to what you are looking for.
>>> from sympy import x, y, z, v, u
>>> from sympy import cosh,sinh,cos,Tuple,solve
>>> e1 = x**2/cosh(v)**2 + y**2/sinh(v)**2-1
>>> e2 = y**2/sinh(v)**2-x**2/cosh(v)**2-cos(2*u)
>>> solve(Tuple(e1,e2).subs(cosh(v)**2,1+z).subs(sinh(v)**2,z), z, cos(2*u), dict=True)
[
{z: x**2/2 + y**2/2 - sqrt((x**2 - 2*x + y**2 + 1)*(x**2 + 2*x + y**2 + 1))/2 - 1/2,
cos(2*u): -x**2 - y**2 - sqrt((x**2 - 2*x + y**2 + 1)*(x**2 + 2*x + y**2 + 1))},
{z: x**2/2 + y**2/2 + sqrt((x**2 - 2*x + y**2 + 1)*(x**2 + 2*x + y**2 + 1))/2 - 1/2,
cos(2*u): -x**2 - y**2 + sqrt((x**2 - 2*x + y**2 + 1)*(x**2 + 2*x + y**2 + 1))}]
I wanna solve a simultaneous differential equation and based on Lorenz equations:
def f(xyz, t, rho, sigma, beta):
x, y, z = xyz
return [sigma * (y - x),
x * (rho - z) - y,
x * y - beta * z]
I Wrote this:
def f(xyz, t, rho, sigma, beta):
x, y, z = xyz
return [sigma * y(t).diff(t) + sigma * x + beta * y -77,
x + rho * y - 61]
So basically I have another differential of y in the first equation and I tried to take derivative but it says:
TypeError: 'numpy.float64' object is not callable"
Can you tell me How can I solve such problems and for second orders of those?
You have a linear system for the derivatives, call them xp, yp. Fortunately it is triangular, so you can solve it via back-substitution. So first solve for yp then insert that for xp.
def f(xy,t):
x,y = xy
yp = 61 - (4*x + y)
xp = 77 - (2*yp + 2*x + 5*x)
return xp,yp
In general you could employ a linear-system solver numpy.linalg.solve or a more general solver like scipy.optimize.fsolve to extract the derivatives from a system of implicit equations (as far as possible, DAE - systems of differential-algebraic equations can also have this form).
The problem is that when you write y(t), Python thinks you are calling a function called y with argument t, but y appears to be a decimal number, not a function.
Python has a dynamic type system, so when you write
x, y, z = xyz
Python will assign the variable y to the data type of the middle value of xyz
So do u want to solve Lorenz differential equations in Python?
The link below can help you very much with the answer you are trying to find
https://www.programmersought.com/article/82154360499/
or you can solve it like this:
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
from mpl_toolkits.mplot3d import Axes3D
def lorenz(state, t, sigma, beta, rho):
x, y, z = state
dx = sigma * (y - x)
dy = x * (rho - z) - y
dz = x * y - beta * z
return [dx, dy, dz]
I am using SymPy to factor polynomials over the complex numbers.
import sympy as sp
x, y = sp.symbols('x y')
z = sp.expand( (x - 1) * (y - 1) )
factorization_1 = sp.factor(z)
factorization_2 = sp.factor(z, extension = [sp.I])
Here, z has contains the expression xy - x - y + 1, and factorization_1 indeed has the expected value (x - 1)(y - 1), but for some reason factorization_2 has the value x - 1.
Where did the factor (y - 1) go?
I am trying to construct polynomials over a two-valued finite field {0, 1}, and I want them to automatically simplify using some identities that exist in this setting.
I have tried the following:
from sympy import *
from sympy.polys.domains.finitefield import FiniteField
x, y, z, t = symbols('x y z t')
k = Poly(x+y * z*z + (x + y) + y + 1, domain=FiniteField(2))
This already simplifies to:
Poly(y*z**2 + 1, x, y, z, modulus=2)
However, the z**2 is actually the same as z in the field that I want to use. It does seem to automatically recognize that y + y = 0. How can I implement the other identity, z * z = z (idempotency)?
What you want doesn't seem to implemented for poly but maybe you can simulate the effect:
In [54]: normalise = lambda p: Poly(ratsimpmodprime(p, [n**2-n for n in p.free_symbols]), modulus=2)
In [55]: e = x+y * z*z + (x + y) + y + 1
In [56]: normalise(e)
Out[56]: Poly(y*z + 1, x, y, z, modulus=2)
from sympy import *
var('x y')
eqn=x**2+y
print(eqn.diff(x))
Getting 2*x but required 2*x+y'
We have to initialize y as a function of x rather than a Symbol
from sympy import *
var('x')
y = Function('y')(x)
eqn = x**2 + y
x=eqn.diff(x)
print(x)
# 2*x + Derivative(y(x), x)
Source:GUser Jashan
As mention by Smichr(https://stackoverflow.com/users/1089161/smichr) ,to get dy/dx from an equation or expression = 0
from sympy import *
var('x y')
eqn = x**2 + y
x=idiff(eqn,y,x)
print(x)
# -2*x