MATLAB Answers

Dot Product Doubt, don't get the result I expected.

7 views (last 30 days)
Wai Han
Wai Han on 28 Sep 2020
Commented: Wai Han on 28 Sep 2020
Hello I am new to Matlab and I am stuck in doing some maths(dot product) problem today. Please help me out.
I can write the correct code and format in python format, but i got some issues in the matlab. The python code is the following --
% This is the python code
RIMatrix_sph1 = RIMatrix_sph + mass_sph * (np.dot(q1.T, q1) * np.eye(3) - np.dot(q1, q1.T))
The error I encounter in matlab is in this specific part of the code --
(np.dot(q1.T, q1) * np.eye(3) - np.dot(q1, q1.T))
Here is the code I wrote in the Matlab
% This is the MATLAB code
mass_sph = 23.4572;
RIMatrix_sph = [0.0938 0 0; 0 0.0938 0; 0 0 0.0938]
RIMatrix_sph1 = RIMatrix_sph + mass_sph * (dot(transpose(q1),q1) * eye(3) - dot(q1,transpose(q1)))
I would be appreciated if you coulde help me with the problem.
PS.
This is the correct answer return by my python code --
RIMatrix_sph1 = [1.0321 0 0; 0 1.0321 0; 0 0 0.0938]
This is the wrong answer return by my MATLAB code --
RIMatrix_sph1 = [0.0938 -0.938 -0.9382; -0.9382 0.0938 -0.9382; -0.9382 -0.9382 0.0938]
Thank you Again!

  5 Comments

Show 2 older comments
Wai Han
Wai Han on 28 Sep 2020
Okay, Thanks for the help guys!
Here is the full python code, the variables are the same in the MATLAB too
import math
import numpy as np
import modern_robotics as mr
pi = math.pi
print("\n------ Question 1 ------")
density = 5600
rad_cyl = 0.02
len_cyl = 0.2
rad_sph = 0.1
mass_cyl = density * pi * len_cyl * rad_cyl**2
print("\nMass of Cylinder: ", mass_cyl, sep='')
Ixx_cyl = mass_cyl * (3 * rad_cyl**2 + len_cyl**2) / 12
Iyy_cyl = Ixx_cyl
Izz_cyl = mass_cyl * rad_cyl**2 / 2
RIMatrix_cyl = np.diag([Ixx_cyl, Iyy_cyl, Izz_cyl])
print("\nRIMatrix_cyl:\n", np.array2string(RIMatrix_cyl, separator=','), sep='')
mass_sph = density * 4/3 * pi * rad_sph**3
print("\nMass of Sphere: ", mass_sph, sep='')
Ixx_sph = mass_sph * 2/5 * rad_sph**2
Iyy_sph = Ixx_sph
Izz_sph = Ixx_sph
RIMatrix_sph = np.diag([Ixx_sph, Iyy_sph, Izz_sph])
print("\nRIMatrix_sph:\n", np.array2string(RIMatrix_sph, separator=','), sep='')
'''exploit Steiner's theorem'''
q1 = np.array([[0], [0], [rad_sph + len_cyl/2]])
RIMatrix_sph1 = RIMatrix_sph + mass_sph * (np.dot(q1.T, q1) * np.eye(3) - np.dot(q1, q1.T))
q2 = np.array([[0], [0], [-rad_sph - len_cyl/2]])
RIMatrix_sph2 = RIMatrix_sph + mass_sph * (np.dot(q2.T, q2) * np.eye(3) - np.dot(q2, q2.T))
print("\nRIMatrix_sph1:\n", np.array2string(RIMatrix_sph1, separator=','), sep='')
print("\nRIMatrix_sph2:\n", np.array2string(RIMatrix_sph2, separator=','), sep='')
RIMatrix_q1 = RIMatrix_cyl + RIMatrix_sph1 + RIMatrix_sph2
RIMatrix_q1_off = np.around(RIMatrix_q1, decimals=2)
print("\nQuestion 1:\n", np.array2string(RIMatrix_q1_off, separator=','), sep='')
Wai Han
Wai Han on 28 Sep 2020
This is the MATLAB version --
rho = 5600;
cyl_rad = 0.02;
cyl_length = 0.2;
sph_rad = 0.1;
cyl_mass = rho*pi*cyl_rad^2*cyl_length
sph_mass = (4*rho*pi*sph_rad^3)/3
% MoI of cylinder
cyl_Ixx =(cyl_mass*(3*cyl_rad^2 + cyl_length^2))/12;
cyl_Iyy = cyl_Ixx;
cyl_Izz = (cyl_mass*cyl_rad^2)/2;
cyl_MoI = diag([cyl_Ixx,cyl_Iyy,cyl_Izz])
%MoI of sphere
sph_Ixx = (sph_mass * sph_rad^2 * 2)/5;
sph_MoI = diag([sph_Ixx,sph_Ixx,sph_Ixx])
%Steiner's theorem
%This is the part which I encounter issuses:
%This picture describes the equation that I used!
This is the equation that I used below!
q1 = [0,0,sph_rad + cyl_length/2]
q2 = [0,0,-sph_rad - cyl_length/2]
I_cm_sph1 = sph_MoI + sph_mass *(dot(transpose(q1),q1) * eye(3) - dot(q1,transpose(q1)))
I_cm_sph2 = sph_MoI + sph_mass *(dot(transpose(q2),q2) * eye(3) - dot(q2,transpose(q2)))
total_MoI = cyl_MoI + I_cm_sph1 + I_cm_sph2

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 28 Sep 2020
Edited: Stephen Cobeldick on 28 Sep 2020
You are using the wrong operator. You need basic matrix multiplication, not the dot product:
>> q1 = [0;0;0.2];
>> q1.'*q1
ans = 0.040000
>> q1*q1.'
ans =
0.000000 0.000000 0.000000
0.000000 0.000000 0.000000
0.000000 0.000000 0.040000
As the dot product of two vectors by definition returns a scalar, the numpy.dot implementation is a bit strange. Reading the numpy documentation clarifies that "If both a and b are 2-D arrays, it is matrix multiplication..." , which is what your python code is actually doing (not a dot product). Note that the numpy documentation also recommends using an explicit matrix multiplication, rather than this bizarre overloaded "dot" operator: "..using matmul or a @ b is preferred".
Also note that your screenshot shows (implicit) matrix multiplication of those terms, not the dot product.

  1 Comment

