How to solve a system of nonlinear equations in python

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
8 replies · 35K views
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?
 
Physics 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   Reactions: 1 person
Thanks AlephZero :) Finally, I was able to solve my problem.