How to solve a system of nonlinear equations in python

  • Python
  • Thread starter PaulaS
  • Start date
  • #1
19
0

Main Question or Discussion Point

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?
 

Answers and Replies

  • #2
AlephZero
Science Advisor
Homework Helper
6,994
291
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.
 
  • #3
19
0
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.
 
  • #4
6,054
390
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.
 
  • #5
AlephZero
Science Advisor
Homework Helper
6,994
291
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.
 
  • #6
19
0
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?
 
  • #7
219
0
Can't you just solve them using matrices in python?
 
  • #8
AlephZero
Science Advisor
Homework Helper
6,994
291
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
  • #9
19
0
Thanks AlephZero :) Finally, I was able to solve my problem.
 

Related Threads on How to solve a system of nonlinear equations in python

Replies
2
Views
2K
Replies
1
Views
422
Replies
21
Views
1K
Replies
8
Views
2K
Replies
6
Views
1K
Replies
7
Views
8K
Replies
4
Views
852
Replies
1
Views
576
Replies
9
Views
16K
Top