Wai Han
Wai Han on 28 Sep 2020
Thanks for the help Mr.Cobeldick! I've thoroughly read through the links you provided " Wikipedia the dot product ". I was mistakenly calculating the vectors. I got them now..

Sign in to comment.

More Answers (1)

Vasishta Bhargava
Vasishta Bhargava on 28 Sep 2020
Edited: madhan ravi on 28 Sep 2020
% This is the MATLAB code
mass_sph = 23.4572;
RIMatrix_sph = [0.0938 0 0; 0 0.0938 0; 0 0 0.0938]
RIMatrix_sph1 = RIMatrix_sph + mass_sph * (dot(transpose(q1),q1). * eye(3) - dot(q1,transpose(q1)))
Try the above

  5 Comments

Show 2 older comments
Wai Han
Wai Han on 28 Sep 2020
Hello, I think I have found the difference.
In this matlab code --
q1 = [0;0;sph_rad + cyl_length/2];
x = dot(transpose(q1),q1)
y = dot(q1,transpose(q1))
The result i get is x = 0.4 and y =0.4 , which in my opinion is wrong for the dot product. I am afraid if it is the correct answer since I am not very familer with the MATLAB function. But by mathematical way, the answer should be the result returned by the python code.
Here is the python code--
q1 = np.array([[0], [0], [0.2]])
x = np.dot(q1.T, q1)
y = np.dot(q1 , q1.T)
print(x)
print(y)
The answer returned is x = [0.04] and y =[0 0 0;0 0 0; 0 0 0.04]
Can someone explain me why that happens?
Again, thanks for your help Sirs!
Stephen Cobeldick
Stephen Cobeldick on 28 Sep 2020
"The result i get is x = 0.4 and y =0.4 , which in my opinion is wrong for the dot product."
According to Wikipedia the dot product is "is an algebraic operation that takes two equal-length sequences of numbers (usually coordinate vectors), and returns a single number", and gives this definition:
Wolfram Mathworld gives this definition (amongst others):
X.Y = X1*Y1 + .. + Xn*Yn
Both of these agree with MATLAB.
"But by mathematical way, the answer should be the result returned by the python code."
Not according to standard mathematics taught in most schools and universities. What the numpy code returns is actually the matrix multiplication of a column vector and a row vector, it is NOT the dot product!

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by