The document discusses various methods for tracing and plotting polar curves and parametric curves in Python using matplotlib. It provides code examples to trace curves such as the Spiral of Archimedes, Cardioid, Rose curve, Circle, and Cycloid. Functions are defined to generate the x and y coordinates from the parametric equations and plot the curves.
The document discusses various methods for tracing and plotting polar curves and parametric curves in Python using matplotlib. It provides code examples to trace curves such as the Spiral of Archimedes, Cardioid, Rose curve, Circle, and Cycloid. Functions are defined to generate the x and y coordinates from the parametric equations and plot the curves.
The document discusses various methods for tracing and plotting polar curves and parametric curves in Python using matplotlib. It provides code examples to trace curves such as the Spiral of Archimedes, Cardioid, Rose curve, Circle, and Cycloid. Functions are defined to generate the x and y coordinates from the parametric equations and plot the curves.
import matplotlib. pyplot as plt ax = plt. subplot(projection='polar') ax.set_title ("Spiral of Archimedes: $r = a + bθ$") import numpy as np a=2 b=4 theta=np. linspace(0,2*np.pi,1000) r=a+b*(theta) plt. polar (theta, r,'r.') plt. show ()
# matplotlib is the whole package; matplotlib. pyplot is a module in matplotlib.
# pylab is a module that gets installed alongside matplotlib. pylab is a convenience module that bulk imports matplotlib. # pyplot (for plotting) and numpy (for Mathematics and working with arrays) in a single name space. # Since heavily importing into the global namespace may result in unexpected behavior, the use of pylab is strongly discouraged. # 1-b) Trace Spiral of Archimedes: r = a + bθ import pylab from pylab import * ax = subplot(projection='polar') ax.set_title ("Spiral of Archimedes: $r = a + bθ$") a=2 b=4 theta=linspace(0,2*pi,1000) r=a+b*(theta) polar (theta, r,'g’) show () # 2-a) Trace a Cardioid: r = 5(1 + cosθ) import matplotlib. pyplot as plt from matplotlib. pyplot import * ax = subplot(projection='polar') ax.set_title ("Cardioid: $r=5(1+cos(ɵ)) $") import numpy as np theta=np. linspace(0,2*np.pi,1000) r=5+5*np.cos(theta) polar (theta, r,'g.') show ()
# 2- b) Trace a Cardioid: r = 5(1 + cosθ).
import pylab ax = pylab. subplot(projection='polar') ax.set_title ("Cardioid: $r=5(1+cos(ɵ)) $") theta=pylab. linspace(0,2*pylab.pi,1000) r=5+5*pylab.cos(theta) pylab. polar (theta, r,'r.') pylab. show () # 3-a) Trace a Four leaved Rose: r = 2|cos2x|. import matplotlib. pyplot as plt from matplotlib. pyplot import * ax = subplot(projection='polar') ax.set_title ("Four leaved Rose: $r=2 | cos2x |$") import numpy as np from numpy import * theta=linspace(0,2*pi,1000) r=2*abs (cos (2* theta)) polar (theta, r,'b.') show ()
# 3-b) Trace a Four leaved Rose: r = 2|cos2x|
import pylab from pylab import * ax = subplot(projection='polar') ax.set_title ("Four leaved Rose: $r=2 | cos2x |$") theta = linspace (0,2*pi, 1000) r=2*abs (cos (2* theta)) polar (theta, r,'r') show () # 4) Trace a Circle: r = p, where p is the radius of the circle. import matplotlib. pyplot as plt ax = plt. subplot(projection='polar') ax.set_title ("Circle: $r=3$") import numpy as np r=3 theta = np. arange (0, 2*np.pi, 0.01) for i in theta: plt. polar (i, r, 'g.') plt. show ()
# 5) Trace Cardioids: r = a + acos(θ) and r = a − acos(θ)
import matplotlib. pyplot as plt import math ax = plt. subplot(projection='polar') ax.set_title ("Cardioids: $r = a + acos(θ) and r = a − acos(θ)$") a=3 import numpy as np theta = np. arange (0, 2*np.pi, 0.01) # plotting the cardioid for i in theta: r1 = a + (a*np.cos(i)) plt. polar (i, r1,'g.') r2=a- (a*np.cos (i)) plt. polar (i, r2,'r.') plt. show () # display the polar plot Tracing parametric curves. # 1. Trace Circle: x = acos(θ); y = asin(θ) import matplotlib. pyplot as plt import numpy as np def circle (r): x = [] # create the list of x coordinates y = [] # create the list of y coordinates plt. title ("Circle: $ x = acos(θ); y = asin(θ)$") for theta in np. linspace (-2*np.pi, 2*np.pi, 100): # loop over a list of theta, which ranges from -2 pi to 2 pi x.append(r*np.cos(theta)) #add the corresponding expression of x to the x list y.append(r*np.sin(theta)) # same for y plt. plot (x, y) # plot using matplotlib. piplot plt. show () # show the plot circle (5) # call the function
# 2. Trace Cycloid: x = a (θ − sinθ); y = a (1 − sinθ)
import matplotlib. pyplot as plt import numpy as np def cycloid (r): x = [] # create the list of x coordinates y = [] # create the list of y coordinates plt. title ("Cycloid: $x = a (θ − sinθ); y = a (1 − sinθ) $") for theta in np. linspace (-2*np.pi, 2*np.pi, 100): # loop over a list of theta, which ranges from -2 pi to 2 pi x. append (r*(theta - np.sin (theta))) #add the corresponding expression of x to the x list y. append (r*(1 - np.cos (theta))) # same for y plt. plot (x, y) # plot using matplotlib. piplot plt. show () # show the plot cycloid (2) # call the function