Trying to run a differentiation loop

I have a matrix X_dot =
3*g*k*x1*t^2 - 2*g*k*x1*t - k*x2
g*x1 - 2*g*t*x1
0
Another matrix, x =
[x1, x2, x3]
when I run these codes separately:
diff(X_dot(2,1),x(1,1))
diff(X_dot(1,1),x(1,1))
I get good answers. But when I put it in a loop, I get weird conversion errors.
My loop looks like this:
L2 = zeros(3)
for r = 1:3
for c = 1:3
L2(r,c) = diff(X_dot(c,r),x(r,c));
end
end
The aim is to differentiate each row of X_dot by each column of x and get a 3x3 matrix output in symbolic form
This is the whole code:
clear all
clc
%%Ans1
syms X1 X2 X3 k t g x1 x2 x3
eqn1 = X1 + (k*t*X2) == x1;
eqn2 = (g*((t-1)*(t*X1))) + ((1+ (g*k)*((t-1)*(t^2)))*X2) == x2;
eqn3 = X3 == x3
[A,B] = equationsToMatrix([eqn1, eqn2, eqn3], [X1, X2, X3])
X = linsolve(A,B)
F = [1 k*t 0;(g*t)*(t-1) g*k*t^2*(t - 1)+1 0;0 0 1]
J = det(F)
F_inv = inv(F)
%%Ans3
F_d = simplify(diff(F,t))
L = simplify(F_d*F_inv)
expand(L(2,1))
X_dot = diff(X,t)
L2 = zeros(3)
x = [x1,x2,x3]
diff(X_dot(2,1),x(1,1))
for r = 1:3
for c = 1:3
L2(r,c) = diff(X_dot(c,r),x(r,c));
end
end
L2
Please help

2 commentaires

KSSV
KSSV le 23 Oct 2022
Copy the full code here.
Saim
Saim le 23 Oct 2022
Post is updated with the whole code, hope that helps

Connectez-vous pour commenter.

 Réponse acceptée

clear all
clc
%%Ans1
syms X1 X2 X3 k t g x1 x2 x3
eqn1 = X1 + (k*t*X2) == x1;
eqn2 = (g*((t-1)*(t*X1))) + ((1+ (g*k)*((t-1)*(t^2)))*X2) == x2;
eqn3 = X3 == x3;
[A,B] = equationsToMatrix([eqn1, eqn2, eqn3], [X1, X2, X3]);
X = linsolve(A,B);
F = [1 k*t 0;(g*t)*(t-1) g*k*t^2*(t - 1)+1 0;0 0 1];
J = det(F);
F_inv = inv(F);
%%Ans3
F_d = simplify(diff(F,t));
L = simplify(F_d*F_inv);
expand(L(2,1));
X_dot = diff(X,t);
%L2 = zeros(3);
x = [x1;x2;x3];
diff(X_dot(2,1),x(1,1))
ans = 
for r = 1:3
for c = 1:3
L2(r,c) = diff(X_dot(r,1),x(c,1));
end
end
L2
L2 = 

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Produits

Version

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by