-1

I have a system of four equations with four unknowns posted below. Each equation (f1,f2,f3,f4) is set equal to zero, and I have tried using fsolve with no success. The issue may be that these are non-linear. Any help setting up a script to solve for these four unknowns in Python would be greatly appreciated. Please note the code summarized below does not have proper syntax, it is essentially just a text summary of the equations/system:

V = Ind. variable from 0 to 300

Qcu = ?
Qzn = ?
Ccu = ?
Czn = ?

f1 = [(Qcu*Czn)/(Qzn*Ccu)] - 3
f2 = Qzn + Qcu - 3.5
f3 = Czn + (Qzn*V) - 0.189
f4 = Ccu + (Qcu*V) - 0.917
erkvos
  • 19
  • 1
  • 2
    Welcome to Stack Overflow! This is not a website where people write code for you so that you don't have to. If you need help debugging code that you have written, you must post a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) and explain the specific problem with your code. – Skully Dec 01 '21 at 00:03
  • 4
    *" I have tried using fsolve with no success"* There's no builtin function called `fsolve` in python. Maybe you meant [`scipy.optimize.fsolve` from library scipy](https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fsolve.html)? A bit of context would be nice. Also, what do Qcu, Qzn, Ccu, Czn mean? Where are the equations? – Stef Dec 01 '21 at 00:11
  • 1
    Also, you said your equations are "non-linear". That is extremely vague. Asking "Can you solve a non-linear equation?" is about as precise as asking "Can you pilot a non-car vehicle?" There's a whole world of different equations that are not linear, just like there's a plethora of vehicles which are not cars. Some non-linear equations are easy to solve, others are extremely hard. – Stef Dec 01 '21 at 00:15

1 Answers1

2

You could try sympy, Python's symbolic math library:

from sympy import symbols, solve

V = symbols('V')
Qcu, Qzn, Ccu, Czn = symbols('Qcu Qzn Ccu Czn')

f1 = ((Qcu * Czn) / (Qzn * Ccu)) - 3
f2 = Qzn + Qcu - 3.5
f3 = Czn + (Qzn * V) - 0.189
f4 = Ccu + (Qcu * V) - 0.917
sol = solve([f1, f2, f3, f4], [Qcu, Qzn, Ccu, Czn])

This solves for the 4 parameters as a function of V

# first solution for Qcu, Qzn, Ccu, Czn
(0.035*(50.0*V - 50.0*sqrt(V**2 - 0.732*V + 0.1764) + 21.0)/V,
 0.035*(50.0*V + 50.0*sqrt(V**2 - 0.732*V + 0.1764) - 21.0)/V,
 -1.75*V + 1.75*sqrt(V**2 - 0.732*V + 0.1764) + 0.182,
 -1.75*V - 1.75*sqrt(V**2 - 0.732*V + 0.1764) + 0.924)
# second solution (changing sign of sqrt
(0.035*(50.0*V + 50.0*sqrt(V**2 - 0.732*V + 0.1764) + 21.0)/V,
 0.035*(50.0*V - 50.0*sqrt(V**2 - 0.732*V + 0.1764) - 21.0)/V,
 -1.75*V - 1.75*sqrt(V**2 - 0.732*V + 0.1764) + 0.182,
 -1.75*V + 1.75*sqrt(V**2 - 0.732*V + 0.1764) + 0.924)

You could visualize the solutions, e.g. via:

plot(*sol[0], (V, 1, 300), ylim=(-5, 5))

sympy plot

Or you could use sympy's lambdify() to create a numpy version of the functions.

JohanC
  • 71,591
  • 8
  • 33
  • 66