How to solve a system of nonlinear equations in python

Click For Summary

Discussion Overview

The discussion revolves around solving a system of three nonlinear equations in Python, specifically using the scipy.optimize.fsolve function. Participants explore the challenges of selecting appropriate initial guesses for the variables involved, as well as the implications of these guesses on the results.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant presents a system of nonlinear equations and seeks a method to solve them in Python without initial guesses.
  • Another participant suggests that an initial guess does not need to be accurate and proposes starting with simple values like y = z = t = 0.
  • Concerns are raised about the impact of initial guesses on the results, particularly since the variables represent physical parameters.
  • Some participants argue that initial guesses should be informed by the physical context of the problem.
  • A participant mentions that guessing initial values is a necessary part of solving nonlinear equations and that it may require experimentation.
  • Initial conditions in the form of inequalities are later introduced, prompting a discussion on how to derive initial guesses from them.
  • Another participant suggests using trial and error to find suitable initial values based on the inequalities provided.
  • Graphical methods are proposed as a way to visualize the valid regions defined by the inequalities.
  • A participant concludes that they were able to solve their problem after considering the advice given.

Areas of Agreement / Disagreement

Participants express differing views on the necessity and strategy for selecting initial guesses, with some advocating for a more physics-based approach while others emphasize the potential for arbitrary guesses. The discussion remains unresolved regarding the best method for determining initial conditions.

Contextual Notes

The discussion highlights the challenges of solving nonlinear equations without clear initial conditions and the dependence on the physical context of the parameters involved. There are unresolved aspects regarding the sensitivity of solutions to initial guesses.

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   Reactions: 1 person
Thanks AlephZero :) Finally, I was able to solve my problem.
 

Similar threads

  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K