Chapter 6: Coordinates, Vectors, and Modeling a Curve


The Python code was developed based on the R code written by
Samuel Shen, Distinguished Professor, San Diego State University, California, USA



In [1]:
import numpy as np
import math
import pandas as pd
import statistics as stats
import scipy.stats as scistats
from scipy.stats import norm
import matplotlib.pyplot as plt
import random
In [2]:
#start from line 760
#Python code for plotting a blue point
x1 = 2
x2 = 3
plt.plot(x1, x2, 'bo') 
#use package matplotlib.pyplot as plt
#plot point at (2,3) on coordinate, b means blue, o means circle in matplot
Out[2]:
[<matplotlib.lines.Line2D at 0x7fdd99729430>]
No description has been provided for this image
In [3]:
x1 = [2, 4, 3]
x2 = [3, 7, -4]
plt.plot(x1, x2, 'bo')
plt.axis([0, 10, -5, 10])

#plot point (2,3), (4,7), (3,-4) in setted x-axis from 0 to 10 and y-axis from -5 to 10
Out[3]:
(0.0, 10.0, -5.0, 10.0)
No description has been provided for this image
In [4]:
plt.plot(2, 3, 'bo' , markersize = 20)
# plot a larger size blue point in (2,3) by use markersize, larger markersize means larger point
Out[4]:
[<matplotlib.lines.Line2D at 0x7fdd99c7c310>]
No description has been provided for this image
In [5]:
ax = plt.axes(projection = '3d')
x = y = z = [1, 2]
ax.scatter3D(x, y, z)
Out[5]:
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x7fdd99d20220>
No description has been provided for this image

Fig. 6.2: Point moves on a circle¶

In [7]:
R = 3
n = 41
ome = 2 # omega, ome
t = np.linspace(start = 0, stop = 3, num = n)
# t is the array of 41 numbers from 0 to 3 

x = [R * math.cos(ome * i) for i in t]
y = [R * math.sin(ome * i) for i in t]
In [8]:
fig = plt.figure(figsize = (4, 4))

plt.scatter(x, y, color = 'black')
plt.title('Footprints of a point moving along a circle')
plt.arrow(0, 0, x[5], y[5], head_width = 0.15, color = 'b')


point1 = [0, 0]
point2 = [x[0], y[0]]
x_values = [point1[0], point2[0]]
y_values = [point1[1], point2[1]]
plt.plot(x_values, y_values, 'b')
#draw a line from point1 to point2

point3 = [x[5], 0]
point4 = [x[5], y[5]]
x_values2 = [point3[0], point4[0]]
y_values2 = [point3[1], point4[1]]
plt.plot(x_values2, y_values2, 'b')
#draw a line from point3 to point4

point5 = [0, y[5]]
point6 = [x[5], y[5]]
x_values3 = [point5[0], point6[0]]
y_values3 = [point5[1], point6[1]]
plt.plot(x_values3, y_values3,'b')
#draw a line from point5 to point6

point7 = [0, 0]
point8 = [0, 3]
x_values4 = [point7[0], point8[0]]
y_values4 = [point7[1], point8[1]]
plt.plot(x_values4, y_values4, 'b')
#draw a line from point7 to point8

plt.plot(x[5], y[5], 'b')
plt.text(x[5], -0.3, 'x', fontsize = 15, c = "b")
plt.text( -0.3, y[5], 'y',  fontsize = 15, c = "b")
plt.text(x[5] / 2, 0.3 + y[5] / 2, 'R',fontsize = 15, c = "b")
plt.text(x[5] + 0.3, y[5], 'P', fontsize = 15, c ="b")
plt.text(-0.2, -0.2, 'O', fontsize = 15, c ="b")
plt.text(1.5, 0.35,  r'$\theta = \omega*t$', c = 'b')
plt.text(0, -1,  r'$x = R*\cos(\omega*t)$', c = 'b')
plt.text(0, -1.7,  r'$y = R*\sin(\omega*t)$', c = 'b')
Out[8]:
Text(0, -1.7, '$y = R*\\sin(\\omega*t)$')
No description has been provided for this image
In [9]:
# directions of a moving point on a circle 
fig = plt.figure(figsize = (4, 4))

