How to get the jacobian of a symbolic vector?

23 views (last 30 days)
Hi,
The following code uses diff(X) and jacobian(r,X) in order to get velocity and accelaration of a point. However, this error appears when I try run:
"Error using sym/jacobian (line 44)
The second argument must be a vector of variables."
t is a variable (time), Phi, Theta and R are functions of t, X is a vector using Phi, Theta and R,(position of the point in cylindrical coordenates) r is function of Phi, Theta and R (position of a point in the cartesian coordenates).
syms t
Phi=sym('Phi(t)');
Theta=sym('Theta(t)');
R=sym('R(t)');
X = [Phi, Theta, R].'
dXdt=diff(X);
d2Xdt2=diff(dXdT);
r = [R*cos(Phi)*sin(Theta), R*sin(Phi)*sin(Theta), R*cos(Theta)].' %position
Ht = jacobian (r,X) %jacobian
vt=Ht*dXdt %velocity
at=Ht*d2Xdt2+diff(Ht,t)*dXdt %acceleration
=========================================================
It's strange, because the "jacobian(r,X)" works very well in the following code:
syms Phi Theta R
X = [Phi Theta R].';
r = [R*cos(Phi)*sin(Theta), R*sin(Phi)*sin(Theta), R*cos(Theta)].'
jacobian(r,X)
In this case, I can't get dXdt, but when Phi, Theta and R are functions of t, I can't get the jacobian :/
  2 Comments
John D'Errico
John D'Errico on 21 Mar 2015
Use the "{} Code" button to format your code. Otherwise, it shows as an unreadable mess.

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 21 Mar 2015
Note that in your code, you have the lines...
dXdt=diff(X);
d2Xdt2=diff(dXdT);
MATLAB gets upset here, because you can't type. It gives the error message:
Undefined function or variable 'dXdT'.
You never defined dXdT, only dXdt.
Next, the error you get reflects a problem in your code. You have defined the FUNCTIONS, Theta, Phi and R, as parametric functions of t. They are not variables. The only variable in this is t.
  2 Comments
Marlon Saveri Silva
Marlon Saveri Silva on 21 Mar 2015
Edited: Marlon Saveri Silva on 21 Mar 2015
Does it mean that's impossible get the jacobian when Theta, Phi and R are parametric functions?
To build the jacobian of a function r(r1, r2, r3) relation to X(x1,x2,x3), I need values like: diff(r1,x1). Since x1 is a parametric function Phi(t), it's impossible get diff(r1,Phi(t))?
diff(R*cos(Phi)*sin(Theta), Phi(t))
Marlon Saveri Silva
Marlon Saveri Silva on 21 Mar 2015
Well, if its not possible, the code below solve my problem.
%This code is used to check position, velocity and acceleration of a point in the cartesian coordinates system from the cylindrical coordinates system
%Obs.: when I use "_t", It means not parametric anymore, but with variables changed by its functions of "t". It was necessary because Matlab can not recognize "jacobian(r,X)" when X = X(Psi(t), Theta(t), R(t)). Therefore, it's necessary write Psi(t), Theta(t) and R(t) in a specific part of the code (after get the jacobian matrix).
clear
clc
syms t Psi Theta R
x = [Psi, Theta, R].'; %position of a point in the cylindrical coordinates system
r = [R*cos(Psi)*sin(Theta), R*sin(Psi)*sin(Theta), R*cos(Theta)].'; %position of this point in the cartesian system
Ht = jacobian (r,x); %jacobian matrix
%Define Psi, Theta and R functions of t
Psi_t=10;
Theta_t=3*t^2;
R_t=2*t;
%Get diff(Ht,t)
Ht_t=subs(Ht,Theta, Theta_t);
Ht_t=subs(Ht_t,R,R_t);
Ht_t=subs(Ht_t,Psi, Psi_t);
d_Ht_t=diff(Ht_t,t);
%Get dx/dt and d²x/dt²
dxdt= [diff(Psi_t,t) diff(Theta_t,t) diff(R_t,t)].';
d2xdt2= [diff(dxdt(1),t) diff(dxdt(2),t) diff(dxdt(3),t)].';
%Velocity Equation
vt=Ht_t*dxdt;
%Acceleration Equation
at=Ht_t*d2xdt2+d_Ht_t*dxdt

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!