# ode 45 (matlab)

• Sep 24th 2010, 12:44 AM
ode 45 (matlab)
At the beginning of my request I remind you that because of the high volume of my program , it was not possible for me to bring it here, as a result I try to express my problem by an example. So my purpose is its algorithm.

suppose we have some differential equations in which one of the variables is matrix and beside that we have some non-matrix differential equations (by that I mean those which have different dimension with previous matrix and in particular consider it with dimention 1)

ODE45 is used for solving above equations. If we consider this problem in matrix state only, first we use "reshape" script and then we convert it with a columnar matrix at the end.

If we have some equations with dimention 1 beside this equation, how should it be modified?
%-----MAIN M-FILE--------------
clear all
%-----VARIABLE SET-UP--------------
exptime=30;
c=0.1;
g=9.81;
A = [0 1 0 0 0; 0 0 -g 0 1; 0 0 0 1 0; 0 0 0 -1/c 0;0 0 0 0 -20];
B = [0; 0; 0; 1/c; 0];
R = 10;
Q = [1 0 0 0 0; 0 0 0 0 0; 0 0 0 0 0; 0 0 0 0 0;0 0 0 0 0];
S = [0; 0; 0; 0; 0];
E = eye(5);
dps=70;
stepnum=dps*exptime
I5=eye(5);
%-----TO RUN and PLOT ODE SOLUTION--------------
t01= 0;
tf1=exptime;
tspan1 = LINSPACE(t01,tf1,dps*(tf1-t01));
X01=zeros(5);
[Time1,X1] = ode45(@(t,X) odefuncare(t,X,A,B,Q,R,S,E),tspan1,X01);
plot(Time1,X1)

another m file :
function dxdt = odefuncare(t,X,A,B,Q,R,S,E)
X = reshape(X,5,5); %converting X from a column vector generated by ode45 into a 5 x 5 Matrix
dxdt=((A')*X*E)+((E')*X*A)-(((E')*(X)*(B)+S)*(inv(R))*((B')*(X)*(E)+(S')))+Q;
dxdt = dxdt( : ) ;

here suppose under and up equations are couple(s) and it could not solved in 2 separated m-files

K=X*T
T is a matrix (5*1)
ydot(1) =f (y(1), y(2),y(3) ,y(4) ,y(5)) + k(1)*y(1)
ydot(2)=g( (y(1), y(2),y(3) ,y(4) ,y(5)) + k(2)*y(2)
ydot(3) =h((y(1), y(2),y(3) ,y(4) ,y(5)) + k(3)*y(3)
ydot(4)=I( (y(1), y(2),y(3) ,y(4) ,y(5)) + k(4)*y(4)
ydot(5) =J( y(1), y(2),y(3) ,y(4) ,y(5)) + k(5)*y(5)

initial condition :
y0=[ y10 ; y20; y30 ; y40; y50]
• Oct 2nd 2010, 11:58 PM
CaptainBlack
Quote:

If we have differential variable of matrix kind ( with dimention 3*3 ) and non matrix ( with dimention 1) simaltanously and as a couple ,how can we use ode45 ?
If we have only matrix kind , we can use m file of ode at first with the command of reshape (3,3) and then change it to column one.What is the solution in this case ?
P,Q,A :matrix (3*3)

Pdot= A*P +P*A’ +Q
K=P*[1 0 0]
xdot(1) =f(x(1),x(2),x(3)) +k(1)*x(1)
xdot(2)=g(x(1),x(2),x(3)) +k(2)*x(2)
xdot(3)=h(x(1) ,x(2),x(3)) +k(3)*x(3)
x0=[ xa;xb;xc]
P0=x01

Example (only matrix differential equation) :
%-----MAIN M-FILE--------------
Code:

```clear all %-----VARIABLE SET-UP-------------- A = [0 1 0; 0  0 5; 1 2 3]; Q = [1 0 0; 0 0.5 0; 0 0 0.9]; %-----TO RUN and PLOT ODE SOLUTION-------------- t01= 0; tf1=30;                  tspan1 = LINSPACE(t01, tf1);  X01=zeros(3); [Time1,X1] = ode45(@(t,X) odefuncare(t,X,A,Q),tspan1,X01); plot(Time1,X1) %-------ODEFUNCTION------------------ function dxdt = odefuncare(t,X,A,Q) X = reshape(X,3,3);  %converting X from a column vector  generated by ode45 into a 3 x 3Matrix dxdt=A*x +x*A’ +Q dxdt = dxdt(:);      %converting dxdt into a column vector  as expected by ode45```

You convert your problem to a first order system of ODEs and use ODE45 to solve that system, then extract what you need from the solution to that.

Also, don't bump. The reason you have received no replies earlier is that you question is posted in an almost incomprehensible manner.

CB
• Oct 8th 2010, 06:25 AM
corrected
Quote:

Originally Posted by CaptainBlack
You convert your problem to a first order system of ODEs and use ODE45 to solve that system, then extract what you need from the solution to that.

Also, don't bump. The reason you have received no replies earlier is that you question is posted in an almost incomprehensible manner.

CB

I corrected my question.
• Oct 8th 2010, 12:30 PM
CaptainBlack
Quote: