import numpy as np
x = np.array([-2, 1.5, 0, 1/2*np.pi, np.pi]); print(x);
xa = np.abs(x); print(x); print(xa);
b = np.cos(x); print(b);
c = np.sin(x); print(c);
d = np.tan(x); print(d);
bi = np.arccos(b); print(bi); print(x);
ci = np.arcsin(c);
di = np.arctan(d);
e = np.arctan2(-1, 1); print(e);
f = np.arctan2(1, 1); print(f);
print(np.pi/4)
y = np.array([-2, -1, 1, 2]);
ys = np.sinh((y)); print(ys);
ysp1 = np.exp(y); ysp2 = np.exp(-y);
ys2 = 1/2*(ysp1 - ysp2); print(ys); print(ys2);
c = 2 + 3j;
print(np.abs(c));
print(np.sqrt(2**2 + 3**2));
print(np.real(c));
print(np.imag(c));
print(np.conj(c));
d = np.array([2+3j, 1-1j, 4+np.pi*1j]); print(d);
dc = np.conj(d); print(dc)
import matplotlib.pyplot as plt
plt.rcParams.update({"text.usetex":True});
%config InlineBackend.figure_format = 'svg';
x = np.linspace(-2, 2, 100);
y = np.cos(2*np.pi*x);
z = np.sin(2*np.pi*x);
# We can change the linspec of the plot. Here we plot cos(x) as a dotted line
plt.plot(x, y,'.', label="$\\cos(x)$");
plt.plot(x, z, label="$\\sin(x)$");
plt.plot(x, np.tan(x), label="$\\tan(x)$");
# Specify the x and y labels
plt.xlabel("$x$"); # Latex rendering
plt.ylabel("$y$");
# Enable the legend. We have already specified the values earlier in plot command
plt.legend();
# Specify the plot title
plt.title("Some functions plotted");
# Specify the limits of x and y axis
plt.ylim(-1,1);
plt.xlim(0, 2);
import scipy.special as sp
x = np.linspace(0, 10);
y = sp.jv(0,x);
plt.plot(x, y);
plt.xlabel("$x$");
plt.ylabel("$J_0$");
x = np.linspace(0, 10);
for i in np.arange(0,4):
y = sp.jv(i,x);
plt.plot(x, y, label="$J_%d$"%i); # In C, sprintf
plt.xlabel("$x$");
plt.ylabel("Bessel function");
plt.legend();
# Turn on Grid
plt.grid();
# useful to set properties of the axes
# we query the axes and then we can modify it
ax = plt.gca();
# Set the aspect ratio of x and y axes
ax.set_aspect(2.5)
def circ_properties(r):
# local variables - scope is limited to the definition of the function
area = np.pi*r**2;
circ = 2*np.pi*r;
return area, circ
We calculate the area and circumference and store them in variables and then return those values
We can now call the function for any radius
props = circ_properties(1);
print("For a circle with radius: ", 1)
print("The area is: ", props[0]);
print("Circumferance: ", props[1]);
def fx(x, c):
return x**2 + c*x + 2*c, np.exp(c);
x = np.linspace(-3, 3);
for i in np.arange(-2, 3):
y, e = fx(x, i);
plt.plot(x, y, label="$c=%d$"%i);
plt.legend();
print(e);
print(np.exp(2));
print(x);
print(y)
print(np.shape(x)); print(np.shape(y))
np.savetxt("Datafile.txt", (x, y));
d = np.loadtxt("Datafile.txt");
print(np.shape(d))
e = np.c_[x, y]; print(e); np.savetxt("data_human.txt", e);