You are on page 1of 50

11/7/23, 10:12 AM MAT541D - Jupyter Notebook

LAB 1

18.7.23

1.Introduction to Basic Commands and plotting of graphs using


matplotlib
plt.axis([-6, 6, -6, 6])
5
In [2]: x = [1, -3, 4, -4]
1 6
import matplotlib.pyplot as plt y = [2, 3, -3, -5]
2 7
plt.axhline(0, lw=0.5, color='black') plt.plot(x, y, linewidth = 0, marker = 'o',
3 markerfacecolor = 'red', marke 8
plt.axvline(0, lw=0.5, color='black') plt.title("Plot")
4
Out[2]: Text(0.5, 1.0, 'Plot')

localhost:8888/notebooks/MAT541D.ipynb 1/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook

In [3]: x = [1, -3, 4, -4]


1
2 9
3 10 11 12
4 y = [2, 3, -3, -5]
5 plt.axis([-6, 6,
6 -6, 6])
7 plt.plot(x, y, "*")
8 plt.annotate("P(1,
2)", (2, 3)) plt.annotate("S(-4,
plt.annotate("Q(-3, -5)", (-4, -4))
3)", (-4, 4)) plt.axhline()
plt.annotate("R(4, plt.axvline()
-3)", (3, -4)) plt.show()

localhost:8888/notebooks/MAT541D.ipynb 2/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook

In [4]: import matplotlib.pyplot as plt


1
2
# Define the coordinates for the points of the star
3
star_x = [0, 0.1, 0.4, 0.2, 0.3, 0, -0.3, -0.2, -0.4, -0.1, 0]
4
star_y = [0.8, 0.4, 0.4, 0, -0.4, -0.2, -0.4, 0, 0.4, 0.4, 0.8]
5
# Create a figure and axis
6
fig, ax = plt.subplots()
7
# Plot the star by connecting the points with lines
8
ax.plot(star_x, star_y, marker='o', linestyle='-', color='b', markersize=8
9
# Set axis limits
10
ax.set_xlim(-1, 1)
11
ax.set_ylim(-1, 1)
12
# Set axis labels
13
ax.set_xlabel('X-axis')
14
ax.set_ylabel('Y-axis')
15
# Set plot title
16
ax.set_title('Star Plot')
17
# Show the plot
18
plt.grid()
19
plt.show()

localhost:8888/notebooks/MAT541D.ipynb 3/47

11/7/23, 10:12 AM MAT541D - Jupyter Notebook 1.1.Scatter plot 2D

In [6]: import matplotlib.pyplot as plt


1
2
import numpy as np
3
years = np.arange(2000, 2021)
4
population = np.random.randint(1000000, 1500000, len(years))
5
# Create a scatter plot
6
plt.figure(figsize=(10, 6)) # Set the figure size
7
plt.plot(years, population, marker='o', color='b', label='Population')
8
# Set axis labels and title
9
plt.xlabel('Year')
10
plt.ylabel('Population')
11
plt.title('Population vs. Year')
12
# Add a grid
13
plt.grid(True)
14
# Display the legend
15
plt.legend()
16
# Show the plot
17
plt.show()

localhost:8888/notebooks/MAT541D.ipynb 4/47

11/7/23, 10:12 AM MAT541D - Jupyter Notebook 1.2.Scatter plot-3D

In [7]: import matplotlib.pyplot as plt


1
2 plt.axes(projection
3 = "3d") #plt.show()
4 shows 3D axes
5 # Can also use
6 #fig = p(t,
7 figure(figsize =
8 {12, 8})) #ax =
9 fig.gca(projection =
10 11 12 13 14 15 '3d') x =
import numpy as np ax.scatter(3, 2, 4)
from mpl_toolkits y = ax.scatter(4, 1,
import mplot3d 2)
#Single point z = ax.scatter(-1,
# Create axes 4, -2)
ax = plt.show()
localhost:8888/notebooks/MAT541D.ipynb 5/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook

In [8]: ax = plt.axes(projection = "3d")


1
2 3 4 5 6 (-1, 4,
7 -2)
x = (3, 2,plt.plot(x
4) y = (4,, y, z)
1, 2) z = plt.show()
localhost:8888/notebooks/MAT541D.ipynb 6/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook

In [9]: ax = plt.axes(projection = "3d")


1
2
x_values = np.random.randint(0, 100, (500))
3
y_values = np.random.randint(0, 100, (500))
4
z_values = np.random.randint(0, 100, (500))
5
ax.scatter(x_values, y_values, z_values)
6
ax.set_title("3D Scatter Plot")
7
plt.show()

localhost:8888/notebooks/MAT541D.ipynb 7/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook

In [10]: ax = plt.axes(projection = "3d")


1
2
x_values = np.random.randint(0, 100, (500))
3
y_values = np.random.randint(0, 100, (500))
4
z_values = np.random.randint(0, 100, (500))
5
ax.plot(x_values, y_values, z_values)
6
ax.set_title("3D Scatter Plot")
7
plt.show()

localhost:8888/notebooks/MAT541D.ipynb 8/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
1.3.Helix
z = linspace (0, 15,
1000) 5
In [11]:
x = sin(z)
1
6
from matplotlib.pyplot
y = cos(z)
import * 2
7
from numpy import *
ax.plot3D(x, y, z)
3
8
ax = axes(projection=
show()
'3d') 4
1

Assignment 1

2D Graphs

In [ ]:
matplotlib.pyplot as
In [12]: plt 2
1 import numpy as np
import 3

1.Line

localhost:8888/notebooks/MAT541D.ipynb 9/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
4
In [13]: plt.plot(x, y,
1 linewidth=4) 5
plt.axis([0, 5, 0, plt.title("Line")
10]) 2 6
x = np.array([1, 2, plt.show()
3, 4]) 3 7
y = x*2

2.Circle
localhost:8888/notebooks/MAT541D.ipynb 10/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
3
In [14]: plt.ylim( -0.95 , 0.95 )
1 4
plt.scatter( 0 , 0 , s = plt.title( "Circle" )
10000, color = 'red' ) 2 5
plt.xlim( -0.85 , 0.85 ) plt.show()

3.Rectangle
localhost:8888/notebooks/MAT541D.ipynb 11/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
4
In [15]: x = [-4, 4, 4, -4, -4]
1 5
plt.axhline(0, lw=0.5, color='black') y = [4, 4, -4, -4, 4]
2 6
plt.axvline(0, lw=0.5, color='black') plt.plot(x, y, linewidth = 3, marker = 'o',
3 markerfacecolor = 'red', marke 7
plt.axis([-6, 6, -6, 6]) plt.title("Rectangle")
Out[15]: Text(0.5, 1.0, 'Rectangle')
4.Parabola

localhost:8888/notebooks/MAT541D.ipynb 12/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
plt.plot(x, x**2,
In [16]: linewidth = 3) 3
1 plt.title("Parabola")
x = np.linspace(-5,5, 4
100) 2 plt.show
Out[16]: <function matplotlib.pyplot.show(close=None, block=None)>

5.Hexagon
localhost:8888/notebooks/MAT541D.ipynb 13/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
4
In [21]: x = [0, 2, 2, 0, -2, -2, 0]
1 5
plt.axhline(0, lw=0.5, color='black') y = [4, 2, -2, -4, -2, 2, 4]
2 6
plt.axvline(0, lw=0.5, color='black') plt.plot(x, y, linewidth = 3, color='red',
3 marker = 'o', markerfacecolor = 7
plt.axis([-6, 6, -6, 6]) plt.title("Hexagon")
Out[21]: Text(0.5, 1.0, 'Hexagon')
3D Graphs
In [28]: from matplotlib.pyplot
1 import *
from mpl_toolkits.mplot3d 3
import Axes3D 2 from numpy import *

1.Sphere

localhost:8888/notebooks/MAT541D.ipynb 14/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
5
In [34]: y =
1 10*outer(sin(u),sin(v))
ax = axes(projection = 6
'3d') z =
2 10*outer((ones(size(v))),
u = linspace(0,2*pi,100) cos(v)) 7
3 ax.plot_surface(x,y,z,col
v = linspace(0,pi,100) or='b') 8
4 title("Sphere")
x = 9
10*outer(cos(u),sin(v)) show()
2.Plane

localhost:8888/notebooks/MAT541D.ipynb 15/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
8 , y, zz)
In [33]: 9 title('Plane')
1 10 ax.set_xlabel('$X$
2 xx, zz = meshgrid ')
3 (range(10), ax.set_ylabel('$Y$
4 range(10)) y = 0 ')
5 ax = ax.set_zlabel('$Z$
6 axes(projection = ')
7 "3d") show()
ax.plot_surface(xx

3.Helix
localhost:8888/notebooks/MAT541D.ipynb 16/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
8 yval = sin (t)
In [31]: 9 zval = t
1 10 11 ax.plot3D (xval,
2 ax = axes yval, zval,
3 (projection = 'red') title
4 '3d') t = ("Helix")
5 linspace(-10, xlabel("X")
6 10, 100) ylabel('Y')
7 xval = cos (t) None

4.Cylinder
localhost:8888/notebooks/MAT541D.ipynb 17/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
10*outer(ones(size(u)),c
In [36]: os(u)) 5
1 y =
ax = axes(projection = 10*outer(ones(size(u)),s
'3d') in(u)) 6
2 z =
u = linspace(0,2*pi,100) outer(v,ones(size(v)))
3 7
v = linspace(0,8,100) ax.plot_surface(x,y,z,co
4 lor='cyan') 8
x = show()

5.Quiver Plot
localhost:8888/notebooks/MAT541D.ipynb 18/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
sin(pi*x)*cos(pi*y)*cos(pi*z)
In [37]: 7
1 v =
fig = figure() -cos(pi*x)*sin(pi*y)*cos(pi*z)
2 8
ax = axes(projection= '3d') w =
3 (sqrt(2.0/3.0)*cos(pi*x)*cos(pi
x, y, z = meshgrid *y)*sin(pi*z)) 9
(arange(-0.8, 1, 0.2), 4 ax.quiver(x, y, z, u, v, w,
arange(-0.8, 1, 0.2), length=0.3)
5 10
arange(-0.8, 1, 0.8)) title("Quiver plot")
6 11
u = show()
LAB 2
1.8.23

localhost:8888/notebooks/MAT541D.ipynb 19/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook 1.Contour Plots

In [38]: 5
1 z = linspace (0, 15, 1000)
r = input('Enter the radius 6
of the circle: ') 2 x = cos(z)
from matplotlib.pyplot import 7
* y = 5*sin(z)
3 8
from numpy import * ax.plot3D(x, y, z)
4 9
ax = axes(projection= '3d') show()
Enter the radius of the circle: 2

localhost:8888/notebooks/MAT541D.ipynb 20/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
4
In [40]: y = [1, 2, 3]
1 5
import x, y= np.meshgrid(x,
matplotlib.pyplot as y)
plt 2 6
import numpy as np plt.scatter(x, y)
3 7
x = [3, 4, 5] plt.show()
In 1 )
[41]: print(x

[[3 4 5]
[3 4 5]
[3 4 5]]
[42]: print(y
1 )
In

[[1 1 1]
[2 2 2]
[3 3 3]]

localhost:8888/notebooks/MAT541D.ipynb 21/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
X, Y =
In [43]: np.meshgrid(xlist,
1 ylist) 4
xlist = np.linspace(3, print(xlist)
5, 3) 2 5
ylist = np.linspace(1, print(ylist)
3, 3) 3 6
print(X) print(Y)
7
[3. 4. 5.]
[1. 2. 3.]
[[3. 4. 5.]
[3. 4. 5.]
[3. 4. 5.]]
[[1. 1. 1.]
[2. 2. 2.]
[3. 3. 3.]]
1 2 r(X,Y)
plt.scatteplt.show()
In [44]:

localhost:8888/notebooks/MAT541D.ipynb 22/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
X, Y = np.meshgrid(x, y)
In [45]: 4
1 Z = sin(2*X) + cos(2*Y)
x = np.linspace(0, 5, 50) 5
2 plt.contour(X, Y, Z,
y = np.linspace(0, 5, 40) colors = 'black') 6
3 None
In [46]: cmap = 'Blues') 2
1 None
plt.contour(X, Y, Z, 20,

localhost:8888/notebooks/MAT541D.ipynb 23/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
4
In [47]: Z = np.sin(X)**10 + np.cos(10 +
1 X * Y) * np.cos(X) 5
x = np.linspace(0, 5, 50) plt.contour(X, Y, Z, colors =
2 'black')
y = np.linspace(0, 5, 40) 6
3 None
X, Y = np.meshgrid(x, y)
In [48]: cmap = 'Blues') 2
1 None
plt.contour(X, Y, Z, 20,

localhost:8888/notebooks/MAT541D.ipynb 24/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
plt.contourf(X, Y, Z, 20,
In [49]: cmap = 'RdGy') 2
1 plt.colorbar();
In [50]: 3, colors = 'black') 2
1 plt.clabel(contours, inline=True,
contours = plt.contour(X, Y, Z, fontsize = 8);

localhost:8888/notebooks/MAT541D.ipynb 25/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
3
In [51]: x, y = meshgrid(linspace(-5, 5,
1 10), linspace(-5, 5, 10)) 4
import matplotlib.pyplot as plt u = x/sqrt(x**2 + y**2)
2 5
from numpy import * v = y/sqrt(x**2 + y**2)
6 8
plt.quiver(x, y, u, v) None
7 9
plt.show()

Assignment 2
7.8.23

localhost:8888/notebooks/MAT541D.ipynb 26/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook 1.Isotherm-Wave Equation for

Temperature

In [53]: import matplotlib.pyplot as plt


1
2
import numpy as np
3
x = np.linspace(0, 5, 50)
4
y = np.linspace(0, 5, 40)
5
X, Y = np.meshgrid(x, y)
6
Z = np.sin(np.sqrt(X**2 + Y**2))
7
plt.title('Isotherm Contour Plot')
8
plt.contourf(X, Y, Z, 20, cmap = 'coolwarm')
9
plt.colorbar();

localhost:8888/notebooks/MAT541D.ipynb 27/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook 2.Mapping

In [54]: x = np.linspace(0, 5, 50)


1
2
y = np.linspace(0, 5, 40)
3
X, Y = np.meshgrid(x, y)
4
Z = np.sin(np.sqrt(X**2 + Y**2)) + np.cos(X) + np.cos(Y)
5
plt.title('Mapping')
6
plt.contourf(X, Y, Z, 20, cmap = 'BrBG')
7
plt.colorbar();
localhost:8888/notebooks/MAT541D.ipynb 28/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook

3.Infection Rate
Z = np.exp(-0.1 * (X**2 + Y**2))
5
In [55]:
2
1
6
x = np.linspace(0, 5, 50)
contour_map = plt.contourf(X, Y, Z,
2
levels=20, cmap='Reds') 7
y = np.linspace(0, 5, 40)
plt.colorbar(contour_map,
3
label='Infection Rate'); 8
X, Y = np.meshgrid(x, y)
4

LAB 3
22.8.23
1.Curves in Spcae,Functions of two varibales and Vector Fields

1.1 Visualizing a space curve

localhost:8888/notebooks/MAT541D.ipynb 29/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook

In [56]: import matplotlib.pyplot as plt


1
2 import * from numpy
3 import *
4 t = np.linspace
5 (-np.pi, np.pi,
6 1000) x = np.cos(t)
7 y = np.sin(t)
8 z = t
9 ax =
10 11 axes(projection=
import numpy as np '3d')
from ax.plot3D(x, y, z)
matplotlib.pyplot show()
localhost:8888/notebooks/MAT541D.ipynb 30/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
from sympy.plotting import
In [58]: plot_parametric as ppl 5
1 import numpy as np
#%matplotlib inline 6
2 t = symbols('t')
from sympy import symbols, cos, sin 7
3 ppl(t**2 - 3*t, t+2, (t, -2*np.pi,
from sympy.plotting import 3*np.pi))
plot3d_parametric_line as ppl3 4

Out[58]: <sympy.plotting.plot.Plot at 0x1bfcf1fdc50>


1.2 Basic Surface Plot

⎯⎯⎯⎯
√ �� √
A program to plot ��(��, ��) = �� +

localhost:8888/notebooks/MAT541D.ipynb 31/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook

In [61]: x = y = np.linspace(0, 5., 41)


1
2
xv, yv = np.meshgrid(x, y, indexing = 'ij', sparse = False)
3
fv = np.sqrt(xv) + np.sqrt(yv)
4
fig = plt.figure (figsize = (8, 5))
5
ax = axes(projection = '3d')
6
ax.plot_surface(xv, yv, fv, cmap=cm.coolwarm)
7
ax.set_xlabel('X axis')
8
ax.set_ylabel('Y axis')
9
None
localhost:8888/notebooks/MAT541D.ipynb 32/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
3
In [62]: x, y = symbols('x, y')
1 4
from sympy.plotting import plot3d (sqrt(x) + sqrt(y), (x,
plot3d 1, 10), (y, 1, 10)) 5
2 None
from sympy import sqrt
1.3 Plotting Vector Fields

localhost:8888/notebooks/MAT541D.ipynb 33/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook

In [63]: fig,ax = plt.subplots()


1
2
x_pos = 0
3
y_pos = 0
4
x_direct = 1
5
y_direct = 1
6
ax.quiver(x_pos, y_pos, x_direct, y_direct)
7
plt.show()

localhost:8888/notebooks/MAT541D.ipynb 34/47

11/7/23, 10:12 AM MAT541D - Jupyter Notebook

In [64]: fig,ax = plt.subplots()


1
2
x_pos = [0, -0.5]
3
y_pos = [0, 0.5]
4
x_direct = [1, 0]
5
y_direct = [1, -1]
6
ax.quiver(x_pos, y_pos, x_direct, y_direct, scale=5)
7
ax.axis([-1.5, 1.5, -1.5, 1.5])
8
plt.show()
localhost:8888/notebooks/MAT541D.ipynb 35/47

11/7/23, 10:12 AM MAT541D - Jupyter Notebook


10 11 12 ax.quiver(X, Y, u,
In [66]: x = np.arange(-5, v)
1 6, 1) ax.xaxis.set_ticks
2 y = np.arange(-5, ([])
3 6, 1) ax.yaxis.set_ticks
4 X, Y = ([])
5 np.meshgrid(x, y) ax.axis([-6, 6,
6 u, v = X/5, -Y/5 -6, 6])
7 fig, ax = ax.set_aspect('equ
8 plt.subplots(figsi al')
9 ze = (7, 7)) plt.show()
Assignment-3
1.9.23
�� =

1.A program to plot ((��������)(���


⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ �����)) (− )/4) √
(
(�� ) + ( )
2
��2

localhost:8888/notebooks/MAT541D.ipynb 36/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
5
In [67]: X, Y = np.meshgrid(x, y)
1 6
import matplotlib.pyplot as plt Z = np.cos(X) * np.cos(Y) *
2 np.e**(-np.sqrt(x**2 + y**2))/4 7
import numpy as np plt.contour(X, Y, Z, 20, cmap =
3 'Blues')
x = np.linspace(-50, 50, 10) 8
4 None
y = np.linspace(-50, 50, 10) 9
2.A program to plot
2
��2 ��2
�� = −(���� )/( ) + ( )

localhost:8888/notebooks/MAT541D.ipynb 37/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
X, Y = np.meshgrid(x, y)
In [68]: 4
1 Z = -(X*Y**2)/(X**2 +
x = np.linspace(-100, 100, Y**2)
20) 5
2 plt.contour(X, Y, Z,20,
y = np.linspace(-100, 100, cmap = 'Blues') 6
20) None
3
3.A program to plot
2
��2
�� = 1/(4�� + )

localhost:8888/notebooks/MAT541D.ipynb 38/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
y = np.linspace(-500, 500,
In [70]: 10)
1 3
x = np.linspace(-500, 500, X, Y = np.meshgrid(x, y)
10) 4
2 Z = 1/(4*X**2 + Y**2)
5 cmap = 'RdGy') 6
plt.contour(X, Y, Z, 20, None

4.A program to plot


�� = ������(−��)��������

localhost:8888/notebooks/MAT541D.ipynb 39/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
2
In [72]: y = np.linspace(-500, -50,
1 5)
x = np.linspace(-500, 0, 3
10) X, Y = np.meshgrid(x, y)
4 plt.contour(X, Y, Z,20
Z = np.e**(-Y) * np.cos(X) ,cmap = 'Blues') 6
5 None

5.A program to plot


2
��2 ��2 ��2
�� = ����(�� − )/( + )

localhost:8888/notebooks/MAT541D.ipynb 40/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
x = np.linspace(-500, 500,
In [73]: 10)
1 2
y = np.linspace(-500, 500, Z = (X*Y*(X**2 -
10) Y**2))/(X**2 + Y**2) 5
3 plt.contour(X, Y, Z,20,
X, Y = np.meshgrid(x, y) cmap = 'Reds') 6
4 None

6.A program to plot


2
��4 ��2
�� = �� − −

localhost:8888/notebooks/MAT541D.ipynb 41/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
3
In [74]: X, Y = np.meshgrid(x, y)
1 4
x = np.linspace(-5, 5, Z = Y**2 - Y**4 - X**2
5000) 5
2 plt.contour(X, Y, Z,
y = np.linspace(-5, 5, colors = 'black') 6
5000) None

LAB 4
5.9.23

�� = ��2
Find the unit tangent vector,curvature and principal unit normal vector of .Plot the curve
y(x),the curvature function and the osculating circle at the point(0,0)
from sympy v=sp.diff(y
import * ,x)
In [76]:
x=Symbol('xprint(v)
1 2 3 4 5 6
') w=sp.diff(v
7 8 9
y=x+x**2 ,x)
import
print(y) print(w)
sympy as sp
x**2 + x
2*x + 1
2

localhost:8888/notebooks/MAT541D.ipynb 42/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook

In [81]:
12
v=diff(r,t)
13
mod_v=sqrt(sum(matrix_multiply_elementwise(v
,v)))
14
15
unit_tan=v/mod_v
16
mod_unit_tan=sqrt(sum(matrix_multiply_elemen
twise(unit_tan,unit_tan))) 17
18
diff_unit_tan=sp.diff(unit_tan,t)
19
mod_diff_unit_tan=sqrt(sum(matrix_multiply_e
lementwise(diff_unit_tan,diff_ 20
21
curvature=mod_diff_unit_tan/mod_v
22
princ_unit_norm=diff_unit_tan/mod_diff_unit_
tan
23
radius=1/(curvature.subs(t,0))
24
unit_tan_P=unit_tan.subs(t,0)
25
princ_unit_P=princ_unit_norm.subs(t,0)
In [78]: Out[78]: 26
center=r.subs(t,0)+radius*princ_unit_P
27

1
trange=np.linspace(-1.2,1.2,50)
2
x=[]
3
y=[]
4
curva=[]
1
5
from sympy import *
6
2
for i in trange:
3
7
import numpy as np
x.append(r[0].subs(t,i))
4
8
import math
y.append(r[1].subs(t,i))
5
9
import matplotlib.pyplot as plt
curva.append(curvature.subs(t,i))
6
10
7
radius
8
11
t=Symbol('t')
center
9
10 0
r=Matrix([t,t**2]) 1
11
[]2
localhost:8888/notebooks/MAT541D.ipynb 43/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
10 11
In [80]: theta=np.linspa plt.plot(xc,yc,
1 ce(0,2*np.pi,50 'r')
2 0) plt.plot(trange
3 ,curva,'g')
4 xc=center[0]+ra plt.axis('equal
5 dius*np.cos(the ')
6 ta) plt.plot(x,y)
7 yc=center[1]+ra plt.legend()
8 dius*np.sin(the plt.show()
9 ta)
No artists with labels found to put in legend. Note that artists whose label
start with an underscore are ignored when legend() is called with no argumen
t.

LAB 5
10.10.23
EXTREME VALUES AND SADDLE POINTS.

��(��, ��) (��, ��)


Let be defined on region R containing the point then;

��(��, ��) �� ��(��, ��) ≥ ��(��, ��) (��, ��)


1. is a local maximum value of if for all domain points in an (��, ��)
open disk centered at
��(��, ��) �� ��(��, ��) ≤ ��(��, ��) (��, ��)
2. is a local minimum value of if for all domain points in an (��, ��)
open disk centered at .

localhost:8888/notebooks/MAT541D.ipynb 44/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook

Find derivative test for local extreme values:-

��(��, ��) (��, ��)


If has a local maximum or min value at an interval point of its domain and if the
����(��, ��) = 0 ����(��, ��) = 0
first partial derivatives exist there, then and

Second derivative test for local extreme values:-

��(��, ��)
Suppose that and its first and second derivatives are continuous throughout a disk (��,
��) ����(��, ��) = ����(��, ��) = 0
centered at and that . Then;

(��, ��) ������ < 0 ������������ − ������ > 0 (��, ��)


1. f has local maximum at if and at . (��, ��) ������ > 0
������������ − ������ > 0
(��, ��)
2. f has local minimum at if and at . (��, ��) ������������
< 0 (��, ��)
3. f has a saddle point at if at .

Q) Find all the local maxima. local minima and saddle points of the
fucntion.

2
��2
1. ��(��, ��) = �� + ���� + + 3�� − 3�� + 4
2 6
3 7
In [83]: 4 8
1 5 9
10 11 12 13 symbols('x,y') diff(f,y)
import numpy as f = x**2 + x*y + print(diff_fx)
np y**2 + 3*x - 3*y print(diff_fy)
from sympy import + 4
* s =
diff_fx = solve((diff_fx,di
x,y = diff(f,x) ff_fy),(x,y)) s
diff_fy =
2*x + y + 3
x + 2*y - 3

Out[83]: {x: -3, y: 3}

localhost:8888/notebooks/MAT541D.ipynb 45/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
diff(diff_fx,x) p*q-r > 0:
In [84]: diff_fyy = print('f has
1 diff(diff_fy,y) local maximum')
2 diff_fxy = elif p > 0 and
3 diff(diff_fx,y) p*q-r > 0:
4 print('f has
5 p = local minimum')
6 diff_fxx.subs({x elif p*r < 0:
7 :-3,y:3}) q = print('f has a
8 diff_fyy.subs({x saddle point ')
9 :-3,y:3}) r = else:
10 11 12 13 14 diff_fxy.subs({x print('f is
15 16 17 :-3,y:3}) inncolusive')
diff_fxx =
if p < 0 and
f has local minimum

2
��2
2. ��(��, ��) = 2���� − 5�� − 2 + 4�� + 4�� − 4
+ 4*x - 4*y - 4 6
7
In [85]: diff_fx = diff(f,x)
1 8
import numpy as np diff_fy = diff(f,y)
2 9
from sympy import * print(diff_fx)
3 10
4 print(diff_fy)
x,y = symbols('x,y') 11
5 12
f = 2*x*y + 5*x**2 + 2*y**2 s =
solve((diff_fx,diff_fy),(x,y s
)) 14
13 15
10*x + 2*y + 4
2*x + 4*y - 4

Out[85]: {x: -2/3, y: 4/3}

localhost:8888/notebooks/MAT541D.ipynb 46/47
11/7/23, 10:12 AM MAT541D - Jupyter Notebook
diff(diff_fx,x) p*q-r > 0:
In [86]: diff_fyy = print('f has
1 diff(diff_fy,y) local maximum')
2 diff_fxy = elif p > 0 and
3 diff(diff_fx,y) p*q-r > 0:
4 print('f has
5 p = local minimum')
6 diff_fxx.subs({x elif p*r < 0:
7 :-3,y:3}) q = print('f has a
8 diff_fyy.subs({x saddle point ')
9 :-3,y:3}) r = else:
10 11 12 13 14 diff_fxy.subs({x print('f is
15 16 :-3,y:3}) inncolusive')
diff_fxx =
if p < 0 and
f has local minimum
localhost:8888/notebooks/MAT541D.ipynb 47/47

You might also like