plt.scatter(x, y, color = 'black')
plt.title('Directions of a moving point on a circle')
s = np.linspace(start = 1, stop = 21, num = 21)

c = ["b","g","r","c","m","y"]
for i in range(21-1):
    dx = (x[i + 1] - x[i]) * 0.8
    dy = (y[i + 1] - y[i]) * 0.8
    plt.arrow(x[i], y[i], dx, dy, head_width = 0.1, color = c[(i%6)])
    
plt.text(-1.5, 1, 'Angular velocity = 2 radians/sec')
plt.text(-1.5, 0, 'Time goes from 0 to 1.5 sec')
plt.text(-1.5, -1, 'Angle goes from 0 to 3 radians')
plt.text(-1.5, -2, 'Point goes through a semi-circle')

plt.title('Directions of a moving point on a circle')
plt.show()

#plt.figuresize
No description has been provided for this image
In [10]:
t
#The coordinates of the first five points of Fig. 6.2
t[0 : 5]

roundx = [round(num,2)for num in x[0 : 5]]
print(roundx)
roundy = [round(num,2)for num in y[0 : 5]]
print(roundy)
[3.0, 2.97, 2.87, 2.7, 2.48]
[0.0, 0.45, 0.89, 1.3, 1.69]

Fig. 6.3: A point is spiraling out¶

In [11]:
n = 81
ome = 2
plt.figure(figsize = (5, 5))
t = np.linspace(start = 0, stop = 12, num = n)
x = [ 2 * i * math.cos(ome * i)  for i in t]
y = [ 2 * i * math.sin(ome * i) for i in t]


plt.scatter(x,y,c = 'black')
c = ["b","g","r","c","m","y"]
for i in range(n - 1):
    dx = (x[i + 1] - x[i]) * 0.8
    dy = (y[i + 1] - y[i]) * 0.8
    plt.arrow(x[i], y[i], dx, dy, head_width = 0.8, color = c[(i%6)])
plt.title('A point is spiraling out')
plt.show()
No description has been provided for this image

Fig. 6.4: Heart-shape path of a point¶

In [12]:
n = 41
ome = 2
plt.figure(figsize = (5, 5))
t = np.linspace(start = 0, stop = 2 * math.pi - 0.15, num = n)
x = [ 2 * math.sin(ome * i) + 5 * math.sin(ome * i / 2) for i in t]
y = [ 2 * math.cos(ome * i) + 5 * math.cos(ome * i / 2)for i in t]


plt.scatter(x, y, c = 'black')
c = ["b", "g", "r", "c", "m", "y"]
for i in range(n - 1):
    dx = (x[i + 1] - x[i]) * 0.8
    dy = (y[i + 1] - y[i]) * 0.8
    plt.arrow(x[i], y[i], dx, dy, head_width = 0.1, color = c[(i%6)])
plt.title('Heart-shape path of a point')
plt.show()
No description has been provided for this image

Fig. 6.5: Model flower pedals¶

In [13]:
fig = plt.figure(figsize = (4, 4))
a = 3
n = 1000
ome = 2
t = np.linspace(start = 0, stop = 2 * math.pi, num = n)
theta = ome * t
r = [a * math.cos(4 * i) for i in theta]
x = [a * math.cos(4 * i) * math.cos(i) for i in theta]
y = [a * math.cos(4 * i) * math.sin(i) for i in theta]


plt.plot(x, y, c = 'pink')
plt.title('An eight-petal rose')
Out[13]:
Text(0.5, 1.0, 'An eight-petal rose')
No description has been provided for this image
In [14]:
# infinitely many petals
fig = plt.figure(figsize = (4, 4))
n = 10000
ome = 2
t = np.linspace(start = 0, stop = 40 * math.pi, num = n)
theta = ome * t
a = math.sqrt(2)
r = [a * math.cos(math.pi * i) for i in theta]
x = [a * math.cos(math.pi * i) * math.cos(i) for i in theta]
y = [a * math.cos(math.pi * i) * math.sin(i) for i in theta]


plt.plot(x,y,c = 'r', linewidth = 0.5)
plt.title('Infinitely many p')
Out[14]:
Text(0.5, 1.0, 'Infinitely many p')
No description has been provided for this image

Figure 6.7: Vector OP¶

In [15]:
fig = plt.figure(figsize = (4, 4))
plt.plot(3, 4, 'ro' , markersize = 10)
plt.arrow(0, 0, 3, 4, width = 0.1, color = 'black')
plt.arrow(-1, 0, 4, 0, head_width = 0.1)
plt.arrow(0, -1, 0, 5,head_width = 0.1)
plt.arrow(0, 0, 0, 1, width = 0.1, color = 'black')
plt.arrow(0, 0, 1, 0, width = 0.1, color = 'black')
plt.arrow(3, 0, 0, 4, linestyle = 'dashed')
plt.arrow(0, 4, 3, 0, linestyle = 'dashed')
plt.text(-0.2, -0.2, 'O', fontsize = 15)
plt.text(3.2, 4.4, r'$P(x_1, x_2)$', c = 'r', fontsize = 20)
plt.text(0.5, 0.3, r'$\theta$', fontsize = 15)
plt.text(1, -0.3, 'i', fontsize = 15)
plt.text(-0.3, 1, 'j', fontsize = 15)
plt.text(3.8, -0.3, 'x', fontsize = 15)
plt.text(-0.2, 4.8, 'y', fontsize = 15)
Out[15]:
Text(-0.2, 4.8, 'y')
No description has been provided for this image
In [16]:
# Python command of entering data
x = [1.4, -2.6, 0.9, -3.7, 4.1, 3.2, 5.0]
x
Out[16]:
[1.4, -2.6, 0.9, -3.7, 4.1, 3.2, 5.0]
In [17]:
#An R code for vector addition and subtraction
u = [3, 4]
v = [-2.5, 1.5]
x = np.add(u, v)
y = np.subtract(u, v)
x
#array([0.5, 5.5])
Out[17]:
array([0.5, 5.5])
In [18]:
y
#array([5.5, 2.5])
Out[18]:
array([5.5, 2.5])

R plot Fig. 6.8: Addition and subtraction of two vectors¶

In [19]:
fig = plt.figure(figsize = (4, 4))
u = [3, 4]
v = [-2.5, 1.5]
o = [0, 0]
x = np.add(u, v)
y = np.subtract(u, v)
plt.plot(0,0, color = 'black', marker = 'o')
plt.axis([-3, 6, -3, 6])

plt.arrow(o[0], o[1], u[0], u[1], head_width = 0.2, color = 'black')
plt.arrow(o[0], o[1], v[0], v[1], head_width = 0.2, color = 'blue')
plt.arrow(o[0], o[1], -v[0], -v[1], color = 'blue', head_width = 0.2, linestyle = 'dotted')
plt.arrow(o[0], o[1], x[0], x[1], head_width = 0.2, color = 'red')
plt.arrow(o[0], o[1], y[0], y[1], head_width = 0.2, color = 'purple')
plt.arrow(x[0], x[1], u[0]-x[0], u[1]-x[1], linestyle = "dotted", color = 'blue')
plt.arrow(x[0], x[1], v[0]-x[0], v[1]-x[1], linestyle = "dotted")
plt.arrow(u[0], u[1], y[0]-u[0], y[1]-u[1], linestyle ="dotted", color = 'blue')
plt.arrow(y[0], y[1], -v[0]-y[0], -v[1]-y[1], linestyle ="dotted", color = 'blue')

plt.arrow(-3, 0, 9, 0, head_width = 0.2 )
plt.arrow(0, -3, 0, 9, head_width = 0.2 )
plt.text(u[0] + 0.3, u[1] + 0.3, 'u', fontsize = 15)
plt.text(v[0] - 0.3, v[1] + 0.3, 'v', color = 'blue', fontsize = 15)
plt.text(-v[0], -v[1] - 0.3, '-v', color = 'blue', fontsize = 15)
plt.text(x[0] + 0.2, x[1] + 0.3, 'u + v', color = 'red', fontsize = 15)
plt.text(y[0] + 0.3, y[1] - 0.3, 'u - v', color = 'purple', fontsize = 15)

