Python How to solve a system of nonlinear equations in python

AI Thread Summary
The discussion centers on solving a system of three nonlinear equations in Python using the `scipy.optimize.fsolve` function. The original poster expresses difficulty in providing initial guesses for the variables y, z, and t, which are essential for the solver to function. Suggestions include starting with simple values like y = z = t = 0 or using values based on the order of magnitude of the constants in the equations. The importance of initial guesses is debated, with some arguing that a good iterative solver should not be overly sensitive to these values, while others emphasize the need for informed guesses based on physical parameters. The conversation shifts to the introduction of inequalities that can help establish initial conditions. It is suggested to use trial and error to find suitable values that satisfy the inequalities. Additionally, plotting the equations is recommended to visualize the solution space. Ultimately, the original poster successfully resolves their issue after considering the provided advice.
PaulaS
Messages
18
Reaction score
0
I have the following system of 3 nonlinear equations that I need to solve in python:

7 = -10zt + 4yzt - 5yt + 4tz^2
3 = 2yzt + 5yt
1 = - 10t + 2yt + 4zt

Therefore I need to solve for y,z, and t.

Attempt to solve the problem:

Code:
def equations(p):
    y,z,t = p
    f1 = -10*z*t + 4*y*z*t - 5*y*t + 4*t*z^2 - 7
    f2 = 2*y*z*t + 5*y*t - 3
    f3 = - 10*t + 2*y*t + 4*z*t - 1
    return (f1,f2,f3)

y,z,t = fsolve(equations)

print equations((y,z,t))

But the thing is that if I want to use scipy.optimize.fsolve then I should input an initial guess. In my case, I do not have initial conditions.

Is there another way to solve 3 nonlinear equations with 3 unknowns in python?
 
Technology news on Phys.org
Your initial guess doesn't have to be accurate. Try y = z = t = 0 if you don't know anything better.

If that doesn't converge, since all the constants in your equations are less than 10, the solution is probably the same order of magnitude. So try something like y = 1, z = 2, t = 3.

If it still doesn't converge, try making some or all of the initial values negative.
 
I cannot guess the values for y,z, and t since after all these parameters refer to some physical parameters. And guessing the 'initial guess' will affect the results.
 
If the equations model something physical, then your guess should be based on its physics. Without more details, it is hard to suggest anything more specific.
 
PaulaS said:
I cannot guess the values for y,z, and t since after all these parameters refer to some physical parameters.

I don't understand that. For example I can guess your age is somewhere between 10 and 100, without knowing anything except you asked a question on PF.

And guessing the 'initial guess' will affect the results.
Is that just a pessimistic assumption you are making, or have you actually tried it? (Reading carefully, you said "it will affect", not " it affected").

Assuming there is only one solution, a good iterative solver should not be sensitive to the initial conditions. If there are multiple solutions, finding all of them is a much harder problem than just finding one.

Solving an arbitrary set of nonlinear equations isn't something you can do by following a plug-and-chug procedure. You have experiment, till you find something that works for your particular equations.
 
Thanks a lot guys. After all, you were right. There must be some initial conditions or the problem cannot be solved.

Here are the initial conditions that I have:

2y + 2z - 5 < 0
y + 2z -5 < 0
z < 0

How can I use these 3 inequalities to guess the initial guess?
 
Can't you just solve them using matrices in python?
 
For those inequalities, if you just what one initial value, use trial and error. For example from the third inequality, try z = -1.
The first two are them
2y - 7 < 0
y - 7 < 0
So z = -1 and any value of y < 3.5 would satisfy all three conditions.

More generally, you can plot graphs of the three equations 2y + 2z - 5 = 0, etc. In your example they are all straight lines. All the points that satisfy 2y + 2z - 5 < 0 lie on one side of the line. The diagram with all three lines will show you the valid region, if it exists.
 
  • Like
Likes 1 person
Thanks AlephZero :) Finally, I was able to solve my problem.
 

Similar threads

Replies
15
Views
2K
Replies
1
Views
1K
Replies
4
Views
5K
Replies
1
Views
4K
Replies
3
Views
2K
Replies
6
Views
3K
Replies
17
Views
3K
Back
Top