Search code examples
pythonscipynumerical-methodsscipy-optimize

Coupled set of equations - Wrong answer from scipy's fsolve


I'm trying to solve the following coupled equations:

x = 1;

y - 0.5*y - 0.7*v = 0;

w - 0.7*x - 0.5*x = 0;

v = 1.

(I know that the equations = 1 seem unnecessary but I need them for a later generalization of the code). My code is the following:

import numpy as np
from scipy.optimize import fsolve

def myFunction(z):
   x = z[0]
   y = z[1]
   w = z[2]
   v = z[3]

   F = np.empty((4))
   F[0] = 1
   F[1] = y - 0.5*y - 0.7*v
   F[2] = w - 0.7*x - 0.5*w
   F[3] = 1
   return F

zGuess = np.array([1,2.5,2.5,1])
z = fsolve(myFunction,zGuess)
print(z)

The answer I get is [-224.57569869, -314.40597772, -314.40597817, -224.57569837], but I would expect [1, 1.4, 1.4, 1]. Why does fsolve is unable to find the answer to this simple set of equations? What's more: why the values of x and v, which are not modified at any point, are not the same as those of the initial guess?


Solution

  • To convert the requirement x = 1 to code, rewrite it as x - 1 = 0. That says that the line F[0] = 1 should be changed to F[0] = x - 1. Similarly, the line F[3] = 1 should be F[3] = v - 1.