Python Forum

Full Version: How to Solving non-linear equation using scipy.optimize fsolve with variable list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi every one,
when i am trying solve this equation using fsolve with variables as list
[Image: 2ZOfu.png]
can any help me out.
from optimize import fsolve
import numpy as np
T = np.array([137,145,150,152,159,160,184])
Di =np.array([1,0,1,1,1,0,1])
r = 5.0

def lnL(P):
    mu, sigma = P
    return -r*np.log(sigma)-(1/2)*np.sum(Di*((T-mu)/sigma)**2)+np.sum(1-Di)

sol = fsolve(lnL, (15.0,258.0))
sol
i am getting this error
fsolve: there is a mismatch between the input and output shape of the 'func' argument 'lnL'.Shape should be (2,) but it is (1,).
This is not an equation because there is no = sign. It is merely a scalar expression of the two variables mu and sigma. If you want to solve the equation "expression = 0", there will be a curve of solutions in the mu, sigma plane. If you look at the expression, you'll see that it is a quadratic equation in mu, which means that you could solve it explicitly and give mu as a function of sigma.
(Jun-09-2020, 04:01 PM)Gribouillis Wrote: [ -> ]This is not an equation because there is no = sign. It is merely a scalar expression of the two variables mu and sigma. If you want to solve the equation "expression = 0", there will be a curve of solutions in the mu, sigma plane. If you look at the expression, you'll see that it is a quadratic equation in mu, which means that you could solve it explicitly and give mu as a function of sigma.

hi,
Actually it is equation only, while taking screen crop i missed that, here challenging is two variables are in list[],while passing them, i am getting error,
djhack Wrote:here challenging is two variables are in list[],while passing them, i am getting error,
This is not the problem. You are getting an error because you have a scalar function (1 real value) of two variables (2 real values). fsolve works when a function of n variables produces n values. This is the first line of the documentation, see the func argument.

Your problem doesn't have a unique solution.

I understand that you want to use an equation solver as a magic black box, but it can only solve the problems for which it was designed.