plt.text(-0.3, -0.3, 'O', fontsize = 20)
plt.text(6, -0.3, 'x', fontsize = 20)
plt.text(-0.4, 6,  'y', fontsize = 20)
Out[19]:
Text(-0.4, 6, 'y')
No description has been provided for this image

R plot Fig. 6.10: Colorado temperature and elevation¶

In [20]:
x = [1671.5, 1635.6, 2097.0, 1295.4, 1822.7, 2396.9, 2763.0, 1284.7,
     1525.2, 1328.6, 1378.9, 2323.8, 2757.8, 1033.3, 1105.5, 1185.7,
     2343.9, 1764.5, 1271.0, 2347.3, 2094.0, 2643.2, 1837.9, 1121.7]
y= [22.064, 23.591, 18.464, 23.995, 20.645, 17.175, 13.582, 24.635,
     22.178, 24.002, 23.952, 16.613, 13.588, 25.645, 25.625, 25.828,
     17.626, 22.433, 24.539, 17.364, 17.327, 15.413, 22.174, 24.549]
np.corrcoef(x, y)
Out[20]:
array([[ 1.        , -0.98138583],
       [-0.98138583,  1.        ]])
In [21]:
np.mean(x)
Out[21]:
1792.8791666666666
In [22]:
np.mean(y)
Out[22]:
20.958625
In [23]:
plt.plot(x, y, 'ko')
plt.title('Colorado Elevation and July Tmean: 1981-2010 Average')
plt.xlabel('Elevation [m]')
plt.ylabel('Temperature [deg C]')
plt.text(2100, 25.5, "Temperature lapse rate: 7.0 deg C/1.0km")
plt.text(2350, 24, "y= 33.48 - 0.0070 x")
plt.text(2350, 22.5,"R-squared = 0.96")
Out[23]:
Text(2350, 22.5, 'R-squared = 0.96')
No description has been provided for this image
In [24]:
from scipy import stats
stats.linregress(x, y)
Out[24]:
LinregressResult(slope=-0.006981884564898768, intercept=33.47630038047857, rvalue=-0.9813858291431772, pvalue=2.939038251418388e-17, stderr=0.00029129156935735884, intercept_stderr=0.5460279120708613)
In [25]:
import statsmodels.api as sm
results = sm.OLS(x, y).fit()
print(results.summary())
                                 OLS Regression Results                                
=======================================================================================
Dep. Variable:                      y   R-squared (uncentered):                   0.789
Model:                            OLS   Adj. R-squared (uncentered):              0.779
Method:                 Least Squares   F-statistic:                              85.83
Date:                Tue, 17 Jan 2023   Prob (F-statistic):                    3.17e-09
Time:                        21:49:52   Log-Likelihood:                         -196.27
No. Observations:                  24   AIC:                                      394.5
Df Residuals:                      23   BIC:                                      395.7
Df Model:                           1                                                  
Covariance Type:            nonrobust                                                  
==============================================================================
                 coef    std err          t      P>|t|      [0.025      0.975]
------------------------------------------------------------------------------
x1            78.0920      8.429      9.265      0.000      60.655      95.529
==============================================================================
Omnibus:                        4.365   Durbin-Watson:                   1.689
Prob(Omnibus):                  0.113   Jarque-Bera (JB):                2.007
Skew:                           0.393   Prob(JB):                        0.367
Kurtosis:                       1.821   Cond. No.                         1.00
==============================================================================

Notes:
[1] R² is computed without centering (uncentered) since the model does not contain a constant.
[2] Standard Errors assume that the covariance matrix of the errors is correctly specified.

R plot Fig. 6.13: Circular motion of a point¶

In [26]:
fig = plt.figure(figsize = (4, 4))
ome = 1
R = 3
t = np.linspace(start = 0, stop = 4 * math.pi, num = 81)
x = [R * math.cos(ome * i) for i in t]
y = [R * math.sin(ome * i) for i in t]

