How to solve a system of nonlinear equations in python

In summary, using the inequalities provided, the initial guesses for y, z, and t can be determined by plotting the equations and finding the valid region. The initial guesses do not have to be accurate, but they should be within the same order of magnitude as the constants in the equations. Trial and error can also be used to find initial values that satisfy all three inequalities. It is important to note that guessing the initial values may affect the results of the solver, but a good iterative solver should not be overly sensitive to the initial conditions. If there are multiple solutions, finding all of them may require further experimentation.
  • #1
PaulaS
19
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
  • #2
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
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
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
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.
 
  • #6
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
Can't you just solve them using matrices in python?
 
  • #8
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
Thanks AlephZero :) Finally, I was able to solve my problem.
 

1. How do I define a system of nonlinear equations in python?

To define a system of nonlinear equations in python, you can use the sympy.Symbol function to create symbolic variables and then use the sympy.Eq function to create equations using those variables.

2. How do I solve a system of nonlinear equations in python?

To solve a system of nonlinear equations in python, you can use the sympy.solve function. This function takes in the equations and variables as arguments and returns a dictionary of solutions.

3. Can I solve a system of nonlinear equations with initial guesses in python?

Yes, you can use the sympy.nsolve function to solve a system of nonlinear equations with initial guesses. This function takes in the equations, variables, and initial guesses as arguments and returns the numerical solutions.

4. How do I plot the solutions of a system of nonlinear equations in python?

To plot the solutions of a system of nonlinear equations in python, you can use the sympy.plot function. This function takes in the equations and variables as arguments and returns a plot of the solutions.

5. Can I solve a system of nonlinear equations with constraints in python?

Yes, you can use the sympy.nsolve function to solve a system of nonlinear equations with constraints. This function takes in the equations, variables, and constraints as arguments and returns the numerical solutions that satisfy the constraints.

Similar threads

  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
670
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
2
Views
894
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
3
Views
928
  • Programming and Computer Science
Replies
17
Views
2K
Back
Top