Clear Filters
Clear Filters

Jacobian calculation of symbolic variables which are function of other variables.

49 views (last 30 days)
Hi everyone,
I am trying to find the jacobian for a transformation matrix. I am using symbolic variables (T, l, m, n). Each of these variables are function of 4 others variables (delta1 delta2 delta3 and delta4), as:
syms u v w p q r phi theta psi x y z;
syms delta1 delta2 delta3 delta4;
% Aerodynamics
V = sqrt(u^2 + v^2 + w^2);
q_bar = (1/2) * rho * V^2;
m = (-1.35* Kf * delta1^2) + (1.35* Kf * delta2^2) + (1.35*K * Kf * delta3^2) + (-1.35* Kf * delta4^2);
l = (0.904* Kf *delta1^2) + (-0.904* Kf *delta2^2) + (0.904* Kf *delta3^2) + (-0.904* Kf *delta4^2);
n = (Km * delta1^2) + (Km * delta2^2) - (Km * delta3^2) - (Km * delta4^2);
T1= (Kf * delta1^2);
T2= (Kf * delta2^2);
T3= (Kf * delta3^2);
T4= (Kf * delta4^2);
T= T1 + T2 + T3 + T4;
phi_dot = p + tan(theta) * (q * sin(phi) + r * cos(phi));
theta_dot = q * cos(phi) - r * sin(phi);
psi_dot = (q * sin(phi) + r * cos(phi)) / cos(theta);
x_dot = cos(psi)*cos(theta)*u + (cos(psi)*sin(theta)*sin(phi) - sin(psi)*cos(phi))*v + (cos(psi)*sin(theta)*cos(phi) + sin(psi)*sin(phi))*w;
y_dot = (sin(psi)*cos(theta))*u + (sin(psi)*sin(theta)*sin(phi) + cos(psi)*cos(phi))*v + (sin(psi)*sin(theta)*cos(phi) - cos(psi)*sin(phi))*w;
z_dot = -sin(theta)*u + cos(theta)*sin(phi)*v + cos(theta)*cos(phi)*w;
f_x = - mass*g * sin(theta);
f_y = mass*g * sin(phi) * cos(theta);
f_z = mass*g * cos(phi) * cos(theta) - T ;
u_dot = r*v - q*w + (1/mass) * (f_x);
v_dot = p*w - r*u + (1/mass) * (f_y);
w_dot = q*u - p*v + (1/mass) * (f_z);
p_dot = gam(1)*p*q - gam(2)*q*r + gam(3)*l + gam(4)*n;
q_dot = gam(5)*p*r - gam(6)*(p^2 - r^2) + (1/J_yy) * m;
r_dot = gam(7)*p*q - gam(1)*q*r + gam(4)*l + gam(8)*n;
% Collect dynamics
f = [ x_dot;
y_dot;
z_dot;
phi_dot;
theta_dot;
psi_dot;
u_dot;
v_dot;
w_dot;
p_dot;
q_dot;
r_dot];
jacobian(f,[T l m n]);
So when calculating jacobian(f,[T l m n]) , i have the error:
"Invalid argument at position 2. Argument must be a variable, a symfun without a formula, or a symfun whose formula is a variable."
Can someone please give me a solution to the problem ?

Answers (1)

Steven Lord
Steven Lord on 5 Aug 2024 at 14:34
I tried running your code but there are several variables that are not defined: rho, Kf, K, Km, mass, g, gam, J_yy. Specifying dummy values for those variables, let's look at your f variable and see if it's functions of T, l, m, and n.
rho = 42;
Kf = 999;
K = 17;
Km = 18;
mass = 12345;
g = 9.8;
gam = 9:-1:1;
J_yy = 343455;
syms u v w p q r phi theta psi x y z;
syms delta1 delta2 delta3 delta4;
% Aerodynamics
V = sqrt(u^2 + v^2 + w^2);
q_bar = (1/2) * rho * V^2;
m = (-1.35* Kf * delta1^2) + (1.35* Kf * delta2^2) + (1.35*K * Kf * delta3^2) + (-1.35* Kf * delta4^2);
l = (0.904* Kf *delta1^2) + (-0.904* Kf *delta2^2) + (0.904* Kf *delta3^2) + (-0.904* Kf *delta4^2);
n = (Km * delta1^2) + (Km * delta2^2) - (Km * delta3^2) - (Km * delta4^2);
T1= (Kf * delta1^2);
T2= (Kf * delta2^2);
T3= (Kf * delta3^2);
T4= (Kf * delta4^2);
T= T1 + T2 + T3 + T4;
phi_dot = p + tan(theta) * (q * sin(phi) + r * cos(phi));
theta_dot = q * cos(phi) - r * sin(phi);
psi_dot = (q * sin(phi) + r * cos(phi)) / cos(theta);
x_dot = cos(psi)*cos(theta)*u + (cos(psi)*sin(theta)*sin(phi) - sin(psi)*cos(phi))*v + (cos(psi)*sin(theta)*cos(phi) + sin(psi)*sin(phi))*w;
y_dot = (sin(psi)*cos(theta))*u + (sin(psi)*sin(theta)*sin(phi) + cos(psi)*cos(phi))*v + (sin(psi)*sin(theta)*cos(phi) - cos(psi)*sin(phi))*w;
z_dot = -sin(theta)*u + cos(theta)*sin(phi)*v + cos(theta)*cos(phi)*w;
f_x = - mass*g * sin(theta);
f_y = mass*g * sin(phi) * cos(theta);
f_z = mass*g * cos(phi) * cos(theta) - T ;
u_dot = r*v - q*w + (1/mass) * (f_x);
v_dot = p*w - r*u + (1/mass) * (f_y);
w_dot = q*u - p*v + (1/mass) * (f_z);
p_dot = gam(1)*p*q - gam(2)*q*r + gam(3)*l + gam(4)*n;
q_dot = gam(5)*p*r - gam(6)*(p^2 - r^2) + (1/J_yy) * m;
r_dot = gam(7)*p*q - gam(1)*q*r + gam(4)*l + gam(8)*n;
% Collect dynamics
f = [ x_dot;
y_dot;
z_dot;
phi_dot;
theta_dot;
psi_dot;
u_dot;
v_dot;
w_dot;
p_dot;
q_dot;
r_dot]
f = 
I don't see variables named T, l, m, or n in the array f. You have defined expressions in T, l, m, and n but you cannot use those as the second input to the jacobian function. Take a look at T:
T
T = 
What exactly would you expect the derivative of f(10) with respect to T to be?
f(10)
ans = 
  3 Comments
Steven Lord
Steven Lord on 5 Aug 2024 at 16:11
the array f includes variables which contains the variables T,l,m,n so technically, it contains these variables.
Which element of f contains the character T?
The array f was created using the expressions stored in the variable T, but that's not the same as saying that f contains the variable T.
To repeat my question from the end of my answer, what exactly would you expect the derivative of f(10) with respect to T to be? [You can use the values of the constants from your comment instead of the dummy values I added to generate f(10), but show us the values of T, f(10), and the specific value you expect to have.]
A
A on 5 Aug 2024 at 17:19
The derivative of f(10) with respect to T should be -1/mass, which should give -0.016667. That's what I am trying to verify with my script.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!