Numerical solution for a matrix ODE in Matlab

140 views Asked by At

I am trying to solve a Cauchy problem of the type

y'=A(t)*y

being A a time-dependent square matrix, depending on a number of parameters.

What I have done is to define a function (in a separate file, as needed)

function timedepf=timedep(time,y,L,t,gamma,Rx,Ry,T)
a=zeros(3*L);
for j=1:L
    a(j,j+1)=-t;
    a(j+1,j)=-t;
    a(L,1)=-t;
    a(1,L)=-t;
    a(L+j,L+j+1)=-t;
    a(L+j+1,L+j)=-t;
    a(L+L,L+1)=-t;
    a(L+1,L+L)=-t;
    a(2*L+j,2*L+j+1)=-t;
    a(2*L+j+1,2*L+j)=-t;
    a(2*L+L,2*L+1)=-t;
    a(2*L+1,2*L+L)=-t;
    a(j,L+j)=Rx*cos(2*pi*time/T)*exp(-1i*gamma*j);
    a(L+j,j)=Rx*cos(2*pi*time/T)*exp(1i*gamma*j);
    a(L+j,2*L+j)=Rx*cos(2*pi*time/T)*exp(-1i*gamma*j);
    a(2*L+j,L+j)=Rx*cos(2*pi*time/T)*exp(1i*gamma*j);
    a(2*L+j,j)=Ry*sin(2*pi*time/T)*exp(-1i*gamma*j);
    a(j,2*L+j)=Ry*sin(2*pi*time/T)*exp(1i*gamma*j);
end
timedepf=a*y;

(please, don't care about the actual form of A, it is just a 3*L X 3*L square matrix) and then to call the solver

time_span=[0 T];
[time,V]=ode45(@(time,V)timedep(time,V,L,t,gamma,Rx,Ry,T),time_span,V0);

where V0 is a suitable 3*L X 3*L initial condition. Then I am getting the errors

Error using  * 
Inner matrix dimensions must agree.

Error in timedep (line 23)
timedepf=a*y;
Error in @(time,V)timedep(time,V,L,t,gamma,Rx,Ry,T)


Error in odearguments (line 88)
f0 = feval(ode,t0,y0,args{:});   % ODE15I sets args{1} to yp0.

Error in ode45 (line 114)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...

Error in Pumping (line 65)
[time,V]=ode45(@(time,V)timedep(time,V,L,t,gamma,Rx,Ry,T),time_span,V0);

I cannot find out where I am wrong. Can you please help me?

Thank you very much!

0

There are 0 answers