Dot Product Doubt, don't get the result I expected.
조회 수: 21 (최근 30일)
이전 댓글 표시
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
채택된 답변
Stephen23
2020년 9월 28일
편집: Stephen23
2020년 9월 28일
>> 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개)
VBBV
2020년 9월 28일
편집: madhan ravi
2020년 9월 28일
% 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
Stephen23
2020년 9월 28일
편집: Stephen23
2020년 9월 28일
"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!
참고 항목
카테고리
Help Center 및 File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!