Python Forum
How to Solving non-linear equation using scipy.optimize fsolve with variable list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to Solving non-linear equation using scipy.optimize fsolve with variable list
#1
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,).
Reply
#2
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.
Reply
#3
(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,
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  backtesting can't optimize Running_Code 0 30 32 minutes ago
Last Post: Running_Code
Question written RK solving same equation with different graphs acbshinn 1 1,334 Feb-09-2024, 12:33 PM
Last Post: Gribouillis
  How can I multithread to optimize a groupby task: davisc4468 0 1,245 Jun-30-2023, 02:45 PM
Last Post: davisc4468
  Positive integral solutions for Linear Diophantine Equation august 4 2,455 Jan-13-2023, 09:48 PM
Last Post: Gribouillis
  Solving an equation by brute force within a range alexfrol86 3 4,385 Aug-09-2022, 09:44 AM
Last Post: Gribouillis
  Split string using variable found in a list japo85 2 2,269 Jul-11-2022, 08:52 AM
Last Post: japo85
  do you have an idea to optimize this code[recursion]]? netanelst 4 2,321 May-20-2022, 06:41 PM
Last Post: jefsummers
  Solving equation equal to zero: How to resolve the syntax error? alexfrol86 3 3,182 Feb-21-2022, 08:58 AM
Last Post: deanhystad
  SOlving LInear Equations in Python(Symoy, NUmpy) - Coefficient Problem quest 3 2,765 Jan-30-2022, 10:53 PM
Last Post: quest
  Optimization using scipy.optimize KaneBilliot 3 3,043 Nov-30-2021, 08:03 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020