Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Solving the same linear ODE with several initial conditions

Subject: Solving the same linear ODE with several initial conditions

From: Robert

Date: 5 Oct, 2011 17:47:11

Message: 1 of 3

Hi, I want to solve a system of the form X(t)'=A*X(t) several times with different initial conditions. Of course, I could solve it separately for each initial condition in a loop, but I was hoping that there is a vectorized method for this.

Here's a more precise description of what I'm looking for: A is an NxN matrix. X0 is MxN where M is the number of initial conditions. TIME is a 1xT vector where T is the number of time bins I want for my solution. I would like a solution X that is MxNxT (or NxMxT, etc) that represents the solution to X'=A*X for each initial condition and at each time.

I first tried using expm since I could apply the result to the matrix X0, but I would need to calculate expm separately at each time bin (requiring a time loop).

I then thought of using ode45 since it takes a vector of times. But it takes only one initial condition (requiring a loop over X0).

Any ideas?

Subject: Solving the same linear ODE with several initial conditions

From: Bruno Luong

Date: 5 Oct, 2011 19:49:31

Message: 2 of 3

"Robert " <robertr@pitt.edu> wrote in message <j6i56v$foe$1@newscl01ah.mathworks.com>...
> Hi, I want to solve a system of the form X(t)'=A*X(t) several times with different initial conditions. Of course, I could solve it separately for each initial condition in a loop, but I was hoping that there is a vectorized method for this.
>
> Here's a more precise description of what I'm looking for: A is an NxN matrix. X0 is MxN where M is the number of initial conditions. TIME is a 1xT vector where T is the number of time bins I want for my solution. I would like a solution X that is MxNxT (or NxMxT, etc) that represents the solution to X'=A*X for each initial condition and at each time.
>
> I first tried using expm since I could apply the result to the matrix X0, but I would need to calculate expm separately at each time bin (requiring a time loop).
>
> I then thought of using ode45 since it takes a vector of times. But it takes only one initial condition (requiring a loop over X0).
>
> Any ideas?

You could use: mtimesx
http://www.mathworks.com/matlabcentral/fileexchange/25977

% Data
T = 100;
N = 3;
M = 10;
A=rand(3);
t = linspace(0,1,T);
X0 = rand(3,M); % I reverse to order

% Engine
[V D] = eig(A);
d = diag(D);
dt = d(:)*t(:).';
E = exp(dt);
E = reshape(E,1,size(A,2),length(t));
X = bsxfun(@times,V,E);
Y = V\X0;
% Solution of dX/dt = A*X and X(t=0) = X0
X = mtimesx(X,Y); % FEX

% Bruno

Subject: Solving the same linear ODE with several initial conditions

From: Roger Stafford

Date: 11 Oct, 2011 00:29:11

Message: 3 of 3

"Robert " <robertr@pitt.edu> wrote in message <j6i56v$foe$1@newscl01ah.mathworks.com>...
> Hi, I want to solve a system of the form X(t)'=A*X(t) several times with different initial conditions. ......
- - - - - - - - -
  A note of caution, Robert. If your matrix A does not possess a full basis of eigenvectors, then a solution may involve polynomials in t as well as exponentials. For example the matrix

 A = [1 0 0 0;1 1 0 0;0 2 1 0;0 0 3 1]

has a single repeated eigenvalue of 1 and only one distinct eigenvector, so its solution may have to involve the functions t^3*exp(t), t^2*exp(t), and t*exp(t). Consult a good book on differential equations or do a Google search for references to this problem.

Roger Stafford

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us