I am trying to rewrite several systems of equations in python, but I have little (very little) experience. I wrote something, but I don't understand if it's correct. Can anyone help?
Here is a photo of the equations:doc My code:
import numpy as np
from scipy.optimize import fsolve
# Initial data
Ptmax_init = 100
Q_init = 100
e_init = 0.01
r0 = 0.85
S = 0.18
b = 0.045
c0 = 9.8 * 10**3
B = 0.53
th = 0.05
Cr = 6.33 * 10**5
mu = 0.8
Qc = 1.177 * 10**4
h_kr = 0.6
L = 2.5
ec = 0.01
k = 2.81
tang = 0.65
#Equations
def equations(vars):
Ptmax, Q, e = vars
eq1 = Ptmax = Q * ((r0 / (r0 - e)) * ((S - b) / S) * ((2 * c0 * (B + 2 * th) * r0) / (Cr * e) + tang - ((2 * c0 * (B + 2 * th)) / Cr)) + (mu * (b / S)))
eq2 = Q = (Qc + (Ptmax * h_kr / L))
eq3 = e = (ec + (Ptmax * h_kr) / (L * 1.44 * np.sqrt((Cr**2 * Q) / r0 * (1 + np.sqrt(k + 1))**2)**(1/3)))
return [eq1, eq2, eq3]
# Solving
initial_guess = [Ptmax_init, Q_init, e_init]
solution = fsolve(equations, initial_guess, maxfev=150)
Ptmax_sol, Q_sol, e_sol = solution
print(f"Solve: Ptmax = {Ptmax_sol}, Q = {Q_sol}, e = {e_sol}")
Try replacing the equal signs =
by minus signs -
in the equations, i.e.:
def equations:
Ptmax, Q, e = vars
eq1 = Ptmax - Q * ((r0 / (r0 - e)) * ((S - b) / S) * ((2 * c0 * (B + 2 * th) * r0) / (Cr * e) + tang - ((2 * c0 * (B + 2 * th)) / Cr)) + (mu * (b / S)))
eq2 = Q - (Qc + (Ptmax * h_kr / L))
eq3 = e - (ec + (Ptmax * h_kr) / (L * 1.44 * np.sqrt((Cr**2 * Q) / r0 * (1 + np.sqrt(k + 1))**2)**(1/3)))
return [eq1, eq2, eq3]
Compare with the example given at the bottom of the documentation page for fsolve:
Find a solution to the system of equations:
x0*cos(x1) = 4, x1*x0 - x1 = 5
.
def func(x):
return [x[0] * np.cos(x[1]) - 4, # minus 4, not equal 4
x[1] * x[0] - x[1] - 5] # minus 5, not equal 5
root = fsolve(func, [1, 1])
In general, in python and in most other programming languages, =
means assignment and ==
means equality. Symbol =
alone does not mean equality.
Consider for instance:
>>> eq = b = 3
>>> print(eq)
3
>>> print(b)
3
>>> eq = (b = 3)
File "<stdin>", line 1
eq = (b = 3)
^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?
>>> eq = (b == 3)
>>> print(eq)
True
>>> if (b = 3):
File "<stdin>", line 1
if (b = 3):
^^^^^
SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?