This is a basic tutorial on using Jupyter to use the scipy modules.
Install matplotlib through conda via:
conda install -y matplotlib
Below we plot a sine function from 0 to 2 pi. Pretty much what you would expect:
In [4]:
import math
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 3 * math.pi, 50)
y = np.sin(x)
plt.plot(x, y)
plt.show()
The x values limit the range of the plot.
Let's get help on the plt.plot function, so as to understand how to use it, in addition to the tutorial at http://matplotlib.org/users/pyplot_tutorial.html
In [5]:
help(plt.plot)
Let's add in 'bo' string to the mix to get dots on the trace:
In [10]:
import math
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2 * math.pi, 50)
y = np.sin(x)
plt.plot(x, y, 'bo')
plt.show()
Let's try to add two traces, the second one is a cosine function:
In [12]:
import math
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,2 * math.pi, 50)
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x, y1, 'bo', x, y2, 'r+')
plt.show()
In [ ]:
In [8]:
import math
import numpy as np
from scipy import linalg, optimize
# Here, we called this function "func2" which is pretty arbitrary. You will need to use better name in practice, of course:
def func2(x):
return np.sin(x)
optimize.fmin(func2, math.pi - 0.01)
Out[8]:
Pretty much what we expected. There is a minimum of -1 for this sine wave function (amplitude of 1 here ... would have been different if we multiplied the sine wave by some other factor). We can call the f function to see the value at that point which is pretty darn close to -1:
In [9]:
func2(4.71237414)
Out[9]:
In [19]:
math.pi * 2 * 0.75
Out[19]:
In [22]:
help(optimize.root)
Let's evaludate the f function (which we know is a sine function) at not quite at the point where it is zero (at pi):
In [10]:
func2(math.pi * 0.75)
Out[10]:
In [12]:
import math
import numpy as np
from scipy import linalg, optimize
# Here, we called this function "func2" which is pretty arbitrary. You will need to use better name in practice, of course:
def func2(x):
return np.sin(x)
optimize.root(func2, math.pi * 0.75)
Out[12]:
So it found the root at pi. Notice the "x" value at the end of the output.
You can turn off the verbose output using keyword arguments to the optimize.root function.