Using scipy to solve nonlinear equations _scipy

Source: Internet
Author: User

We have two normal distributions: N (2,4) and N (3,5), and now we want to find the equal probability point near the mean value of the two, as shown in the figure:


where Difference=n (2,4)-N (3,5), which is the function f in our following code

#! /usr/bin/python3
#-*-coding:utf-8-*-from

scipy.optimize import fsolve
import numpy as NP
import Math

#定义函数 (equation f (x|arg) =0)
def f (x,*arg): #len (ARG) =4, arg= (miu_1,miu_2,sigma_1,sigma_2)
    f1=math.exp (-( X-arg[0]) **2/2/arg[2]**2/arg[2]/math.sqrt (2*math.pi)
    f2=math.exp (-(x-arg[1)) **2/2/arg[3]**2)/arg[3]/ MATH.SQRT (2*MATH.PI) return
    (F1-F2)


x0= (2.5,2,3,4,5)
result=fsolve (f,x0=x0[0],args=x0[1:])
Print (Result)
#输出: [5.19949597]

We can see that the root of the solution is very close to the position of the callout on our graph, which means that the root is the solution we need.
Note that in the Fsolve function, it is noted that the first number of f parameters are the variables required in our equation (x0 length), which are the parameters in the equation (args)
For equations with multiple roots, it is important to x0 the numerical value because the initial solution largely determines the direction of the root convergence.
In order to obtain the roots near the center of the two distributions, the initial solution is best chosen as the mean value of two mean, where we set the x0 to (2+3)/2=2.5
If set to 1, you can see that the derivative of the difference at 1 is almost 0, so the root will converge very wonderful, the actual operation done, the output result is [-152.18013068], far away from the distribution center position. Because Fsolve has a default parameter xtol=1.49012e-08, as long as the difference between the two iterations results is less than Xtol will terminate the iteration, so it is not difficult to understand why the equation has only two small roots, but it has come to a strange result.
If you set the x0 to 0, you can get another root: [-4.75505152], which is farther away from the distribution center.

Because here we only solve a variable x, so when we define a function, we simply return a value (a constraint), and if we solve the multivariate equation, we return a list when we define the function, and fsolve automatically asks the root of all elements of the list to be 0.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.