# How to solve differential equation including derivative of eigenvalue of tensor?

27 views (last 30 days)
Hiroki ENDO on 24 Sep 2022
Commented: Bruno Luong on 28 Oct 2022
I am trying to solve this equation by ODE45.
here, A is sencond-order tensor, and is time derivative of eigenvalue of A.
So, I try to wirte like bellow.
tspan=[0:0.01:1];
Azero= [0.4 0.1 0; 0.1 0.4 0.1; 0 0.1 0.2]
[T,Av] = ode45('Adotxx', tspan, tens2vec(Azero)); % Azero converted to vector style, this is another function
%%
A = vec2tens(Av); % Azero converted to tensor style from vector style
% making eigenvector
[e, lambda] = eig(A);
% must be sorted eigenvectors and eigenvalues
[lambda_index, SortO] = sort(diag(lambda),'descend');
lambda_temp = zeros(3);
e_temp = zeros(3);
for i =1:3
lambda_temp(i,i) = lambda_index(i);
e_temp(1:3,i) = e(1:3,SortO(i));
end
% time derivative of lambda
lambda_dot = diff(lambda);
X = lambda_dot
% differential equation
end
However, you know, the lambda_dot is not true time derivative. it is just derivative in matrix at one moment like Differentiation - MATLAB & Simulink - MathWorks. lambda_dot become matrix of (2,3) by diff(lambda).
How can I correct the code?

Torsten on 28 Sep 2022
Edited: Torsten on 28 Sep 2022
A third variante of the code also seems to work under MATLAB ( for high values of the integration tolerances :-) )
syms s
A = sym('A',[3 3]);
% Define characteristic polynomial
determinant = det(A-s*eye(3));
% Compute eigenvalues
lambda = solve(determinant==0,s,'MaxDegree',3);
tspan = [0:0.01:1];
Azero = [0.4 0.1 0; 0.1 0.4 0.1; 0 0.1 0.2];
L = double(subs(lambda,A,Azero)).';
Azero = [Azero;L];
Azero = reshape(Azero.',[12,1]);
M = [eye(9),zeros(9,3);zeros(3,12)];
alpha = 0.2;
M(1,10) = -alpha;
M(5,11) = -alpha;
M(9,12) = -alpha;
options = odeset('Mass',M,'RelTol',1e-3,'AbsTol',1e-3);
[T,Av] = ode15s(@(t,A)Adotxx(t,A,lambda), tspan, Azero, options); % Azero converted to vector style, this is another function
plot(T,Av(:,1))
Warning: Imaginary parts of complex X and/or Y arguments ignored.
A = reshape(A,[3 4]).';
Av = A(1:3,:);
L = A(4,:);
A = sym('A',[3 3]);
LL = double(subs(lambda,A,Av)).';
% differential equation
end
Hiroki ENDO on 28 Oct 2022
I am sorry to late to reply.
This code was worked very well.
Thanks again.

Walter Roberson on 24 Sep 2022
You need to construct the formula for the eigenvalues of the derivative based on the equation for A. As you have a 3x3 matrix that will possibly involve the roots of a cubic equation. You must write them out in explicit form. This all must be calculated ahead of time.
Then inside you substitute particular numeric values into the equations inside your ode function. It is important that you do not use eig and sort, because those do not use consistent orders and so you would fail the requirements for continuous derivatives of your equations.
Bruno Luong on 28 Oct 2022
Not differentiable continuously.
I think using consistent fomal radical formula doesn't ensure there is no branch swapping either.

Torsten on 26 Sep 2022
Edited: Torsten on 27 Sep 2022
See if it works. I cannot test it.
syms s
A = sym('A',[3 3]);
% Define characteristic polynomial
determinant = det(A-s*eye(3));
% Compute eigenvalues
lambda = solve(determinant==0,s,'MaxDegree',3)
% Compute d(lambda_k)/d(A(i,j))
for i = 1:3
for j = 1:3
for k = 1:3
d(k,i,j) = diff(lambda(k),A(i,j))
end
end
end
tspan = [0:0.01:1];
Azero = [0.4 0.1 0; 0.1 0.4 0.1; 0 0.1 0.2];
[T,Av] = ode15i(@(t,Av, Avd)Adotxx(t, Av, Avd, d), tspan, tens2vec(Azero), tens2vec(Adotzero)); % Azero converted to vector style, this is another function
function res = Adotxx(t, Av, Avd, d)
A = sym('A',[3 3]);
Av = vec2tens(Av);
Avd = vec2tens(Avd);
d_num = zeros(3,3,3);
for i = 1:3
for j = 1:3
for k = 1:3
d_num(k,i,j) = double(subs(d(k,i,j),A,Av));
end
end
end
lambda_dot = zeros(3,1);
for k =1:3
lambda_dot(k) = 0.0;
for i = 1:3
for j = 1:3
lambda_dot(k) = lambda_dot(k) + d_num(k,i,j)*Avd(i,j);
end
end
end
alpha = 1;
f = @(A) A;
res = Avd - f(Av) - alpha*diag(lambda_dot);
res = tens2vec(res);
end
Hiroki ENDO on 28 Sep 2022
And I put bellow code in function for derivative. It can see time record of calculation.
t=t
It works. But values of t become smaller and smaller. It never reaches over 0.01.
So, now I am using the profiler of MATLAB. I don't know if it works. I am seeking any ideas.
If you have any ideas, could you please teach me?
Thank you.

Torsten on 27 Sep 2022
Edited: Torsten on 27 Sep 2022
Here is a numerical code to solve your problem.
It's risky to use this approach since - as Walter Roberson noted - the ordering of the eigenvalues coming from "eig" might change during the computation. This will cause wrong results.
Thus the suggested symbolic approach from above is more safe.
tspan=[0:0.01:1];
Azero= [0.4 0.1 0; 0.1 0.4 0.1; 0 0.1 0.2];
[~, lambda] = eig(Azero);
lambda=[lambda(1,1),lambda(2,2),lambda(3,3)];
Azero = [Azero;lambda];
for i=1:4
for j=1:3
Av((i-1)*3+j) = Azero(i,j);
end
end
M = [eye(9),zeros(9,3);zeros(3,12)];
alpha = 0.2;
M(1,10) = -alpha;
M(5,11) = -alpha;
M(9,12) = -alpha;
options = odeset('Mass',M);%,'RelTol',1e-8,'AbsTol',1e-8);
[T,Av] = ode15s(@Adotxx, tspan, Av, options); % Azero converted to vector style, this is another function
plot(T,Av(:,12))
%%
for i=1:3
for j=1:3
Av(i,j) = A((i-1)*3+j);
end
L(i) = A(9+i);
end
[~, lambda] = eig(Av);
lambda = [lambda(1,1),lambda(2,2),lambda(3,3)];
% differential equation
for i=1:4
for j=1:3
end
end
end
Hiroki ENDO on 28 Sep 2022
Oh. OK.
I will try hard more.

### Categories

Find more on Assumptions in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!

Translated by