Trying to run a differentiation loop

조회 수: 3 (최근 30일)
Saim
Saim 2022년 10월 23일
답변: Torsten 2022년 10월 23일
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
KSSV
KSSV 2022년 10월 23일
Copy the full code here.
Saim
Saim 2022년 10월 23일
Post is updated with the whole code, hope that helps

댓글을 달려면 로그인하십시오.

채택된 답변

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에서 Numeric Solvers에 대해 자세히 알아보기

제품


릴리스

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by