简体   繁体   中英

Solving 3 non linear equations using fsolve

I want to find the valuews of a,b,c. It gives me error:

from scipy.optimize import fsolve
def equations(p):
    a,b,c = p
    return (a*np.log10([-b])+c, a*np.log10([100-b])+c-100, a*np.log10([80-b])+c-20)

a,b,c =  fsolve(equations, (1, 1, 1))

print(a,b,c)
> print(a,b,c)

TypeError: fsolve: there is a mismatch between the input and output shape of the 'func' argument 'equations'.Shape should be (3,) but it is (3, 1).

You cannot use square brackets [] in your equations. They have special meaning in Python. You should use round brackets for enforcing sequence of mathematical operations. The following code gets rid of the error but you need to work some more on the math because fsolve is not converging to a solution for your system. You are giving initial guess for b as 1 but then you do log10(-b) . Logs of negative numbers are not defined.

import numpy as np
from scipy.optimize import fsolve
def equations(p):
    a,b,c = p
    return ( a*np.log10(-b)+c, a*np.log10(100-b)+c-100,
        a*np.log10(80-b)+c-20 )

a,b,c =  fsolve(equations, ( 1, 1, 1 ))

print(a,b,c)

If we write down your system on paper you have the following 3 equations.

Substituting the first equation in the second and third, you are left with

The above two equations cannot be simultaneously true. So your system has no answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM