0% found this document useful (0 votes)
2 views

Python Theory Class 25092024-2 (3)

The document provides a series of Python code snippets demonstrating the use of the SymPy library for solving various types of equations, including polynomial, trigonometric, and systems of equations. It includes examples of solving equations symbolically and numerically, showcasing the results for different mathematical expressions. The document also touches on the use of SymPy for solving differential equations, indicating a comprehensive approach to mathematical problem-solving using Python.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Theory Class 25092024-2 (3)

The document provides a series of Python code snippets demonstrating the use of the SymPy library for solving various types of equations, including polynomial, trigonometric, and systems of equations. It includes examples of solving equations symbolically and numerically, showcasing the results for different mathematical expressions. The document also touches on the use of SymPy for solving differential equations, indicating a comprehensive approach to mathematical problem-solving using Python.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Python Theory Class 25092024

September 25, 2024

[95]: import numpy as np


import sympy as sp
import matplotlib.pyplot as plt
x,y,z,a,b,c=sp.symbols('x,y,z,a,b,c')

[96]: #Solution of a Equation


A=x**2-5*x+6
sp.solve(A,x)

[96]: [2, 3]

[97]: sp.solve(A)

[97]: [2, 3]

[98]: sp.solve(sp.Eq(A,0),x)

[98]: [2, 3]

[99]: sp.solve(sp.Eq(A,0))

[99]: [2, 3]

[100]: A1=x**3-5*x**2+x
sp.solve(A1,x)

[100]: [0, 5/2 - sqrt(21)/2, sqrt(21)/2 + 5/2]

[101]: sp.solve([x**2-4,y])

[101]: [{x: -2, y: 0}, {x: 2, y: 0}]

[102]: sp.solve([x**2-4,y],[x,y])

[102]: [(-2, 0), (2, 0)]

[103]: sp.solve([x**2-4,y],x,y)

[103]: [(-2, 0), (2, 0)]

1
[104]: sp.solve(sp.sin(x))

[104]: [0, pi]

[105]: sp.solve(sp.cos(x))

[105]: [pi/2, 3*pi/2]

[106]: sp.solve(sp.tan(x))

[106]: [0]

[107]: sp.solve([x+y-z-5,x-5*y+z-34,x+z-345])

[107]: {x: 1439/10, y: 311/5, z: 2011/10}

[108]: sp.solve([x+y-z-5,x-5*y+z-34,x+z-345],[x,y,z])

[108]: {x: 1439/10, y: 311/5, z: 2011/10}

[109]: sp.solve([x**2+y-z-5,x-5*y+z-34,x+z-345],[x,y,z])

[109]: [(-1/2 + sqrt(28805)/10, 311/5, 691/2 - sqrt(28805)/10),


(-sqrt(28805)/10 - 1/2, 311/5, sqrt(28805)/10 + 691/2)]

[110]: sp.solve([x**2-3,y,z],[x,y,z])

[110]: [(-sqrt(3), 0, 0), (sqrt(3), 0, 0)]

[111]: sp.solve([x**2-3,y-4,z],[x,y,z])

[111]: [(-sqrt(3), 4, 0), (sqrt(3), 4, 0)]

[112]: sp.solve([x**2-3,y-4,z**3-1],[x,y,z])

[112]: [(-sqrt(3), 4, 1),


(-sqrt(3), 4, -1/2 - sqrt(3)*I/2),
(-sqrt(3), 4, -1/2 + sqrt(3)*I/2),
(sqrt(3), 4, 1),
(sqrt(3), 4, -1/2 - sqrt(3)*I/2),
(sqrt(3), 4, -1/2 + sqrt(3)*I/2)]

[113]: sp.solve([x**2-3,y**2-74,z**3-1],[x,y,z])

[113]: [(-sqrt(3), -sqrt(74), 1),


(-sqrt(3), -sqrt(74), -1/2 - sqrt(3)*I/2),
(-sqrt(3), -sqrt(74), -1/2 + sqrt(3)*I/2),
(-sqrt(3), sqrt(74), 1),
(-sqrt(3), sqrt(74), -1/2 - sqrt(3)*I/2),

2
(-sqrt(3), sqrt(74), -1/2 + sqrt(3)*I/2),
(sqrt(3), -sqrt(74), 1),
(sqrt(3), -sqrt(74), -1/2 - sqrt(3)*I/2),
(sqrt(3), -sqrt(74), -1/2 + sqrt(3)*I/2),
(sqrt(3), sqrt(74), 1),
(sqrt(3), sqrt(74), -1/2 - sqrt(3)*I/2),
(sqrt(3), sqrt(74), -1/2 + sqrt(3)*I/2)]

[114]: sp.solve([x**2-3,y**2-74,z**3-1])

