Dot product does not work for symbolic vectors

11 views (last 30 days)
Hi, I'm working on dynamics simulation with matlab, so I have the vector r with the size of [3 1]. I examine the calculation
dot(r,r) - (r(1)^2+r(2)^2 +r(3)^2) and I got an answer different than 0. Is there any explanation for this ?
syms t g real
syms alpha1(t) beta1(t) gamma1(t)
syms m1 m2 m3 m4 real
syms h d1 d2 r1 r2 r3 r4 t1 t2 t3 L real
syms a b c d5 real
r = [(a*b*c*d5*m1*sin(conj(gamma1(t))-gamma(t)))/(-a*b*c*m1 +pi*m3*r1^2*t1 +pi*m1*r1^2*t1);
-(0.5000*(- a*b*c^2*m1 + a*b*c*m1*t1 + 3.1416*m3*r1^2*t1*t3 + 3.1416*m3*r1^2*t1^2))/(- a*b*c*m1 + 3.1416*m3*r1^2*t1 + 3.1416*m1*r1^2*t1);
-(a*b*c*d5*m1*cos(conj(gamma1(t)) - gamma1(t)))/(- a*b*c*m1 + 3.1416*m3*r1^2*t1 + 3.1416*m1*r1^2*t1) ];
r_x = r(1) ;
r_y = r(2) ;
r_z = r(3) ;
% dot minus sum of elements. This should be 0
% I think this is a problem with dot function of MATLAB.
dot(r,r) - (r_x^2 + r_y^2 + r_z^2 )
ans = 

Answers (2)

Sam Chak
Sam Chak on 21 Apr 2024
Probably it has something to do with how MATLAB interprets 'gamma1(t)' .
syms t g y real
syms alpha1(t) beta1(t) gamma1(t)
syms m1 m2 m3 m4 real
syms h d1 d2 r1 r2 r3 r4 t1 t2 t3 L real
syms a b c d5 real
% r = [(a*b*c*d5*m1*sin(conj(gamma1(t)) - gamma1(t)))/(-a*b*c*m1 +pi*m3*r1^2*t1 +pi*m1*r1^2*t1);
% -(0.5000*(- a*b*c^2*m1 + a*b*c*m1*t1 + 3.1416*m3*r1^2*t1*t3 + 3.1416*m3*r1^2*t1^2))/(- a*b*c*m1 + 3.1416*m3*r1^2*t1 + 3.1416*m1*r1^2*t1);
% -(a*b*c*d5*m1*cos(conj(gamma1(t)) - gamma1(t)))/(- a*b*c*m1 + 3.1416*m3*r1^2*t1 + 3.1416*m1*r1^2*t1) ];
r1 = (a*b*c*d5*m1*sin(conj(y) - y))/(- a*b*c*m1 + pi*m3*r1^2*t1 + pi*m1*r1^2*t1);
r2 = -(0.5000*(- a*b*c^2*m1 + a*b*c*m1*t1 + pi*m3*r1^2*t1*t3 + pi*m3*r1^2*t1^2))/(- a*b*c*m1 + pi*m3*r1^2*t1 + pi*m1*r1^2*t1);
r3 = -(a*b*c*d5*m1*cos(conj(y) - y))/(- a*b*c*m1 + pi*m3*r1^2*t1 + pi*m1*r1^2*t1);
r = [r1;
r2;
r3];
r_x = r(1);
r_y = r(2);
r_z = r(3);
% dot minus sum of elements. This should be 0
% I think this is a problem with dot function of MATLAB.
A = dot(r,r);
A = simplify(A, 'steps', 100)
A = 
B = (r_x)^2 + (r_y)^2 + (r_z)^2;
B = simplify(B, 'steps', 100)
B = 
C = A - B
C = 
0

Torsten
Torsten on 21 Apr 2024
Since you cannot assume functions to be real-valued, you must use
dot(r,r) - (r_x*r_x' + r_y*r_y' + r_z*r_z' )
instead of
dot(r,r) - (r_x^2 + r_y^2 + r_z^2 )
  5 Comments
Torsten
Torsten on 21 Apr 2024
syms t g real
syms alpha1(t) beta1(t) gamma1(t)
syms m1 m2 m3 m4 real
syms h d1 d2 r1 r2 r3 r4 t1 t2 t3 L real
syms a b c d5 real
assume(alpha1(t), 'real')
assumeAlso(beta1(t), 'real')
assumeAlso(gamma1(t), 'real')
r = [(a*b*c*d5*m1*sin(conj(gamma1(t))-gamma(t)))/(-a*b*c*m1 +pi*m3*r1^2*t1 +pi*m1*r1^2*t1);
-(0.5000*(- a*b*c^2*m1 + a*b*c*m1*t1 + 3.1416*m3*r1^2*t1*t3 + 3.1416*m3*r1^2*t1^2))/(- a*b*c*m1 + 3.1416*m3*r1^2*t1 + 3.1416*m1*r1^2*t1);
-(a*b*c*d5*m1*cos(conj(gamma1(t)) - gamma1(t)))/(- a*b*c*m1 + 3.1416*m3*r1^2*t1 + 3.1416*m1*r1^2*t1) ];
r_x = r(1) ;
r_y = r(2) ;
r_z = r(3) ;
% dot minus sum of elements. This should be 0
% I think this is a problem with dot function of MATLAB.
dot(r,r) - (r_x^2 + r_y^2 + r_z^2 )
ans = 
Dyuman Joshi
Dyuman Joshi on 22 Apr 2024
There might be a typo in the code and it should be gamma1(t) instead of gamma(t), as is the case in the cos term in 3rd row -
syms t g real
syms alpha1(t) beta1(t) gamma1(t)
syms m1 m2 m3 m4 real
syms h d1 d2 r1 r2 r3 r4 t1 t2 t3 L real
syms a b c d5 real
assume(alpha1(t), 'real')
assumeAlso(beta1(t), 'real')
assumeAlso(gamma1(t), 'real')
%% v
r = [(a*b*c*d5*m1*sin(conj(gamma1(t))-gamma1(t)))/(-a*b*c*m1 +pi*m3*r1^2*t1 +pi*m1*r1^2*t1);
-(0.5000*(- a*b*c^2*m1 + a*b*c*m1*t1 + 3.1416*m3*r1^2*t1*t3 + 3.1416*m3*r1^2*t1^2))/(- a*b*c*m1 + 3.1416*m3*r1^2*t1 + 3.1416*m1*r1^2*t1);
-(a*b*c*d5*m1*cos(conj(gamma1(t)) - gamma1(t)))/(- a*b*c*m1 + 3.1416*m3*r1^2*t1 + 3.1416*m1*r1^2*t1) ];
r_x = r(1) ;
r_y = r(2) ;
r_z = r(3) ;
dot(r,r) - (r_x^2 + r_y^2 + r_z^2 )
ans = 
0

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!