using python to solve a nonlinear equation

后端 未结 2 1024
借酒劲吻你
借酒劲吻你 2021-01-03 11:37

I have never used python but Mathematica can\'t handle the equation I am trying to solve. I am trying to solve for the variable \"a\" of the following equations where s, c,

2条回答
  •  北海茫月
    2021-01-03 12:20

    I think its worth examining the behaviour of the function before atempting to solve it. Without doing that you dont know if there is a unique solution, many solutions, or no solution. (The biggest problem is many solutions, where numerical methods may not give you the solution you require/expect - and if you blindly use it "bad things" might happen). You examine the behaviour nicely using scipy and ipython. This is an example notebook that does that

    # -*- coding: utf-8 -*-
    # 3.0
    
    # 
    
    s = 10014.6
    c = 6339.06
    mu = 398600.0
    dt = 780.0
    
    # 
    
    def sin_alpha_2(x):
        return numpy.sqrt(s/(2*x))
    def sin_beta_2(x):
        return numpy.sqrt((s-c)/(2*x))
    def alpha(x):
        return 2*numpy.arcsin( numpy.clip(sin_alpha_2(x),-0.99,0.99) )
    def beta(x):
        return 2*numpy.arcsin( numpy.clip(sin_beta_2(x),-0.99,0.99) )
    
    # 
    
    def fn(x):
        return alpha(x)-beta(x)-numpy.sin(alpha(x))+numpy.sin(beta(x)) - dt * numpy.sqrt( mu / numpy.power(x,3) )
    
    # 
    
    xx = numpy.arange(1,20000)
    pylab.plot(xx, numpy.clip(fn(xx),-2,2) )
    

    Graph of fn

    # 
    
    xx=numpy.arange(4000,10000)
    pylab.plot(xx,fn(xx))
    

    enter image description here

    # 
    
    xx=numpy.arange(8000,9000)
    pylab.plot(xx,fn(xx))
    

    enter image description here

    This shows that we expect to find a solution with a between 8000 and 9000. The odd kink in the curve at about 5000 and earlier solution at about 4000 is due to the clipping required to make arcsin behave. Really the equation does not make sense below about a=5000. (exact value is the a0 given in Rays solution). This then gives a nice range that can be used with the techniques in Rays solution.

提交回复
热议问题