3

I am using sympy to solve for b1 and b2:

y=x/[x+exp(b1-b2*x)]
x1 = 90; y1 = 0.05 and x2=99;y2=0.95


import sympy
b1,b2 = symbols('b1 b2')
solve([Eq(90*0.05+90*exp(b1-(b2*90))-90, 0.0), Eq(99*0.95+99*exp(b1-(b2*99))-99, 0.0)], [b1, b2])

>>> {b1:29.3930964972769,b2:0.327159886574049}

How do I use these results to plot a S-shaped curve constrained by these values. Y-axis ranges from 0 to 1. x1,y1 and x2,y2 are 2 points on that curve.

user308827
  • 21,227
  • 87
  • 254
  • 417

2 Answers2

6

Using the latest versions of sympy and ipython

In [1]: from sympy import *

In [2]: x, b1, b2 = symbols("x b1 b2")

In [3]: f = x/(x+exp(b1-b2*x))

In [4]: res = {b1:29.3930964972769,b2:0.327159886574049}

In [5]: plot(f.subs(res), (x, 0, 100))

With the output figure: enter image description here

ptb
  • 2,138
  • 1
  • 11
  • 16
4

Sympy allegedly has it's own plotting functionality, but I couldn't get it to work from the manual they had. I'm not an active user of sympy.

But here's a version on how to do it with numpy and matplotlib

  • Define your function so it can act on a np.array
  • Spread points uniformly in some range "x"
  • act on those points with your function "y"
  • plot the set of uniformly spaced points "x" vs function values in those points "y"

    import numpy as np
    import matplotlib.pyplot as plt
    
    def f(a):
       c1 = 0.327159886574049
       c2 = 29.3930964972769
       return a/(a+np.exp(c1-c2*a))
    
    x = np.linspace(0, 1, 500)
    y = f(x)
    plt.plot(x,y)
    plt.show()
    

You should get something like this:

enter image description here

ljetibo
  • 3,048
  • 19
  • 25
  • This answer doesn't use the power of sympy, and therefore, this method can't be used for plotting complicated functions in sympy. Here is how you can use lambdify to get a numpy array, and then use matplotlib to plot the function- https://stackoverflow.com/a/35395086/3797285 – Mohit Pandey May 23 '17 at 23:01
  • Not sure what you mean, you can plot whatever function you want like this. Even non-analytic ones. Naturally, at some point you might have to add your own code that evaluates it and sympy might be smart enough to do it by itself. The answer you linked to deals with lambdify which you can also do. [Even for fairly complicated functions](http://mathb.in/143777). As I said, this answer does not answer OP directly because it doesn't use sympy but you can get the same results with numpy. There is no particular limit on func. complexity, or execution time, that stops numpy but not sympy. – ljetibo May 24 '17 at 09:24
  • 1
    Yes, I agree with you that your answer allows to plot whatever function we want. The linked answer improves your answer. You can first do whatever calculation you want to do in sympy,then import your functions to numpy using lambdify and finally plot it using matplotlib. [detailed example](https://github.com/mohitpandey92/counterdiabatic-driving/blob/master/codes/CD_driving.ipynb) – Mohit Pandey May 25 '17 at 13:25
  • 1
    Oh, I get what you mean now. You could do the same in numpy, for more complex operations than derivation you would also need scipy most likely. With lambdify it's just easier and much shorter to use sympy. – ljetibo May 25 '17 at 17:42