plt.plot(x, y, color = 'black')
plt.axis([-4, 4, -4, 4])
plt.title('Circular motion of a point')
plt.arrow(-3.5, 0, 7.3, 0, head_width = 0.1 )
plt.arrow(0, -3.5, 0, 7.3, head_width = 0.1 )

s = np.linspace(start = 1, stop = 71, num = 71)
c = ["b","g","r","c","m","y"]
for i in range(71 - 1):
    dx = (x[i + 1] - x[i]) * 0.8
    dy = (y[i + 1] - y[i]) * 0.8
    plt.arrow(x[i], y[i], dx, dy, head_width = 0.2, color = c[(i%6)])
plt.plot(x[15], y[15], 'ro', markersize = 12) 

plt.arrow(0, 0, x[15], y[15], color = 'red', head_width = 0.2)
plt.arrow(x[15], y[15], 0.7 * x[15] - x[15], 0.7 * y[15] - y[15], 
          color = 'black', head_width = 0.3)
plt.arrow(x[15], y[15], 0.3 * x[15] - x[15], 0.3 * y[15] - y[15], 
          color = 'blue', head_width = 0.2)
plt.text(-0.4, -0.3, 'O', fontsize = 20)
plt.text(x[15] - 0.3, y[15] + 0.3, 'r' , 
         color = 'red', fontsize = 20)
plt.text(0.7 * x[15] - 0.1, 0.7 * y[15] - 0.3, 'a',
         color = 'black', fontsize = 20)
plt.text(0.3 * x[15] - 0.1, 0.3 * y[15] - 0.3, 'F', 
         color = 'blue', fontsize = 20)
plt.text(3.6, -0.3, 'x', fontsize = 18)
plt.text(-0.3, 3.6, 'y', fontsize = 18)
Out[26]:
Text(-0.3, 3.6, 'y')
No description has been provided for this image

Symbolic calculation of derivatives¶

Example 6.4¶

In [27]:
from sympy import *
t = symbols('t')
x = diff(2 * t * cos(2 * t))
x
Out[27]:
$\displaystyle - 4 t \sin{\left(2 t \right)} + 2 \cos{\left(2 t \right)}$
In [28]:
y = diff(2 * t * sin(2 * t))
y
Out[28]:
$\displaystyle 4 t \cos{\left(2 t \right)} + 2 \sin{\left(2 t \right)}$

Example 6.5¶

In [29]:
x = diff(R * cos(ome * t), t)
x
Out[29]:
$\displaystyle - 3 \sin{\left(t \right)}$
In [30]:
y = diff(R * sin(ome * t), t)
y
Out[30]:
$\displaystyle 3 \cos{\left(t \right)}$
In [31]:
fig = plt.figure(figsize = (4, 4))
from sklearn.linear_model import LinearRegression
x = np.array([1, 4, 6]).reshape((-1, 1))
y = np.array([-2, 3.8, 1.0])
plt.plot(x, y)
Out[31]:
[<matplotlib.lines.Line2D at 0x7fdd7edc8250>]
No description has been provided for this image
In [32]:
model = LinearRegression().fit(x, y)
r_sq = model.score(x, y)
print(f"coefficient of determination: {r_sq}")
coefficient of determination: 0.3744265576778715

R plot Fig. 6.14: tangent line of a curve¶

In [33]:
fig = plt.figure(figsize = (4, 4))
ome = 2 * pi
R = 3
t = np.linspace(start = 0, stop = 1, num = 101)
x = [R * math.cos(ome * i) for i in t]
y = [R * math.sin(ome * i) for i in t]


plt.plot(x,y, color = 'black')
plt.axis([-4, 4, -4, 4])
plt.arrow(-3.5, 0, 7.3, 0, head_width = 0.1 )
plt.arrow(0, -3.5, 0, 7.3, head_width = 0.1 )
plt.plot(3 * sqrt(2) / 2, 3 * sqrt(2) /   2, 'ro')


tau = np.linspace(start = -1, stop = 1, num = 100)
xt = [3 * sqrt(2) / 2 * (1 - i) for i in tau]
yt = [3 * sqrt(2) / 2 * (1 + i) for i in tau]


