In [3]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
nx = 44
ny = 44
x1,y1 = 5,5
X,Y = np.meshgrid(np.linspace(0,x1,nx),np.linspace(0,y1,ny))
X.shape
Out[3]:
In [21]:
def gradient_f_x(x,y):
return( 2*np.cos(2*x)*np.cos(y) )
def gradient_f_y(x,y):
return( -np.sin(2*x)*np.sin(y) )
In [22]:
import sympy
from sympy.abc import x,y
In [23]:
F_sympy = sympy.sin(2*x)*sympy.cos(y)
F_sympy.diff(y)
Out[23]:
In [32]:
gradient_f_x_z_sympy = sympy.lambdify((x,y),F_sympy.diff(x),"numpy")
gradient_f_y_z_sympy = sympy.lambdify((x,y),F_sympy.diff(y),"numpy")
In [31]:
gradient_f_x(1,2),gradient_f_x_z_sympy(1,2)
Out[31]:
In [43]:
gradient_f_x_z_sympy(1,2),gradient_f_y_z_sympy(1,2)
Out[43]:
In [44]:
f = lambda X_,Y_:np.sin(2*X_)*np.cos(Y_)
Z = f(X,Y)
plt.axes().set_aspect('equal')
plt.arrow(1,2,0.34635637, -0.826821810,head_width=0.4)
plt.contourf(X,Y,Z)
Out[44]:
In [41]:
from ipywidgets import widgets
from ipywidgets import interact
In [42]:
@interact(x0=widgets.FloatSlider(min=0,max=5),\
y0=widgets.FloatSlider(min=0,max=5) )
def myplot(x0,y0):
plt.plot( x0,y0,'ro')
plt.contourf(X,Y,Z)
plt.show()
In [39]:
myplot(1,2)
In [ ]: