I need help with Symbolic Differentiation Error

1 visualizzazione (ultimi 30 giorni)
Hello MATLAB community,
I am trying to do partial differentiation to find a 6x3 (Jr) matrix, but I am getting an error which says:
" Second argument must be a variable or a nonnegative integer specifying the number of differentiations."
I tried to play around by changing the code, but the problem is still the same.
Below is my code:
syms phi theta psi Psi
h = 110.75;
d2r = pi/180;
r2d = 180/pi;
ROT = [(cos(phi)*cos(psi))-(cos(theta)*sin(phi)*sin(psi)) -(cos(phi)*sin(psi))-(cos(theta)*cos(psi)*sin(phi)) (sin(phi)*sin(theta));...
(cos(psi)*sin(phi))+(cos(phi)*cos(theta)*sin(psi)) (cos(phi)*cos(theta)*cos(psi)-sin(phi)*sin(psi)) -(cos(phi)*sin(theta));...
(sin(theta)*sin(psi)) (cos(psi)*sin(theta)) cos(theta)];
px = (h/2)*(ROT(1,1)-ROT(2,2));
py = -h*ROT(2,1);
eqn = ROT(1,2) == ROT(2,1);
Psi = eqn;
pz = input('Please enter value for pz: ');
phi = input('Please enter value for phi: ')*d2r;
theta = input('Please enter value for theta: ')*d2r;
if phi >= 0
psi = -phi ;
elseif phi <= 0
psi = -phi ;
end
jr = [diff(px, pz), diff(px, phi), diff(px, theta); diff(py, pz), diff(py, phi), diff(py, theta); 1, 0, 0; 0, 1, 0; 0, 0, 1; diff(Psi, pz), diff(Psi, phi), diff(Psi, theta)];
Jr = double(subs(jr,{phi,theta,psi},{phi,theta,psi}))
You can assume the values for pz, phi, theta to be 435, 5, and 10, respectively.
Can anyone please help me with this problem.
Kind regards,
Shiv

Risposta accettata

Dyuman Joshi
Dyuman Joshi il 20 Feb 2023
Modificato: Dyuman Joshi il 20 Feb 2023
You are over-writing the symboic variables, psi and theta by taking input from the user. Rename the variables which you will use for substitution
Also, the if-else condition block seems redundant, as regardless of the value of phi, psi = -phi
syms phi theta psi Psi
h = 110.75;
d2r = pi/180;
r2d = 180/pi;
ROT = [(cos(phi)*cos(psi))-(cos(theta)*sin(phi)*sin(psi)) -(cos(phi)*sin(psi))-(cos(theta)*cos(psi)*sin(phi)) (sin(phi)*sin(theta));...
(cos(psi)*sin(phi))+(cos(phi)*cos(theta)*sin(psi)) (cos(phi)*cos(theta)*cos(psi)-sin(phi)*sin(psi)) -(cos(phi)*sin(theta));...
(sin(theta)*sin(psi)) (cos(psi)*sin(theta)) cos(theta)];
px = (h/2)*(ROT(1,1)-ROT(2,2));
py = -h*ROT(2,1);
eqn = ROT(1,2) == ROT(2,1);
Psi = eqn;
pz = 435; %input('Please enter value for pz: ');
phi0 = 5; %input('Please enter value for phi: ')*d2r;
theta0 = 10; %input('Please enter value for theta: ')*d2r;
if phi0 >= 0
psi0 = -phi0 ;
elseif phi0 <= 0
psi0 = -phi0 ;
end
jr = [diff(px, pz), diff(px, phi), diff(px, theta); diff(py, pz), diff(py, phi), diff(py, theta); 1, 0, 0; 0, 1, 0; 0, 0, 1; diff(Psi, pz), diff(Psi, phi), diff(Psi, theta)];
Jr = double(subs(jr,{phi,theta,psi},{phi0,theta0,psi0}))
Jr = 6×3
-25.2772 55.4023 25.2772 16.3887 76.5384 -16.3887 1.0000 0 0 0 1.0000 0 0 0 1.0000 1.0000 0 1.0000
  1 Commento
Shiv Karpoor
Shiv Karpoor il 20 Feb 2023
Hi Dyuman,
Thank you for the response.
This is actually half of the original code, the pz, phi, and theta values keep changing with for loop in the original script, and the input function for pz, phi, theta in this half-script helps verify values quickly.
But anyways, I understood your solution and it worked well.
Thank you.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Get Started with Symbolic Math Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by