plt.plot(xt,yt, color = 'blue')
plt.text(-0.4, -0.3, 'O')
plt.text(3.6, -0.3, 'x')
plt.text(-0.3, 3.6, 'y')
Out[33]:
Text(-0.3, 3.6, 'y')
No description has been provided for this image

R plot Fig. 6.15: Helix and its tangent line¶

In [34]:
from mpl_toolkits import mplot3d
In [35]:
from mpl_toolkits import mplot3d

ax = plt.axes(projection = '3d')

R = 3
ome = 2 * math.pi
c = 1
t = np.linspace(0, 3.11, 1000)
x = [R * math.cos(ome * i) for i in t]
y = [R * math.sin(ome * i) for i in t]
z = c * t
t0 = 1.863
tau = np.linspace(-0.15, 0.15, 300)
xt = [R * math.cos(ome * t0) - R * ome * math.sin(ome * t0) * i for i in tau]
yt = [R * math.sin(ome * t0) + R * ome * math.cos(ome * t0) * i for i in tau]
zt = c * t0 + c * tau

ax.scatter3D(x, y, z, color = 'blue')
plt.xlim(-3, 3)
plt.ylim(-3, 3)
ax.scatter3D(xt, yt, zt)
Out[35]:
<mpl_toolkits.mplot3d.art3d.Path3DCollection at 0x7fdd7ef45fd0>
No description has been provided for this image

Plot Fig. 6.16a for k = pi¶

In [36]:
fig = plt.figure(figsize = (4, 4))
n = 10000
a = 3
ome = 2
k = math.pi
t = np.linspace(0, 30 * k , n)
theta = ome * t
r = [a * math.cos(k * i) for i in theta]
x = [a * math.cos(k * i) * math.cos(i) for i in theta]
y = [a * math.cos(k * i) * math.sin(i) for i in theta]
#plt.plot(x, y, color = 'red')
c = ["b","g","r","c","m","y"]
for i in range(1000-1):
    dx = (x[i + 1] - x[i]) * 0.8
    dy = (y[i + 1] - y[i]) * 0.8
    plt.arrow(x[i], y[i], dx, dy, head_width = 0.2, color = c[(i%6)])
No description has been provided for this image
In [37]:
#Plot Fig. 6.16b for k = sqrt(2)
#The same as above by a different k

Plot 6.17(a): Artistic hypotrochoid patterns¶

In [2]:
plt.figure(figsize = (4, 4))
n = 6000
R = 100
r = 2/3
p = 70
ome = 1
t = np.linspace(0, 30 * math.pi , n)
theta = ome * t
x = [(R - r) * math.cos( i ) + p * math.cos((R - r) * i / r) for i in theta]
y = [(R - r) * math.sin( i ) - p * math.sin((R - r) * i / r) for i in theta]


plt.plot(x,y,color = 'pink')
c = ["b", "g", "r", "c", "m", "y"]
for i in range(n - 1):
    dx = (x[i + 1] - x[i]) * 0.8
    dy = (y[i + 1] - y[i]) * 0.8
    plt.arrow(x[i], y[i], dx, dy, head_width = 0.1, color = c[(i%6)])
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[2], line 1
----> 1 plt.figure(figsize = (4, 4))
      2 n = 6000
      3 R = 100

NameError: name 'plt' is not defined

Plot 6.17(b) artistic patterns¶

In [39]:
fig = plt.figure(figsize = (4, 4))
n = 10000
R = 22
r = 10 * 2 / 3
p = -9
ome = math.sqrt(5)
t = np.linspace(0, 190 * math.pi , n)
theta = ome * t
x = [(R - r ) * math.cos( i ) + p * math.cos((R - r) * i / r) for i in theta]
y = [(R - r) * math.sin( i ) - p * math.sin((R - r) * i / r) for i in theta]
#plt.plot(x, y, color = 'red')
c = ["b", "g", "r", "c", "m", "y"]
for i in range(n - 1):
    dx = (x[i + 1] - x[i]) * 0.8
    dy = (y[i + 1] - y[i]) * 0.8
    plt.arrow(x[i], y[i], dx, dy, head_width = 0.3, color = c[(i%6)])
No description has been provided for this image