Trying to run a differentiation loop

6 次查看(过去 30 天)
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

采纳的回答

Torsten
Torsten 2022-10-23
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 = 

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Particle & Nuclear Physics 的更多信息

产品


版本

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by