ICT Engineering Mathematics Matrices and Linear Systems (3) Contents • Polynomial Curve Fitting • 2-Link Robot Arm Simulation Polynomial Curve Fitting • Suppose that you are given 𝑛𝑛 points in the xy-plane as follows: 𝑥𝑥1 , 𝑦𝑦1 , 𝑥𝑥2 , 𝑦𝑦2 , … , 𝑥𝑥𝑛𝑛 , 𝑦𝑦𝑛𝑛 . • Here, the points represent a collection of data. Then, the required task is to find a polynomial function of degree 𝑛𝑛 − 1 as 𝑝𝑝 𝑥𝑥 = 𝑎𝑎0 + 𝑎𝑎1 𝑥𝑥 + 𝑎𝑎2 𝑥𝑥 2 + ⋯ + 𝑎𝑎𝑛𝑛−1 𝑥𝑥 𝑛𝑛−1 whose graph passes through the given points. This procedure is called polynomial curve fitting. How to solve • To solve for the 𝑛𝑛 coefficients of 𝑝𝑝 𝑥𝑥 , substitute each of the 𝑛𝑛 points into the polynomial function and obtain 𝑛𝑛 linear equations in 𝑛𝑛 variables 𝑎𝑎0 , 𝑎𝑎1 , 𝑎𝑎2 , …, 𝑎𝑎𝑛𝑛−1 as follows: 𝑝𝑝 𝑥𝑥1 = 𝑎𝑎0 + 𝑎𝑎1 𝑥𝑥1 + 𝑎𝑎2 𝑥𝑥1 2 + ⋯ + 𝑎𝑎𝑛𝑛−1 𝑥𝑥1 𝑛𝑛−1 = 𝑦𝑦1 𝑝𝑝 𝑥𝑥2 = 𝑎𝑎0 + 𝑎𝑎1 𝑥𝑥2 + 𝑎𝑎2 𝑥𝑥2 2 + ⋯ + 𝑎𝑎𝑛𝑛−1 𝑥𝑥2 𝑛𝑛−1 = 𝑦𝑦2 ……… 𝑝𝑝 𝑥𝑥𝑛𝑛 = 𝑎𝑎0 + 𝑎𝑎1 𝑥𝑥𝑛𝑛 + 𝑎𝑎2 𝑥𝑥𝑛𝑛 2 + ⋯ + 𝑎𝑎𝑛𝑛−1 𝑥𝑥𝑛𝑛 𝑛𝑛−1 = 𝑦𝑦𝑛𝑛 This gives linear system A𝐱𝐱 = 𝐛𝐛, where 1 𝑥𝑥1 1 𝑥𝑥2 A= ⋮ ⋮ 1 𝑥𝑥𝑛𝑛 … 𝑥𝑥1 𝑛𝑛−1 … 𝑥𝑥2 𝑛𝑛−1 , ⋱ ⋮ … 𝑥𝑥𝑛𝑛 𝑛𝑛−1 𝑎𝑎0 𝑎𝑎1 𝐱𝐱 = ⋮ 𝑎𝑎𝑛𝑛−1 and 𝑦𝑦1 𝑦𝑦2 𝐛𝐛 = ⋮ . 𝑦𝑦𝑛𝑛 • Example 1: • Determine polynomial 𝑝𝑝 𝑥𝑥 = 𝑎𝑎0 + 𝑎𝑎1 𝑥𝑥 + 𝑎𝑎2 𝑥𝑥 2 whose graph passes through 1,4 , 2,0 , 3,12 . • Solution: 1 A|𝐛𝐛 = 1 1 A 1 1 4 2 4 0 3 9 12 1 1 0 1 0 0 1 0 0 𝐛𝐛 1 3 2 0 −2 1 3 0 1 4 −4 16 8 −4 8 ∴ 𝑝𝑝 𝑥𝑥 = 24 − 28𝑥𝑥 + 8𝑥𝑥 2 1 1 0 1 1 3 1 3 9 1 0 0 1 0 0 8 −2 0 −28 8 1 1 1 0 1 0 0 1 3 1 4 −4 12 4 −4 8 1 1 0 1 0 2 1 0 0 1 0 0 1 3 8 0 −2 1 3 0 1 4 −4 8 8 −4 8 0 0 24 1 0 −28 8 0 1 • Python code Solve linear system Ax = b for x. linspace(start, stop, num=50) Return num evenly-spaced numbers over the interval from start to stop. • Example 2 • Find a polynomial that fits the points −2,3 , −1,5 , (0,1), (1,4), (2,10). • Solution: • Because we are given five points, choose a fourth-degree polynomial function 𝑝𝑝 𝑥𝑥 = 𝑎𝑎0 + 𝑎𝑎1 𝑥𝑥 + 𝑎𝑎2 𝑥𝑥 2 + 𝑎𝑎3 𝑥𝑥 3 + 𝑎𝑎4 𝑥𝑥 4 . • Substituting the given points into 𝑝𝑝 𝑥𝑥 produces linear system A𝐱𝐱 = 𝐛𝐛, where 1 −2 4 −8 16 1 −1 1 −1 1 A= 1 0 0 0 0 , 1 1 1 1 1 1 2 4 8 16 3 5 𝐛𝐛 = 1 , 4 10 • The solution of the above linear system is 𝐱𝐱 = 1 − 1 𝑎𝑎0 𝑎𝑎1 𝐱𝐱 = 𝑎𝑎2 . 𝑎𝑎3 𝑎𝑎4 30 101 18 24 24 24 which results in 𝑝𝑝 𝑥𝑥 = 24 24 − 30𝑥𝑥 + 101𝑥𝑥 2 + 18𝑥𝑥 3 − 17𝑥𝑥 4 . − 17 T , 24 • Python code • Example 3 • Find a polynomial that relates the periods of the three planets that are closest to the Sun to their mean distances from the Sun, as shown in the table. Then test accuracy of the fit by using the polynomial to calculate the period of Mars. (In the table, the mean distance is given in normalized units by the distance for the earth, and the period is given in years.) Planet Mercury Venus Earth Mars Mean Distance 0.387 0.723 1.000 1.524 Period 0.241 0.615 1.000 1.881 • Solution • Begin by fitting a quadratic polynomial function 𝑝𝑝 𝑥𝑥 = 𝑎𝑎0 + 𝑎𝑎1 𝑥𝑥 + 𝑎𝑎2 𝑥𝑥 2 to the points 0.387,0.241 , 0.723,0.615 , 1.000,1.000 . • The system of linear equations obtained by substituting these points into 𝑝𝑝 𝑥𝑥 is 𝑎𝑎0 + 0.387𝑎𝑎1 + 0.3872 𝑎𝑎2 = 0.241 𝑎𝑎0 + 0.723𝑎𝑎1 + 0.7232 𝑎𝑎2 = 0.615 𝑎𝑎0 + 𝑎𝑎1 + 𝑎𝑎2 = 1. • The approximate solution of the system is 𝑎𝑎0 ≈ −0.00634, 𝑎𝑎1 ≈ 0.6119, 𝑎𝑎2 ≈ 0.4515 which means that an approximation of the polynomial function is 𝑝𝑝 𝑥𝑥 = −0.00634 + 0.6119𝑥𝑥 + 0.4515𝑥𝑥 2 . • Using 𝑝𝑝 𝑥𝑥 to evaluate the period of Mars produces 𝑝𝑝 1.524 ≈ 1.918 years. • Note that the actual period of Mars is 1.881 years. • Python code 2-Link Robot Arm Simulation • 2-Link Robot Arm Dynamics 𝝉𝝉 = M𝒂𝒂 + 𝐛𝐛 𝜏𝜏1 𝝉𝝉 = 𝜏𝜏 2 𝜃𝜃̈1 𝒂𝒂 = 𝜃𝜃̈2 : joint torques : joint accelerations, where 𝜃𝜃̈ 𝑖𝑖 = M ∈ ℝ2×2 : mass matrix 𝐛𝐛 ∈ ℝ2 : bias forces 𝑚𝑚𝑖𝑖 : mass of link 𝑖𝑖 𝑙𝑙𝑖𝑖 : length of link 𝑖𝑖 𝑑𝑑 𝑑𝑑 𝜃𝜃 𝑑𝑑𝑑𝑑 𝑑𝑑𝑑𝑑 𝑖𝑖 • Equations of M and 𝐛𝐛 M= 𝐛𝐛 = 𝜃𝜃̇ 𝑖𝑖 = + 𝑑𝑑 𝜃𝜃 : 𝑑𝑑𝑑𝑑 𝑖𝑖 joint velocity of link 𝑖𝑖 𝑔𝑔 : gravitational acceleration 𝑘𝑘𝑑𝑑 : damping coefficient 𝑐𝑐𝑖𝑖 = cos 𝜃𝜃𝑖𝑖 𝑠𝑠𝑖𝑖 = sin 𝜃𝜃𝑖𝑖 𝑐𝑐12 = cos 𝜃𝜃1 + 𝜃𝜃2 𝑘𝑘𝑑𝑑 𝜃𝜃̇1 + 𝑘𝑘𝑑𝑑 𝜃𝜃̇ 2 • How to simulate: • We assume that there is no actuation (𝝉𝝉 = 𝟎𝟎) for simplicity. 𝟎𝟎 = M𝒂𝒂 + 𝐛𝐛 M𝒂𝒂 = −𝐛𝐛 • Then, integrate the acceleration to update the current joint velocities 𝐯𝐯 as follows: ℎ 𝐯𝐯 ′ = 𝐯𝐯 + � 𝒂𝒂 𝑑𝑑𝑑𝑑 ≈ 𝐯𝐯 + ℎ𝒂𝒂 0 𝒂𝒂 is computed by Gaussian elimination. 𝜃𝜃̇1 Here, 𝐯𝐯 = and ℎ is the integration step size, which is a small constant. ̇𝜃𝜃2 • In a similar way, update the current joint position as follows: ℎ 𝐩𝐩′ = 𝐩𝐩 + � 𝐯𝐯 𝑑𝑑𝑑𝑑 ≈ 𝐩𝐩 + ℎ𝐯𝐯 ′ , 0 where 𝐩𝐩 = 𝜃𝜃1 . 𝜃𝜃2 • Python code • Define a 2-link robot class. • Given a robot instance, compute the mass matrix. • Given a robot instance, compute the bias forces. • Simulation code (1/2) Clear the figure. Enforce a square region for the figure X 2 ,Y 2 X 1 ,Y 1 X 0 ,Y 0 • Simulation code (2/2) References • Linear algebra - 7th edition [Larson] • Introduction to Robotics: Mechanics and Control - 3rd edition [Craig]