How to solve time dependent ODEs of the form (dx/dt = f(x,t))using ODE45

7 views (last 30 days)
I have a complex time dependent dynamic system of order 4. I want to solve this system using ODE45 in matrix form.
My dynamic equation is of the form below
I need to solve this time dependent ODEs in Matrix form. I am getting dimension mismatch error for the below function;
Can anybody help me with this?
Thanks
function xdot = TV_dynamic(tspan, x, k)
%%% k is the time dependent variable
con_exp = (0.1/2) * exp(x(1,1) + x(2,1)+ x(3,1));
Delta_z = [(x(1,1) - (sin(0.1*k)/2) + con_exp + (x(4,1)*cos(0.1*k)));
(x(2,1) - (sin(0.1*k)/2) + con_exp + (x(4,1)*sin(0.1*k)));
(x(3,1) - (sin(0.1*k)/2) + con_exp + x(4,1));
((cos(0.1*k)*x(1,1)) + (sin(0.1*k)*x(2,1)) + x(3,1)- 1)];
Delta_zz = [1+con_exp con_exp con_exp cos(0.1*k);
con_exp 1+con_exp con_exp sin(0.1*k);
con_exp con_exp 1+con_exp 1;
cos(0.1*k) sin(0.1*k) 1 0];
Delta_zt = [((-0.1*cos(0.1*k)/2) - (0.1 * x(4,1) * sin(0.1*k)));
((-0.1*cos(0.1*k)/2) + (0.1 * x(4,1) * cos(0.1*k)));
(-0.1*cos(0.1*k)/2) ;
((-0.1* sin(0.1*k)* x(1,1)) + (0.1* cos(0.1*k))*x(2,1))];
xdot(:,1) = (- inv(Delta_zz)) * ( P * Delta_z(:,1) + Delta_zt );
end

Answers (1)

Ameer Hamza
Ameer Hamza on 22 Jun 2020
It depends on how the time-dependent term is defined. The following shows an example if the term 'k' depends on time.
k_fun = @(t) sin(t); % k as function of time
tspan = [0 1];
ic = [1; 0; 0; 0];
[t, y] = ode45(@(t, x) TV_dynamic(t, x, k_fun(t)), tspan, ic);
plot(t, y);
function xdot = TV_dynamic(tspan, x, k)
%%% k is the time dependent variable
P = rand(4); % for example
con_exp = (0.1/2) * exp(x(1,1) + x(2,1)+ x(3,1));
Delta_z = [(x(1,1) - (sin(0.1*k)/2) + con_exp + (x(4,1)*cos(0.1*k)));
(x(2,1) - (sin(0.1*k)/2) + con_exp + (x(4,1)*sin(0.1*k)));
(x(3,1) - (sin(0.1*k)/2) + con_exp + x(4,1));
((cos(0.1*k)*x(1,1)) + (sin(0.1*k)*x(2,1)) + x(3,1)- 1)];
Delta_zz = [1+con_exp con_exp con_exp cos(0.1*k);
con_exp 1+con_exp con_exp sin(0.1*k);
con_exp con_exp 1+con_exp 1;
cos(0.1*k) sin(0.1*k) 1 0];
Delta_zt = [((-0.1*cos(0.1*k)/2) - (0.1 * x(4,1) * sin(0.1*k)));
((-0.1*cos(0.1*k)/2) + (0.1 * x(4,1) * cos(0.1*k)));
(-0.1*cos(0.1*k)/2) ;
((-0.1* sin(0.1*k)* x(1,1)) + (0.1* cos(0.1*k))*x(2,1))];
xdot(:,1) = (- inv(Delta_zz)) * ( P * Delta_z(:,1) + Delta_zt );
end
Note that I have defined P as a random 4x4 matrix.
  2 Comments
Bhagyashree Umathe
Bhagyashree Umathe on 22 Jun 2020
Thank you for your reply. I want to confirm that in main code, "k" is not reflecting anywhere while in function file, time dependent variable is "k". Is it okay?
Also, "k_fun = @(t) sin(t)" will it take care of cos(t) in dynamic equation?
Ameer Hamza
Ameer Hamza on 22 Jun 2020
Yes, it is fine. 'k' itself does not need to appear in the main script. Also, k_fun = @(t) sin(t), is just an example. It indicates that k is a function of time. In this case, I used sin() as an example. You can vary 'k' however you want.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!