Practical 2
In [1]: from mpl_toolkits import mplot3d
from pylab import *
import numpy as np
Q1.i)
In [2]: def f(x,y):
return np.sin(x**2+y**2)
x=linspace(0,10)
y=linspace(0,10)
M,N=np.meshgrid(x,y)
Z=f(M,N)
ax=axes(projection='3d')
ax.contour3D(M,N,Z,50)
xlabel('X')
ylabel('Y')
title('3D Contour Graph')
show()
ii)
In [3]: def f(x,y):
return np.sin(x)+cos(y)
x=linspace(-10,10)
y=linspace(-10,10)
M,N=np.meshgrid(x,y)
Z=f(M,N)
ax=axes(projection='3d')
ax.contour3D(M,N,Z,50)
xlabel('X')
ylabel('Y')
title('3D Contour Graph')
show()
iii)
In [4]: def f(x,y):
return x**2+y**2
x=linspace(-6,6)
y=linspace(-6,6)
M,N=np.meshgrid(x,y)
Z=f(M,N)
ax=axes(projection='3d')
ax.contour3D(M,N,Z,50)
xlabel('X')
ylabel('Y')
title('3D Contour Graph')
show()
Q2.
In [5]: def f(x,y):
return np.cos(x**2+y**2-0.5)
x=linspace(-1,1)
y=linspace(-1,1)
M,N=np.meshgrid(x,y)
Z=f(M,N)
ax=axes(projection='3d')
ax.plot_surface(M,N,Z)
xlabel('X')
ylabel('Y')
title('Surface Plot')
show()
Q3.
In [6]: from pylab import *
from numpy import *
x=linspace(0,2*pi)
y=sin(x)-e**x+3*x**2-log(x)
plot(x,y)
show()
D:\Data_Folder\Modern.MODERN-STUDENTS\AppData\Local\Temp/ipykernel_10136/
214030037.py:4: RuntimeWarning: divide by zero encountered in log
y=sin(x)-e**x+3*x**2-log(x)
Q4.
In [7]: import matplotlib.pyplot as plt
import numpy as np
from math import *
x=np.linspace(0,5,20)
def f(x):
return np.sin(x)
def g(x):
return np.cos(x)
def h(x):
return np.exp(x)
def k(x):
return x**2
plt.subplot(2,2,1)
plt.plot(x,f(x),color='r',linestyle="--")
plt.title('sinx')
plt.legend()
plt.subplot(2,2,2)
plt.plot(x,g(x),color='g',linestyle=":")
plt.title('cosx')
plt.legend()
plt.subplot(2,2,3)
plt.plot(x,h(x),color='y',linestyle="-")
plt.title('exp(x)')
plt.legend()
plt.subplot(2,2,4)
plt.plot(x,k(x),color='b',linestyle="-.")
plt.title('x**2')
plt.legend()
plt.show()
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
Q6.
In [8]: import matplotlib.pyplot as plt
import numpy as np
def f(x):
if -10<=x<5:
return(x**2+4)
else:
return(3*x+9)
x=np.linspace(-10,10)
y=[]
for i in x:
y.append(f(i))
plt.plot(x,y)
plt.show()
Q5.
In [9]: from mpl_toolkits import mplot3d
from pylab import *
import numpy as np
ax=axes(projection='3d')
t=np.linspace(10,20,100)
x=cos(2*t)
y=sin(2*t)
z=t
ax.set_xlabel('x axis')
ax.set_ylabel('y axis')
ax.set_zlabel('z axis')
ax.plot3D(x,y,z,color='r',linestyle='--')
title('Line in 3D')
show()
Q7.
In [10]: from mpl_toolkits import mplot3d
from pylab import *
import numpy as np
def f(x,y):
return np.log(x**2*y**2)
ax=axes(projection='3d')
x=np.linspace(-5,5)
y=np.linspace(-5,5)
M,N=np.meshgrid(x,y)
Z=f(M,N)
ax.contour3D(M,N,Z,100)
xlabel('x axis')
ylabel('y axis')
title('Contour 3D Graph')
show()
Q8.
In [11]: from mpl_toolkits import mplot3d
from pylab import *
import numpy as np
def f(x,y):
return np.exp(-x**2-y**2)
ax=axes(projection='3d')
x=np.linspace(-5,5)
y=np.linspace(-5,5)
M,N=np.meshgrid(x,y)
Z=f(M,N)
ax.plot_wireframe(M,N,Z,rstride=2,cstride=2)
xlabel('x axis')
ylabel('y axis')
title("3D Wireframe")
show()