[114]: [{x: -sqrt(3), y: -sqrt(74), z: 1},


{x: -sqrt(3), y: -sqrt(74), z: -1/2 - sqrt(3)*I/2},
{x: -sqrt(3), y: -sqrt(74), z: -1/2 + sqrt(3)*I/2},
{x: -sqrt(3), y: sqrt(74), z: 1},
{x: -sqrt(3), y: sqrt(74), z: -1/2 - sqrt(3)*I/2},
{x: -sqrt(3), y: sqrt(74), z: -1/2 + sqrt(3)*I/2},
{x: sqrt(3), y: -sqrt(74), z: 1},
{x: sqrt(3), y: -sqrt(74), z: -1/2 - sqrt(3)*I/2},
{x: sqrt(3), y: -sqrt(74), z: -1/2 + sqrt(3)*I/2},
{x: sqrt(3), y: sqrt(74), z: 1},
{x: sqrt(3), y: sqrt(74), z: -1/2 - sqrt(3)*I/2},
{x: sqrt(3), y: sqrt(74), z: -1/2 + sqrt(3)*I/2}]

[115]: sp.solve([x**2-3,y**2-74,z**3-1],x,y,z)

[115]: [(-sqrt(3), -sqrt(74), 1),


(-sqrt(3), -sqrt(74), -1/2 - sqrt(3)*I/2),
(-sqrt(3), -sqrt(74), -1/2 + sqrt(3)*I/2),
(-sqrt(3), sqrt(74), 1),
(-sqrt(3), sqrt(74), -1/2 - sqrt(3)*I/2),
(-sqrt(3), sqrt(74), -1/2 + sqrt(3)*I/2),
(sqrt(3), -sqrt(74), 1),
(sqrt(3), -sqrt(74), -1/2 - sqrt(3)*I/2),
(sqrt(3), -sqrt(74), -1/2 + sqrt(3)*I/2),
(sqrt(3), sqrt(74), 1),
(sqrt(3), sqrt(74), -1/2 - sqrt(3)*I/2),
(sqrt(3), sqrt(74), -1/2 + sqrt(3)*I/2)]

[116]: #x**2-a*x+b
sp.solve(x**2-a*x+b,x)

[116]: [a/2 - sqrt(a**2 - 4*b)/2, a/2 + sqrt(a**2 - 4*b)/2]

[117]: sp.solve(x**2-a*x+b,a)

[117]: [b/x + x]

[118]: sp.solve(x**2-a*x+b,b)

3
[118]: [x*(a - x)]

[119]: sp.solve(x**2-a*x+b,a,b)

[119]: [(b/x + x, b)]

[120]: sp.solve(x**2-a*x+b,b,a)

[120]: [(x*(a - x), a)]

[121]: eqs = (x*y + 3*y + sp.sqrt(3), x + 4 + y)


sp.solve(eqs, y, x + 2)

[121]: {y: -sqrt(3)/(x + 3), x + 2: -2*x/(x + 3) - 6/(x + 3) + sqrt(3)/(x + 3)}

[122]: sp.solve(eqs, y*x, x)

[122]: {x: -y - 4, x*y: -3*y - sqrt(3)}

[123]: x1,y1=sp.symbols('x1,y1',real=True)

[124]: sp.solve(x1**2+1)

[124]: []

[125]: sp.solve(x1**3-1)

[125]: [1]

[126]: x2,y2=sp.symbols('x2,y2',positive=True)

[127]: sp.solve(x2**2-1)

[127]: [1]

[128]: sp.solve(x**2-1/x,x)

[128]: [1, -1/2 - sqrt(3)*I/2, -1/2 + sqrt(3)*I/2]

[129]: sp.solve(x**2-1/x,x,check=False)

[129]: [1, -1/2 - sqrt(3)*I/2, -1/2 + sqrt(3)*I/2]

[130]: sp.solve(x**2-1/x,x,check=True)

[130]: [1, -1/2 - sqrt(3)*I/2, -1/2 + sqrt(3)*I/2]

[131]: sp.solve(sp.sin(x)/x,x)

4
[131]: [pi]

[132]: sp.solve(sp.sin(x)/x,x,check=True)

[132]: [pi]

[133]: sp.solve(sp.sin(x)/x,x,check=True)

[133]: [pi]

[134]: #sp.solveset()
sp.solve(sp.sin(x))

[134]: [0, pi]

[135]: sp.solveset(sp.sin(x),x)
[135]:
{2𝑛𝜋 | 𝑛 ∈ ℤ} ∪ {2𝑛𝜋 + 𝜋 | 𝑛 ∈ ℤ}

[136]: sp.solve(sp.cos(x),x)

[136]: [pi/2, 3*pi/2]

[137]: sp.solveset(sp.cos(x),x)
[137]: 𝜋 3𝜋
{2𝑛𝜋 + ∣ 𝑛 ∈ ℤ} ∪ {2𝑛𝜋 + ∣ 𝑛 ∈ ℤ}
2 2
[138]: sp.solveset(sp.tan(x),x)
[138]:
{𝑛𝜋 | 𝑛 ∈ ℤ}

[139]: sp.solveset(x**3-4,x)
[139]: 2 2√ 2 2√
2 23 2 3 3𝑖 2 3 2 3 3𝑖
{2 , −
3 − ,− + }
2 2 2 2

[140]: sp.solveset(x**4-4,x)
[140]: √ √ √ √
{− 2, 2, − 2𝑖, 2𝑖}

[141]: sp.solveset(x**5-4,x)
[141]: ⎧ 2 2√ √ 2 2√ √ 2√ 2 √ 2√ 2
{ 2 25 25 5 2
√ 5 5 2 5 2 5 5 2
√ 5 5 2 5 5 2 5 2
√ 5 5 2 5 5 25
⎨2 ,−
5 + −2 𝑖
5 + ,− + +2 𝑖
5 + ,− − −2 𝑖
5 − ,− − +
{ 4 4 8 8 4 4 8 8 4 4 8 8 4 4

[142]: sp.solveset(x**5-5,x)
[142]: ⎧ √ 7 √ √ 7 √ 7 √ √ 7 √
{√5
5
5 5 10 √
5 √ 5 5 5
5 5 10 √
5 √ 5 5 5 10 5
5 √
5 √ 5 5 5 10 5
5 √5
5, − + − 5𝑖 + ,− + + 5𝑖 + ,− − − 5𝑖 − ,− − +

{ 4 4 8 8 4 4 8 8 4 4 8 8 4 4

5
[143]: sp.solveset(x**8-5,x)
[143]: √ √ √ √ √ √ √ √ √ √ √ √ √ √ √ √

8

8

8

8 285 2 8 5𝑖 285 2 8 5𝑖 2 8 5 2 8 5𝑖 2 8 5 2 8 5𝑖
{− 5, 5, − 5𝑖, 5𝑖, − − ,− + , − , + }
2 2 2 2 2 2 2 2

[144]: len(sp.solveset(x**50-5,x))

[144]: 50

[145]: sp.solveset(x**12-1,x)
[145]: √ √ √ √ √ √ √ √
1 3𝑖 1 3𝑖 1 3𝑖 1 3𝑖 3 𝑖 3 𝑖 3 𝑖 3 𝑖
{−1, 1, −𝑖, 𝑖, − − ,− + , − , + ,− − ,− + , − , + }
2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2

[146]: len(sp.solveset(x**12-1,x))

[146]: 12

[147]: sp.solveset(x**2-1,x)
[147]:
{−1, 1}

[148]: sp.solveset(x**4-x,x)
[148]: √ √
1 3𝑖 1 3𝑖
{0, 1, − − ,− + }
2 2 2 2

[149]: #To solve differential Equation


#sp.dsolve()

[150]: f=sp.Function('f')
g=sp.Function('g')

[151]: '''
Solve the differential Equation
y''-5*y'+6y=0
and plot its solution on the interval [-3,3].
'''
eqn=sp.Eq(sp.diff(f(x),[x,2])-5*sp.diff(f(x),x)+6*f(x),0)
sol=sp.dsolve(eqn,f(x))
sol
[151]:
𝑓(𝑥) = (𝐶1 + 𝐶2 𝑒𝑥 ) 𝑒2𝑥

[152]: C1,C2=sp.symbols('C1,C2')
par1=sol.rhs.subs({C1:-1,C2:2})
par1
[152]:
(2𝑒𝑥 − 1) 𝑒2𝑥

6
[153]: par2=sol.rhs.subs({C1:1,C2:0})
par2
[153]:
𝑒2𝑥

[154]: par3=sol.rhs.subs({C1:0,C2:1})
par3
[154]:
𝑒3𝑥

[155]: par4=sol.rhs.subs({C1:-1,C2:0})
par4
[155]:
−𝑒2𝑥

[156]: par5=sol.rhs.subs({C1:0,C2:-1})
par5
[156]:
−𝑒3𝑥

[157]: par6=sol.rhs.subs({C1:1,C2:-1})
par6
[157]:
(1 − 𝑒𝑥 ) 𝑒2𝑥

[158]: X=np.linspace(-3,3,100)
Y1=2*np.exp(3*X)-np.exp(2*X)#par1
Y2=np.exp(2*X)#par2
Y3=np.exp(3*X)#par3
Y4=-np.exp(2*X)#par4
Y5=-np.exp(3*X)#par5
Y6=np.exp(2*X)-np.exp(3*X)#par6
plt.plot(X,Y1,label='$par1=2e^{3x}-e^{2x}$')
plt.plot(X,Y2,label='$par2=e^{2x}$')
plt.plot(X,Y3,label='$par3=e^{3x}$')
plt.plot(X,Y4,label='$par4=-e^{2x}$')
plt.plot(X,Y5,label='$par5=-e^{3x}$')
plt.plot(X,Y6,label='$par6=e^{2x}-e^{3x}$')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()

7
[ ]:

You might also like