solving non-linear set of equations in python

187 views Asked by At

I would like to know how to solve a set of equations using python.

y = arccosh(A/cos(x)) and y = arcsinh(B/sin(x))

A, B are real numbers. I need to solve the problem for a lot of different values for A and B.

I would like to solve this set of equations in the interval 0 to pi (so x lies between 0 and pi, y is unrestricted). Further I know that there is one solution or that there are no solutions.

Normally I would do something like this:

def f(x, A, B):
    return arccosh(A/cos(x)) - arcsinh(B/sin(x))

if f(0, A, B) * f(pi, A, B) < 0  #there is a solution
    x_solution = scipy.optimize.bisect(f, 0, pi, args=(A,B))
    y_solution = arcsinh(B/sin(x))
    print('a solution has been found')
else:                            #there is no solution
    print('no solution')

The problem with this is that arccosh() is not defined for arguments smaller than 1. So, for some A-values f(0, A, B) or f(pi, A, B) returns: NaN. I also tried using other python functions such as optimize.fsolve, but if I use this and there is no solution, I get an error:

RuntimeWarning: The iteration is not making good progress, as measured by the improvement from the last ten iterations. warnings.warn(msg, RuntimeWarning)

(if there is a solution scipy.optimize.fsolve gives the correct answer)

I need to to this for like a million different values of A and B.

Does someone have an idea how to best solve this? Thanks a lot folks!

0

There are 0 answers